AI Coding Agents

Give every AI agent its own isolated environment to write and run code safely
View as MarkdownOpen in Claude

One of the most powerful use cases for Modbox is giving AI coding agents a fully isolated execution environment. Instead of running agent-generated code on your infrastructure, each agent gets its own sandbox — with its own filesystem, network, and process space.

The problem without sandboxes

When an AI agent writes and runs code, a few things can go wrong:

  • Security — agent-generated code can exfiltrate secrets, delete files, or call external APIs you didn’t authorize
  • Isolation — one agent’s side effects can corrupt another agent’s state
  • Cleanup — leftover processes, temporary files, and installed packages accumulate
  • Resource limits — a runaway loop can consume all available CPU or memory

Modbox solves all of this by giving each agent its own container with a configurable TTL.

Architecture

Your Backend
├── Agent 1 ──► Sandbox A (task_id: agent-abc123) ──► sandbox-a.modbox.run
├── Agent 2 ──► Sandbox B (task_id: agent-def456) ──► sandbox-b.modbox.run
└── Agent 3 ──► Sandbox C (task_id: agent-ghi789) ──► sandbox-c.modbox.run

Each sandbox is completely isolated. Agents communicate with their sandbox via HTTP — sending code to execute and receiving output back.

Example: AI coding agent with per-session sandboxes

1import { ModboxClient } from "modbox-sdk";
2import { generateAgentCode } from "./your-llm-client";
3
4const modbox = new ModboxClient({ token: process.env.MODBOX_API_TOKEN });
5
6async function runAgentSession(userPrompt: string, sessionId: string) {
7 // 1. Provision a fresh sandbox for this agent session
8 const { sandboxId } = await modbox.provisionSandbox({
9 taskId: `agent-${sessionId}`,
10 imageId: process.env.CODE_EXEC_IMAGE_ID, // your code execution image
11 ttlSeconds: 600, // auto-destroy after 10 minutes
12 envVars: {
13 SESSION_ID: sessionId,
14 },
15 });
16
17 // 2. Wait for it to be ready
18 await modbox.waitForSandbox({
19 taskId: `agent-${sessionId}`,
20 timeout: 30,
21 });
22
23 const sandbox = await modbox.getSandbox(sandboxId);
24
25 // 3. Let the LLM generate code
26 const code = await generateAgentCode(userPrompt);
27
28 // 4. Execute the code inside the isolated sandbox
29 const result = await fetch(`${sandbox.sandboxUrl}/execute`, {
30 method: "POST",
31 headers: { "Content-Type": "application/json" },
32 body: JSON.stringify({ language: "python", code }),
33 });
34
35 const output = await result.json();
36
37 // 5. Destroy the sandbox when done (or let TTL handle it)
38 await modbox.destroySandbox({ taskId: `agent-${sessionId}` });
39
40 return output;
41}

Multi-step agent loops

For agents that run multiple steps (like ReAct or tool-calling loops), keep the sandbox alive across steps:

1async function runMultiStepAgent(sessionId: string) {
2 const taskId = `agent-${sessionId}`;
3
4 // Provision once
5 await modbox.provisionSandbox({ taskId, ttlSeconds: 1800 });
6 await modbox.waitForSandbox({ taskId, timeout: 30 });
7 const sandbox = await modbox.getSandbox(taskId);
8
9 // Run multiple steps in the SAME sandbox (state persists between steps)
10 for (const step of agentSteps) {
11 const result = await fetch(`${sandbox.sandboxUrl}/execute`, {
12 method: "POST",
13 body: JSON.stringify({ code: step.code }),
14 });
15 // Feed output back to the LLM for the next step
16 step.output = await result.json();
17 }
18
19 // Destroy when the agent finishes
20 await modbox.destroySandbox({ taskId });
21}

State persists within a sandbox session — installed packages, created files, and environment variables are all retained across multiple executions in the same sandbox.

Recommendations

ScenarioRecommended TTL
Single code execution60–120s
Multi-step agent loop600–1800s
Long-running background agent3600s+
Interactive dev session7200s

Always set a TTL. Without it, a crashed agent can leave sandboxes running indefinitely, incurring costs.