Kimi Code
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.
Kimi Code is a Moonshot AI agent with tools for reading and writing files, executing commands, and searching. Use the kimi template to run the official CLI in a separate sandbox and retrieve results from your application.
Connect
Install the AgentBox SDK and set AGENTBOX_API_KEY.
The examples call moonshotai/kimi-k2.5 through OpenRouter. The application reads OPENROUTER_API_KEY and passes it to the CLI as KIMI_MODEL_API_KEY. KIMI_MODEL_PROVIDER_TYPE, KIMI_MODEL_BASE_URL, and KIMI_MODEL_NAME select the OpenAI-compatible protocol, OpenRouter endpoint, and model. These settings apply to the process invocation and do not require interactive login.
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.
Draft a customer reply
The example supplies order details and asks Kimi to write a response draft to reply.md. The application retrieves the file through the filesystem SDK.
import { Sandbox } from "@abox-dev/sdk";
const apiKey = process.env.OPENROUTER_API_KEY;
if (!apiKey) throw new Error("OPENROUTER_API_KEY is required");
const proxy =
process.env.SANDBOX_PROXY_URL ?? "https://sandbox-proxy.agentbox.ru:65181";
const sandbox = await Sandbox.create("kimi", { timeoutMs: 600_000 });
try {
await sandbox.files.makeDir("/home/user/work");
await sandbox.files.write(
"/home/user/work/request.txt",
"Customer Acme asks when order 1042 will arrive. Order 1042 shipped on September 18, 2026. Estimated delivery is September 21, 2026. Tracking number: AB1042.\n",
);
const result = await sandbox.commands.run('kimi -p "$AGENT_PROMPT"', {
cwd: "/home/user/work",
timeoutMs: 300_000,
envs: {
HTTPS_PROXY: proxy,
HTTP_PROXY: proxy,
NO_PROXY: "localhost,127.0.0.1",
AGENT_PROMPT:
"Read request.txt and write reply.md with a short response to the customer.",
KIMI_MODEL_API_KEY: apiKey,
KIMI_MODEL_PROVIDER_TYPE: "openai",
KIMI_MODEL_BASE_URL: "https://openrouter.ai/api/v1",
KIMI_MODEL_NAME: "moonshotai/kimi-k2.5",
KIMI_MODEL_MAX_CONTEXT_SIZE: "262144",
KIMI_MODEL_CAPABILITIES: "tool_use",
},
onStderr: (chunk) => process.stderr.write(chunk),
});
console.log(result.stdout);
console.log(
"reply.md\n" + (await sandbox.files.read("/home/user/work/reply.md")),
);
} finally {
await sandbox.kill();
}-p runs the task without the interactive UI. Regular tools run automatically without approval prompts in this mode. Separate --auto or --yolo flags are not needed. The prompt is passed through an environment variable and expanded inside quotes, preserving it as one shell argument.
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
--output-format stream-json returns JSONL messages. The example displays answers and tool names, obtains the session ID, and supplies a delivery update in the same session.
import { Sandbox } from "@abox-dev/sdk";
const apiKey = process.env.OPENROUTER_API_KEY;
if (!apiKey) throw new Error("OPENROUTER_API_KEY is required");
const proxy =
process.env.SANDBOX_PROXY_URL ?? "https://sandbox-proxy.agentbox.ru:65181";
const sandbox = await Sandbox.create("kimi", { timeoutMs: 600_000 });
try {
await sandbox.files.makeDir("/home/user/work");
await sandbox.files.write(
"/home/user/work/request.txt",
"Customer Acme asks when order 1042 will arrive. Order 1042 shipped on September 18, 2026. Estimated delivery is September 21, 2026. Tracking number: AB1042.\n",
);
async function execute(prompt, sessionId) {
let pending = "",
nextSessionId;
function event(message) {
if (message.role === "assistant") {
if (message.content) console.log(message.content);
for (const call of message.tool_calls ?? [])
console.log("Tool:", call.function.name);
}
if (message.type === "session.resume_hint")
nextSessionId = message.session_id;
}
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(
'kimi -p "$AGENT_PROMPT" --output-format stream-json' +
(sessionId ? ' --session "$AGENT_SESSION_ID"' : ""),
{
cwd: "/home/user/work",
timeoutMs: 300_000,
envs: {
HTTPS_PROXY: proxy,
HTTP_PROXY: proxy,
NO_PROXY: "localhost,127.0.0.1",
AGENT_PROMPT: prompt,
KIMI_MODEL_API_KEY: apiKey,
KIMI_MODEL_PROVIDER_TYPE: "openai",
KIMI_MODEL_BASE_URL: "https://openrouter.ai/api/v1",
KIMI_MODEL_NAME: "moonshotai/kimi-k2.5",
KIMI_MODEL_MAX_CONTEXT_SIZE: "262144",
KIMI_MODEL_CAPABILITIES: "tool_use",
AGENT_SESSION_ID: sessionId ?? "",
},
onStdout,
onStderr: (chunk) => process.stderr.write(chunk),
},
);
if (pending.trim()) event(JSON.parse(pending));
if (!nextSessionId) throw new Error("Agent did not return a session ID");
console.log("\nSession:", nextSessionId);
return nextSessionId;
}
const sessionId = await execute(
"Read request.txt and write reply.md with a short response to the customer.",
);
await execute(
"Logistics has confirmed delivery on September 22, 2026 instead. Update reply.md with the new date.",
sessionId,
);
console.log(
"reply.md\n" + (await sandbox.files.read("/home/user/work/reply.md")),
);
} finally {
await sandbox.kill();
}Messages with role: assistant contain text in content and calls in tool_calls. Tool results arrive with role: tool. The final session.resume_hint provides session_id, which the example passes to --session. This streams messages and tool results rather than individual tokens. The process must exit successfully before the application reads the output file.
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 Kimi Code reference.