Grok
Image updates
Ready-made agent images are updated every morning with the current CLI version. To keep a fixed image version, pin a template version.
Grok Build is an xAI agent for working with files, commands, and external tools. The ready-made grok template contains the official CLI. Your application starts it through the AgentBox commands API and retrieves a response or output file.
Connect
Install the AgentBox SDK and set AGENTBOX_API_KEY.
The xAI key is read from XAI_API_KEY and passed only to the agent process. The examples select grok-4.3. Run grok models inside the sandbox with your key to list available models.
For model access, you can use the AgentBox HTTPS proxy https://sandbox-proxy.agentbox.ru:65181.
The examples pass HTTPS_PROXY, HTTP_PROXY, and NO_PROXY to the agent process. These settings may also affect other requests made by the agent and its child processes. Connections to localhost and 127.0.0.1 remain direct.
Prepare a delivery summary
The example uploads a delivery export and asks Grok to write delayed orders and their known causes to exceptions.md. The application then reads the file through the SDK.
import { Sandbox } from "@abox-dev/sdk";
const apiKey = process.env.XAI_API_KEY;
if (!apiKey) throw new Error("XAI_API_KEY is required");
const proxy =
process.env.SANDBOX_PROXY_URL ?? "https://sandbox-proxy.agentbox.ru:65181";
const sandbox = await Sandbox.create("grok", { timeoutMs: 600_000 });
try {
await sandbox.files.makeDir("/home/user/work");
await sandbox.files.write(
"/home/user/work/shipments.csv",
`order,customer,status,note
1042,Acme,delayed,Awaiting warehouse pickup
1043,Northwind,delivered,Delivered on September 18
1044,Contoso,delayed,Address confirmation required
`,
);
await sandbox.files.write(
"/home/user/work/task.txt",
"Read shipments.csv and write exceptions.md for the logistics team: delayed orders and their known reasons.",
);
const result = await sandbox.commands.run(
"grok --always-approve --sandbox off --model grok-4.3 --prompt-file task.txt",
{
cwd: "/home/user/work",
timeoutMs: 300_000,
envs: {
XAI_API_KEY: apiKey,
HTTPS_PROXY: proxy,
HTTP_PROXY: proxy,
NO_PROXY: "localhost,127.0.0.1",
},
onStderr: (chunk) => process.stderr.write(chunk),
},
);
console.log(result.stdout);
console.log(
"exceptions.md\n" +
(await sandbox.files.read("/home/user/work/exceptions.md")),
);
} finally {
await sandbox.kill();
}--prompt-file task.txt runs one task without the interactive UI. --always-approve permits tools without approval prompts. --sandbox off disables Grok’s additional isolation layer. The AgentBox sandbox provides the environment boundary.
The example reads the result before killing the sandbox. finally also releases it on error. The Go example uses a separate context for cleanup.
Events and session continuation
With --output-format streaming-json, Grok emits newline-delimited JSON objects. The example displays response text and tool names, waits for completion, and continues the same session with an updated delivery status.
import { Sandbox } from "@abox-dev/sdk";
const apiKey = process.env.XAI_API_KEY;
if (!apiKey) throw new Error("XAI_API_KEY is required");
const proxy =
process.env.SANDBOX_PROXY_URL ?? "https://sandbox-proxy.agentbox.ru:65181";
const sandbox = await Sandbox.create("grok", { timeoutMs: 600_000 });
try {
await sandbox.files.makeDir("/home/user/work");
await sandbox.files.write(
"/home/user/work/shipments.csv",
`order,customer,status,note
1042,Acme,delayed,Awaiting warehouse pickup
1043,Northwind,delivered,Delivered on September 18
1044,Contoso,delayed,Address confirmation required
`,
);
async function execute(prompt, sessionId) {
await sandbox.files.write("/home/user/work/task.txt", prompt);
let pending = "",
nextSessionId,
terminal,
failure;
function event(message) {
if (message.type === "text") process.stdout.write(message.data);
if (message.type === "tool_call") console.log("Tool:", message.title);
if (message.type === "end") {
terminal = message.stopReason;
nextSessionId = message.sessionId;
}
if (message.type === "error") failure = new Error(message.message);
}
function onStdout(chunk) {
pending += chunk;
let newline;
while ((newline = pending.indexOf("\n")) !== -1) {
const line = pending.slice(0, newline);
pending = pending.slice(newline + 1);
if (line.trim()) event(JSON.parse(line));
}
}
await sandbox.commands.run(
"grok --always-approve --sandbox off --model grok-4.3 --prompt-file task.txt --output-format streaming-json" +
(sessionId ? ' --resume "$AGENT_SESSION_ID"' : ""),
{
cwd: "/home/user/work",
timeoutMs: 300_000,
envs: {
XAI_API_KEY: apiKey,
HTTPS_PROXY: proxy,
HTTP_PROXY: proxy,
NO_PROXY: "localhost,127.0.0.1",
AGENT_SESSION_ID: sessionId ?? "",
},
onStdout,
onStderr: (chunk) => process.stderr.write(chunk),
},
);
if (pending.trim()) event(JSON.parse(pending));
if (failure) throw failure;
if (terminal !== "end_turn")
throw new Error("Agent did not finish normally: " + terminal);
if (!nextSessionId) throw new Error("Agent did not return a session ID");
console.log("\nSession:", nextSessionId);
return nextSessionId;
}
const sessionId = await execute(
"Read shipments.csv and write exceptions.md for the logistics team: delayed orders and their known reasons.",
);
await execute(
"The warehouse confirmed that order 1042 was picked up. Update exceptions.md and keep the other order details.",
sessionId,
);
console.log(
"exceptions.md\n" +
(await sandbox.files.read("/home/user/work/exceptions.md")),
);
} finally {
await sandbox.kill();
}In a text event, data holds a response fragment. tool_call announces a tool invocation. The end event contains sessionId and stopReason. The example accepts end_turn as normal completion and passes the returned ID to --resume. --session-id creates a new session with a chosen UUID. It does not resume an existing one.
Session history and files are stored in the sandbox. For another call, keep it and the session ID, or pause it. The full protocol and CLI options are described in the Grok Build reference.