# Google ADK

[`adk-kosa8`](https://github.com/kosa8ai/adk-kosa8) connects [Google's Agent Development Kit](https://adk.dev) to kosa8 Cloud in two ways. `Kosa8CodeExecutor` is an ADK code executor: each Python block the model writes runs in a kosa8 sandbox instead of on your machine. `kosa8_mcp_toolset()` gives the agent kosa8's sandbox tools from the [hosted MCP server](https://docs.kosa8.com/guides/cloud-mcp/), so the model can start sandboxes, run commands and read files itself.

**How this page was tested**

adk-kosa8 0.1.0 with google-adk 2.15.1 and kosa8 0.11.0 on Python 3.12, against kosa8 Cloud in production on 26 Nov 2026. The tests covered create, running code, files across code blocks, error passthrough, a timeout, input and output files, stateful variables, an allow-list of one host, a whole ADK run with a scripted model, and the MCP tool list. They left no sandboxes running. No hosted model was used for the run, so the examples' prompts were not run against Gemini.

## Install

```
pip install adk-kosa8
export KOSA8_API_KEY=k8c_live_…      # or run `kosa8 login` once
```

It needs Python 3.11 or newer, and installs `google-adk[mcp]` 2.x and the [kosa8 Python SDK](https://docs.kosa8.com/guides/py-quickstart/).

## Run the agent's code in kosa8

```
from google.adk.agents import LlmAgent
from adk_kosa8 import Kosa8CodeExecutor

root_agent = LlmAgent(
    name="python_coder",
    model="gemini-flash-latest",
    instruction="Solve problems by writing Python in ```python blocks. Answer from the output.",
    code_executor=Kosa8CodeExecutor(size="1x2", timeout_seconds=120),
)
```

- **One sandbox per ADK session** by default, from the `python` template. It is created on the session's first code block and reused for later ones, so files the code writes are still there. `sandbox_scope="call"` gives each block a new sandbox that is deleted when the block finishes. `sandbox_scope="executor"` shares one sandbox across sessions.

- **What the model sees:** stdout, stderr and the exit code. An exception is a traceback in stderr and exit code 1, so ADK's error retries apply.

- **Timeouts:** `timeout_seconds` (default 300) stops the block in the sandbox. The result then has exit code 124 and `Code execution timed out after N seconds.` in stderr.

- **Files:** everything the code writes under `/workspace/outputs` (`$ADK_OUTPUT_DIR`) comes back as an output file, and ADK saves it as an artifact. Input files from `CodeExecutorContext.add_input_files()` are in `/workspace/inputs` (`$ADK_INPUT_DIR`). Both folders are emptied before each block.

- **Clean up:** `executor.close()`, or `with Kosa8CodeExecutor() as ex:`, deletes the sandboxes it made. Each sandbox also gets a one-hour lifetime (`ttl_s`), after which kosa8 deletes it anyway.

- **kosa8 errors raise:** a bad key is `Kosa8AuthError`, no plan or credit is `Kosa8PaymentRequiredError` (with `upgrade_url`). They are not passed to the model as code output.

### Stateful code blocks

```
Kosa8CodeExecutor(stateful=True)
```

Variables, imports and functions stay from one block to the next, as in a notebook. Each block runs in the [code interpreter](https://docs.kosa8.com/guides/cloud-code-interpreter/) of a persistent sandbox, one context per ADK session. The value of the block's last expression is added to stdout, and charts come back as PNG output files. A stateful block takes at most 80 seconds.

### Lock the network to one host

```
Kosa8CodeExecutor(allow_hosts=["pypi.org"])   # [] for no network at all
```

The sandbox reaches `pypi.org` and nothing else. A request to any other host fails at DNS. The policy is enforced on the host, outside the sandbox, so code inside cannot change it. For presets and deny lists, pass a whole policy as `egress=`; see [Egress controls](https://docs.kosa8.com/guides/cloud-egress/). Without either, the sandbox gets your organization's policy.

### Options

| Option | Default | What it does |
|---|---|---|
| `template`, `size`, `region` | `python`, the template's, chosen by kosa8 | What new sandboxes run on |
| `allow_hosts` / `egress` | org policy | Network policy |
| `sandbox_scope` | `session` | `session`, `executor` or `call` |
| `stateful` | `False` | Keep Python variables between blocks |
| `timeout_seconds` | 300 (80 stateful) | Time limit for one block |
| `ttl_s`, `sleep_after_s` | 3600, kosa8's | Lifetime and idle sleep of each sandbox |
| `env`, `labels` | none | Environment variables and kosa8 labels |
| `api_key`, `client` | `KOSA8_API_KEY` | Credentials, or a `kosa8.Kosa8Cloud` to share |

## Give the agent kosa8's tools

```
from google.adk.agents import LlmAgent
from adk_kosa8 import SANDBOX_TOOLS, kosa8_mcp_toolset

root_agent = LlmAgent(
    name="kosa8_tools",
    model="gemini-flash-latest",
    instruction="Run code with the kosa8 sandbox tools. Delete every sandbox you create.",
    tools=[kosa8_mcp_toolset(tool_filter=SANDBOX_TOOLS)],
)
```

`kosa8_mcp_toolset(api_key=None, tool_filter=None, ...)` is ADK's `McpToolset` over Streamable HTTP to `https://api.kosa8.com/mcp`, with the key as a Bearer token. `SANDBOX_TOOLS` limits it to `run`, `templates_list`, `sandbox_create`, `sandbox_get`, `sandbox_list`, `sandbox_exec`, `sandbox_exec_status`, `file_read`, `file_write`, `file_list` and `sandbox_delete`. Without a filter the agent gets every tool the server lists. Other arguments go to `McpToolset`, for example `require_confirmation=True`.

Use the code executor when the model writes code for you to run. Use the toolset when the model should manage sandboxes itself.

## Examples

The repository's [`examples/`](https://github.com/kosa8ai/adk-kosa8/tree/main/examples) folder has three agents for `adk web examples`: `python_coder`, `egress_locked` (only `pypi.org` is reachable, so a request to `example.com` is blocked) and `mcp_tools`. `examples/executor_only.py` runs the executor with no model.

## Security notes

- Model-written code can be steered by what the model reads. kosa8 limits what that code can do, through the sandbox, its egress policy and its lifetime. It does not stop prompt injection itself.

- Keep API keys out of `env`. Store them as [secrets](https://docs.kosa8.com/guides/cloud-secrets/), so the sandbox holds only a placeholder.

- When kosa8's hosts are full, a plain sandbox may run on [overflow capacity](https://docs.kosa8.com/guides/cloud/), which is not a kosa8 microVM. Stateful, egress-policy and named-region sandboxes always wait for a kosa8 host.

---
Source: https://docs.kosa8.com/guides/google-adk/
