Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ pip install kimi-agent-sdk
```

Python quick start: [guides/python/quickstart.md](./guides/python/quickstart.md)

## Examples

- Go: [examples/go/ralph-loop](./examples/go/ralph-loop), [examples/go/rumor-buster](./examples/go/rumor-buster)
- Python: [examples/python/customized-tools](./examples/python/customized-tools), [examples/python/e2b-sandbox](./examples/python/e2b-sandbox)
73 changes: 73 additions & 0 deletions examples/python/e2b-sandbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Example: E2B Sandbox

This example demonstrates how to run Kimi Agent in an [E2B](https://e2b.dev) cloud sandbox using **KAOS** (Kimi Agent Operating System).

## What is KAOS?

**KAOS** is a runtime abstraction layer that decouples agent tools from the underlying execution environment. It defines a standard interface for file system operations, process execution, and path manipulation—allowing the same agent code to run locally or in a cloud sandbox.

```mermaid
flowchart LR
subgraph Server
App["Your App"] --> SDK["Kimi Agent SDK"] --> CLI["Kimi CLI"]
subgraph Tools["Tools"]
ReadFile["ReadFile"]
WriteFile["WriteFile"]
Shell["Shell"]
end
CLI --- Tools
end

subgraph E2B["E2B Sandbox"]
FS[("Filesystem")]
SH{{"Shell"}}
end

ReadFile -->|"E2BKaos.readtext()"| FS
WriteFile -->|"E2BKaos.writetext()"| FS
Shell -->|"E2BKaos.exec()"| SH

style Tools stroke-dasharray: 5 5
```

By default, tools operate on your local filesystem. With `set_current_kaos(E2BKaos(...))`, all tool calls are transparently routed to the E2B sandbox—no agent code changes required.

## How It Works

This example vendors an `E2BKaos` implementation in `e2b_kaos.py`, then installs it via `set_current_kaos` so Kimi CLI tools operate on the E2B sandbox environment:

```python
from e2b import AsyncSandbox
from kaos import set_current_kaos
from kaos.path import KaosPath
from kimi_agent_sdk import prompt

# Create an E2B sandbox and install as KAOS backend
sandbox = await AsyncSandbox.create()
set_current_kaos(E2BKaos(sandbox, cwd="/home/user"))

# Agent tools now operate inside the sandbox
async for msg in prompt("Create a Python project", work_dir=KaosPath("/home/user")):
print(msg.extract_text(), end="")
```
Comment on lines +39 to +52
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the "How It Works" code snippet, E2BKaos is used but never imported, so readers copying this snippet as-is will get a NameError. Please add the missing import (e.g., importing E2BKaos from e2b_kaos) to keep the example self-contained and runnable.

Copilot uses AI. Check for mistakes.

## Run

```sh
cd examples/python/e2b-sandbox
uv sync --reinstall

# Required
export KIMI_API_KEY=your-api-key
export KIMI_BASE_URL=https://api.moonshot.ai/v1
export KIMI_MODEL_NAME=kimi-k2-thinking-turbo
export E2B_API_KEY=your-e2b-api-key

# Optional
export E2B_SANDBOX_ID=...
export KIMI_WORK_DIR=/home/user/kimi-workdir

uv run main.py
```

If `E2B_SANDBOX_ID` is not set, the script creates a sandbox and prints the ID. The sandbox lifecycle is managed outside of the SDK.
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The README suggests that setting E2B_SANDBOX_ID will cause the script to connect to an existing sandbox ("If E2B_SANDBOX_ID is not set, the script creates a sandbox..."), but main.py's _get_sandbox currently ignores E2B_SANDBOX_ID and always calls AsyncSandbox.create. Please either implement the E2B_SANDBOX_ID connect flow in _get_sandbox (as hinted in the commented-out code) or update this section of the README to reflect the current behavior so users are not misled.

Suggested change
If `E2B_SANDBOX_ID` is not set, the script creates a sandbox and prints the ID. The sandbox lifecycle is managed outside of the SDK.
This example currently always creates a new sandbox instance and prints its ID; `E2B_SANDBOX_ID` is reserved for future use, and the sandbox lifecycle is managed outside of the SDK.

Copilot uses AI. Check for mistakes.
7 changes: 7 additions & 0 deletions examples/python/e2b-sandbox/agent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 1
agent:
extend: default
name: "e2b"
# Grep tool only supports local KAOS; disable it for E2B sandboxes.
exclude_tools:
- "kimi_cli.tools.file:Grep"
Loading
Loading