Skip to content

Docs · Agents

Let an agent use sandboxes

Your agent gets three tools: start a sandbox, run a command, stop it. You set the limits once, on the key.

How an agent uses Kalworks

  1. 1. Start. The agent calls sandbox_create with a workload. It gets an ID once the sandbox is ready.
  2. 2. Work. It calls sandbox_run and sandbox_write_file as many times as it needs, reading exit codes and output.
  3. 3. Stop. It calls sandbox_stop. If it doesn’t, the runtime limit stops it.

Every call shows up in the sandbox’s Events tab with the agent key name, so you can see exactly what it did.

Connect over MCP

Point any MCP client at the Kalworks server. Use an agent key, not your personal key.

{
  "mcpServers": {
    "kalworks": {
      "url": "https://mcp.kalworks.dev",
      "headers": { "Authorization": "Bearer kw_agent_…" }
    }
  }
}

Or define tools yourself

AI SDK
import { tool } from "ai"
import { z } from "zod"
import { Kalworks } from "kalworks"

const kw = new Kalworks({ apiKey: process.env.KALWORKS_AGENT_KEY })

export const runCode = tool({
  description: "Run a shell command in an isolated sandbox and return its output.",
  inputSchema: z.object({ command: z.array(z.string()) }),
  execute: async ({ command }) => {
    await using sb = await kw.sandboxes.create({ workload: "rl-v1", shutDownAfterMin: 5 })
    const r = await sb.run(command, { timeoutS: 120 })
    return { exitCode: r.exitCode, stdout: r.stdout.slice(-4000), stderr: r.stderr.slice(-2000) }
  },
})
One sandbox per tool call is simplest. Keep one open across calls when the agent needs state between steps.

Limits the agent can’t change

Agent key
A key with scope sandboxes:run only. It can’t create fleets, change billing, or make other keys.
Spend cap
Each agent key has a daily budget. At the cap, create calls fail with budget_reached.
Max runtime
Every sandbox the agent creates stops after its runtime limit, even if the agent forgets.
Default-deny network
Agent sandboxes get no outbound network unless the workload allows named hosts.
Attribution
Each sandbox is labeled with the agent key name, so Usage shows what the agent spent.