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.

Set up a sandbox template

Page as Markdown

Create a sandbox template, then start a Sandbox from it and run a command.

Set up a sandbox template

Create a sandbox template, then start a Sandbox from it and run a command.

About this tutorial

A Sandbox is an isolated scratch environment that a person, a script, or an agent creates to run commands. A sandbox template says what every Sandbox of one kind starts with: the container image that holds its programs, and the worker pool that runs it.

In this tutorial, you create a template for Python, start a Sandbox from it, and run a command in it. The kagent UI has no page for sandbox templates, so you work from the command line. For what a Sandbox is and what isolates it, see Sandboxes and Core concepts.

A terminal creating the python sandbox template, waiting for it to be ready, creating a Sandbox from it, running python --version in it, and deleting it
Figure: Creating the template and running a command in a Sandbox

Time: about 10 minutes.

Before you begin

  1. Install kagent, and make sure that the kagent CLI and jq are on your path.

  2. Set 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 template does not become ready until you set one.

    cat > sandbox-values.yaml <<'EOF'
    config:
      sandboxes:
        guestImage: ghcr.io/kagent-dev/kagent/sandbox-guest@sha256:16e84a2285119ce02cc6ba45ea7f68181eb1527335de85477614fb9a6b5ecf03
    EOF
  3. Apply the file to your kagent release. The commands 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

Choose an image

The image holds every program that the Sandbox can run. A Sandbox has no network access unless you grant it, so a command cannot install a missing package later.

  1. Pick an image that already contains every program that your commands need. This tutorial uses python:3.13-slim.

  2. Read the image’s digest. kagent accepts only an image that is pinned to a digest.

    docker buildx imagetools inspect python:3.13-slim --format '{{json .Manifest.Digest}}'

Create the template

  1. Apply the template. It uses the kagent-default worker pool and the snapshot location that the installation sets up.

    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. Wait until the template is ready. kagent builds a snapshot of the template first, which can take a minute the first time that a Worker pulls the image.

    kubectl wait sandboxtemplate/python -n kagent --for=condition=Ready --timeout=5m

    If the command times out, read the reason.

    kubectl get sandboxtemplate python -n kagent \
      -o jsonpath='{range .status.conditions[?(@.type=="Ready")]}{.status} {.reason} {.message}{end}'

    The reason PreparationFailed usually means that the guest image from Before you begin is not set.

  3. List the templates that you can create a Sandbox from.

    kagent sandbox templates

    Example output:

    NAMESPACE  NAME    IMAGE
    kagent     python  docker.io/library/python:3.13-slim@sha256:e48f59cdba69a3cbd9ed2cf0f348d90d45a8d350d98134cf1070bc106ee9ed90
    

Verify the template

  1. Create a Sandbox from the template, and save its ID. The request ID makes the command safe to retry.

    export SANDBOX_ID=$(kagent sandbox create python --request-id "cli-$(uuidgen)" -o json | jq -r .id)
    echo $SANDBOX_ID
  2. Run a command in the Sandbox.

    kagent sandbox exec $SANDBOX_ID -- python --version

    Example output:

    Process proc-2c32646d3e4cac9f0763a1da in sandbox 01a209a5-684a-7106-85ca-8851f2059dd6: started (stdout offset 0, stderr offset 0)
    Python 3.13.18
    Process proc-2c32646d3e4cac9f0763a1da: PROCESS_STATE_EXITED, exit_code=0
    

What you built

A sandbox template for Python, and a Sandbox started from it. Along the way, you used these features:

FeatureWhat you didRead more
Sandbox templatesPinned an image to a digest, and chose the worker pool and snapshot locationSet up sandbox templates
The sandbox CLICreated a Sandbox and ran a command in itRun commands with the CLI

Clean up

  1. Delete the Sandbox.

    kagent sandbox delete $SANDBOX_ID
  2. Optional: Delete the template. Build a sandbox coding agent applies the same python template, so keep it if you go on to that guide.

    kubectl delete sandboxtemplate python -n kagent

Next steps