Skip to content
This documentation covers kagent while it is in development and can describe unreleased features. For the current release, see the latest docs.

Build a sandbox coding agent

Page as Markdown

Build an agent that writes Python code, runs it in an isolated Sandbox, and reports the result.

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.

  1. Install kubectl, helm 3, and kind v0.32.0 or later.

  2. 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.0
  3. Set the API key of your model provider, then install the kagent CLI and run it. kagent install sets 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 install
  4. Check that the kagent pods run. Every pod reports Running.

    kubectl get pods -n kagent

Set the sandbox guest image

  1. 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
    EOF
  2. Apply the file to your kagent release. The commands use jq to 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.

  1. 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/
    EOF
  2. Confirm 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 prepared
    

    If 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 dashboard

The command forwards the UI to http://localhost:8082 and opens it in your browser. Leave it running while you work.

Create the agent

The kagent UI creating an agent named code-runner and choosing the sandbox tools of the kagent-api server
Figure: Creating the code-runner agent with the sandbox tools, sped up
  1. In the sidebar, click Agents, and then click New Agent.

  2. In the Name field, enter code-runner. Leave the Namespace as kagent, and leave Shared harness set to kagent-default.

  3. In the Template section, select Inline, and fill in the template.

    FieldValue
    Model configurationkagent/default-model-config
    DescriptionRuns Python code in a Sandbox and reports the result
  4. In 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.

  5. Under Tools from MCP servers, click Add an MCP server, and choose kagent/kagent-api from the MCP server list. This server is kagent’s own, and it offers the sandbox tools.

  6. 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, and delete_sandbox.

  7. 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

  1. Click the agent name, code-runner, to open its chat page.

  2. 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

  1. Check that the agent is ready.

    kubectl get agent code-runner -n kagent

    Example output:

    NAME          READY   AGE
    code-runner   True    5m
    
  2. Check the reply to the request that you sent. It reports the program output, which contains 10, and the exit code 0.

  3. Check that the agent deleted its Sandbox. The conversation in the kagent UI shows a delete_sandbox call 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:

FeatureWhat you didRead more
Sandbox templatesSet the guest image, and created a template from a Python imageSandbox templates
Agent sandbox toolsGave the agent the sandbox tools of the built-in kagent-api serverGive an agent sandbox tools
Agents and inline templatesTold the agent in its prompt which template to use and when to delete a SandboxAbout agents

Clean up

  1. Delete the agent and the sandbox template.

    kubectl delete agent code-runner -n kagent
    kubectl delete sandboxtemplate python -n kagent
  2. Optional: 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

Next steps