Build a sandbox coding agent
Build an agent that writes Python code, runs it in an isolated Sandbox, and reports the result.
About this tutorial
Build an agent that writes a Python program for a request, runs it in a fresh Sandbox, reports the output, and deletes the Sandbox. The Sandbox keeps the code that the agent writes apart from the agent itself, with its own files and no network access unless you grant it.
This tutorial has two parts. You prepare Sandboxes from the command line, because the kagent UI has no page for sandbox templates. Then you build the agent in the kagent UI or as YAML. For how a Sandbox is isolated and how long it lives, see About Sandboxes.
Time: about 20 minutes.
Before you begin
If kagent already runs in your cluster, skip these steps.
Create a local kind cluster. To use a Kubernetes cluster that you already run, at version 1.37 or later, skip this step.
kind create cluster --name kagent --image kindest/node:v1.37.0Set the API key of your model provider, then install the kagent CLI and run it.
kagent installsets up the CRDs, Agent Substrate, a development PostgreSQL database, and kagent, and takes a few minutes.export OPENAI_API_KEY="your-api-key-here" curl -sL https://kagent.dev/install | bash && kagent installCheck that the kagent pods run. Every pod reports
Running.kubectl get pods -n kagent
Set the sandbox guest image
Choose the sandbox guest image. kagent runs its own guest program in every Sandbox, and the chart does not ship a default image for it, so a sandbox template does not become ready until you set one. Save the setting in a values file.
cat > sandbox-values.yaml <<'EOF' config: sandboxes: guestImage: ghcr.io/kagent-dev/kagent/sandbox-guest@sha256:16e84a2285119ce02cc6ba45ea7f68181eb1527335de85477614fb9a6b5ecf03 EOFApply the file to your kagent release. The commands use
jqto read the installed chart version, so that the upgrade keeps that version and the release’s other values.export KAGENT_CHART_VERSION=$(helm list -n kagent -o json \ | jq -er '.[] | select(.name == "kagent") | .chart | ltrimstr("kagent-")') helm upgrade kagent \ oci://ghcr.io/kagent-dev/kagent/helm/kagent \ --namespace kagent --version "$KAGENT_CHART_VERSION" \ --reuse-values -f sandbox-values.yaml --wait
Set up a sandbox template
A SandboxTemplate names the image that every Sandbox starts from. The image holds the programs that the agent can run. This template uses a Python image.
Create the template.
kubectl apply -f - <<EOF apiVersion: api.kagent.dev/v1alpha3 kind: SandboxTemplate metadata: name: python namespace: kagent spec: workload: image: docker.io/library/python:3.13-slim@sha256:e48f59cdba69a3cbd9ed2cf0f348d90d45a8d350d98134cf1070bc106ee9ed90 env: - name: LANG value: C.UTF-8 substrate: workerPoolRef: name: kagent-default snapshotPolicy: location: s3://ate-snapshots/kagent/ EOFConfirm that the template is ready. This step can take a minute the first time, while a Worker pulls the image.
kubectl get sandboxtemplate python -n kagent \ -o jsonpath='{range .status.conditions[?(@.type=="Ready")]}{.status} {.reason} {.message}{end}'Example output:
True Prepared Sandbox runtime is preparedIf the output reports
False PreparationFailed, the guest image from Set the sandbox guest image is not set.
Build the coding agent
Build the agent step by step in the kagent UI, or apply the same resources as YAML and talk to the agent with the kagent CLI.
Open the kagent UI
kagent dashboardThe command forwards the UI to http://localhost:8082 and opens it in your browser. Leave it running while you work.
Create the agent

In the sidebar, click Agents, and then click New Agent.
In the Name field, enter
code-runner. Leave the Namespace askagent, and leave Shared harness set tokagent-default.In the Template section, select Inline, and fill in the template.
Field Value Model configuration kagent/default-model-configDescription Runs Python code in a Sandbox and reports the resultIn the System prompt field, enter the following prompt.
You run Python code for the user in a standalone kagent Sandbox. - Create one Sandbox per request from the python SandboxTemplate in the kagent namespace, with a new request_id and ttl_seconds set to 900. If a call fails, retry it with the same request_id. - Write the code to main.py with write_sandbox_file, with the file bytes as standard base64. - Run it with start_sandbox_process, using the command ["python3", "main.py"]. Poll get_sandbox_process until the state is PROCESS_STATE_EXITED, then read the output with read_sandbox_outputs. - Always delete the Sandbox with delete_sandbox before you answer. - Reply with the program output and its exit code.The prompt names the template, so that the agent does not have to discover one. It also tells the agent to delete the Sandbox, so that Sandboxes do not pile up.
Under Tools from MCP servers, click Add an MCP server, and choose
kagent/kagent-apifrom the MCP server list. This server is kagent’s own, and it offers the sandbox tools.From the tool list beside it, choose these tools:
list_sandbox_templates,create_sandbox,get_sandbox,write_sandbox_file,start_sandbox_process,get_sandbox_process,read_sandbox_outputs,read_sandbox_file, anddelete_sandbox.Click Create agent, and then click Refresh until the agent is Ready. This agent can take a couple of minutes to prepare.
Run code through the agent
Click the agent name,
code-runner, to open its chat page.Send a request, such as
Compute the sum of the numbers [2, 3, 5] in Python and print it as JSON.
The conversation shows each tool call that the agent makes: creating the Sandbox, writing the file, starting the process, reading the output, and deleting the Sandbox.
Verify the agent
Check that the agent is ready.
kubectl get agent code-runner -n kagentExample output:
NAME READY AGE code-runner True 5mCheck the reply to the request that you sent. It reports the program output, which contains
10, and the exit code0.Check that the agent deleted its Sandbox. The conversation in the kagent UI shows a
delete_sandboxcall before the answer.
What you built
An agent that runs the code it writes in a fresh, isolated Sandbox, and cleans up after itself. Along the way, you used these features:
| Feature | What you did | Read more |
|---|---|---|
| Sandbox templates | Set the guest image, and created a template from a Python image | Sandbox templates |
| Agent sandbox tools | Gave the agent the sandbox tools of the built-in kagent-api server | Give an agent sandbox tools |
| Agents and inline templates | Told the agent in its prompt which template to use and when to delete a Sandbox | About agents |
Clean up
Delete the agent and the sandbox template.
kubectl delete agent code-runner -n kagent kubectl delete sandboxtemplate python -n kagentOptional: Delete the local values file. The guest image setting stays on your kagent release, so other sandbox templates can use it.
rm sandbox-values.yaml