Skip to main content
Kimi Code CLI is Moonshot AI’s coding agent and CLI. E2B provides a pre-built kimi template with Kimi Code CLI already installed.

CLI

Create a sandbox with the E2B CLI.
Once inside the sandbox, start Kimi Code CLI.
On first launch, Kimi Code CLI asks you to trust the current folder. Then run /login to sign in with Kimi Code OAuth or a Moonshot AI API key.

Run headless

Use -p to run a single prompt non-interactively. In this mode Kimi Code CLI never asks for approval and runs tool calls automatically. The sandbox isolates the agent from your host machine, but sandboxes can reach the open internet by default — restrict outbound traffic with network rules. Kimi Code CLI authenticates with an API key from the Moonshot AI Open Platform. Pass the key as KIMI_MODEL_API_KEY and the model as KIMI_MODEL_NAME. Kimi Code CLI does not read KIMI_API_KEY from the environment.
Auto-approving tool calls is contained by the sandbox: the agent cannot touch your host machine, local files, or credentials. It can still make outbound network requests — internet access is enabled by default. To limit where an auto-approved agent can connect, configure outbound network rules (allowInternetAccess, plus allow/deny lists by CIDR or hostname). Hostname rules apply to HTTP(S) traffic only; use CIDR rules for other protocols.
The assistant’s reply goes to stdout; thinking and tool progress go to stderr. If the key is missing or invalid, kimi exits with code 1 and prints the error to stderr. To use an OpenAI or Anthropic model instead, set KIMI_MODEL_PROVIDER_TYPE to openai or anthropic, set KIMI_MODEL_NAME to a model from that provider, and pass that provider’s key as KIMI_MODEL_API_KEY.

Example: work on a cloned repository

Streaming events

Add --output-format stream-json to get a JSONL event stream on stdout. Each line is a JSON object with a role: assistant for model replies (including tool_calls), tool for tool results, and meta for run metadata, such as the CLI version. The last event is session.resume_hint, which carries the session ID. See Resume a session.

Resume a session

Kimi Code CLI stores each session under ~/.kimi-code/sessions in the sandbox. Run the first task with --output-format stream-json, read the session ID from the last event (session.resume_hint), and pass it to kimi -r <id> to continue the same session.
To continue the most recent session in the working directory without an ID, use kimi -c -p "follow-up task".

Build a custom template

If you need to customize the environment (e.g. pre-install dependencies, add config files), build your own custom template on top of the pre-built kimi template.
Run the build script to create the template.

Sandbox persistence

Auto-pause, resume, and manage sandbox lifecycle

SSH access

Connect to the sandbox via SSH for interactive sessions