Skip to content

Sandbox execution ​

Sandbox execution gives an agent a disposable workspace for commands and files without running model-generated work in the application process. @anvia/sandbox provides Docker-backed sessions and turns a live session into ordinary Anvia tools.

Explore sandbox execution ​

PageLearn how to
Create a sandboxInstall the package, choose an image, seed a workspace, and attach tools.
File tools and artifactsBound text reads and writes, then move generated artifacts into application storage.
Command executionRun commands directly or let an agent use a restricted command tool.
Processes and previewsManage long-running processes and proxy a loopback-only website preview.
Sessions and cleanupChoose ephemeral or persistent workspaces and guarantee cleanup.
Limits and securityLayer container, network, resource, tool, and approval controls.
Production checklistReview isolation, lifecycle, artifacts, and observability before release.

Create a bounded workspace ​

ts
import { AgentBuilder } from '@anvia/core'
import { createSandboxTools, DockerSandbox } from '@anvia/sandbox'

const sandbox = DockerSandbox.node({
  network: false,
  limits: {
    timeoutMs: 20_000,
    maxOutputBytes: 64_000,
    maxFileBytes: 256_000,
    memoryMb: 512,
    cpus: 1,
    pidsLimit: 64,
  },
})

const session = await sandbox.createSession({
  manifest: {
    files: {
      'input/ticket.txt': ticket.body,
    },
    directories: ['output'],
  },
})

try {
  const tools = createSandboxTools(session, {
    include: ['list_files', 'read_file', 'write_file', 'exec_command'],
    exec: {
      allowedCommands: ['node'],
      maxTimeoutMs: 20_000,
    },
    readFile: { maxBytes: 64_000 },
    writeFile: { maxBytes: 64_000 },
  })

  const agent = new AgentBuilder('ticket-analyzer', model)
    .instructions([
      'Work only inside the sandbox workspace.',
      'Read input/ticket.txt and write the final report to output/report.md.',
      'Do not claim a command succeeded unless its result confirms success.',
    ].join('\n'))
    .tools(tools)
    .defaultMaxTurns(8)
    .build()

  await agent.prompt('Analyze the ticket and create the report.').send()
  const report = await session.readTextFile('output/report.md')
} finally {
  await session.destroy()
}

The session owns the live container and workspace. The application owns when that session may be created, which capabilities the agent receives, what leaves the workspace, and when the session is destroyed.

Understand the boundaries ​

text
application policy
   ├─ image and seeded inputs
   ├─ network and resource limits
   ├─ exposed tools and approvals
   └─ artifact export and cleanup
              ↓
       Docker sandbox session
       ├─ isolated workspace
       ├─ commands and processes
       └─ generated files
              ↓
        selected agent tools

A sandbox reduces exposure to the host process; it does not make arbitrary code safe by itself. Treat command execution as a privileged side effect and combine the package with infrastructure isolation appropriate to the trust level of the code.

Use the right interface ​

Use createSandboxTools(...) for capabilities the model should choose. Use the session methods from trusted application code for setup, artifact export, health checks, and cleanup.

NeedInterface
Let the model inspect a source fileread_file tool
Let the model run an allowed executableexec_command tool
Seed trusted input before the runSession manifest or session.writeFile(...)
Export a binary resultsession.readFile(...)
Start an application-owned commandsession.exec(...) or session.startProcess(...)
End the isolation boundarysession.destroy()

Do not expose every session method merely because the provider supports it. Start from the smallest tool bundle required by the workflow.

Built for Anvia.