The Sandbox primitive is the foundation of Agentbase’s execution model. Every agent session operates within its own isolated sandbox - a lightweight, secure container that provides a clean execution environment. This isolation ensures that:
Security: Agent operations are contained and cannot affect other sessions or the host system
Resource Isolation: Each sandbox has dedicated CPU, memory, and disk resources
Clean State: Every new session starts with a fresh environment
Predictable Execution: Consistent runtime conditions across all sessions
Automatic Creation
Sandboxes are created automatically when you run an agent - no manual setup required
Session-Based
Each sandbox is tied to a session ID and can be reused across multiple requests
Resource Managed
Automatic resource allocation and cleanup based on workload requirements
Network Enabled
Full internet access for API calls, package downloads, and web interactions
import { Agentbase } from '@agentbase/sdk';const agentbase = new Agentbase({ apiKey: process.env.AGENTBASE_API_KEY});// Sandbox is created automaticallyconst result = await agentbase.runAgent({ message: "Create a Python script to analyze data.csv"});console.log('Session ID:', result.session);// Output: Session ID: agent_session_abc123...
from agentbase import Agentbaseagentbase = Agentbase(api_key=os.environ['AGENTBASE_API_KEY'])# Sandbox is created automaticallyresult = agentbase.run_agent( message="Create a Python script to analyze data.csv")print(f"Session ID: {result.session}")# Output: Session ID: agent_session_abc123...
curl -X POST https://api.agentbase.sh \ -H "Authorization: Bearer $AGENTBASE_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "message": "Create a Python script to analyze data.csv" }'# Response includes session ID for sandbox reuse
Maintain state across multiple requests by reusing the same session:
// First request - creates sandboxconst result1 = await agentbase.runAgent({ message: "Install pandas and create a sample CSV file"});// Second request - reuses the same sandboxconst result2 = await agentbase.runAgent({ message: "Now read that CSV file and show the contents", session: result1.session});// The CSV file and pandas installation persist
# First request - creates sandboxresult1 = agentbase.run_agent( message="Install pandas and create a sample CSV file")# Second request - reuses the same sandboxresult2 = agentbase.run_agent( message="Now read that CSV file and show the contents", session=result1.session)# The CSV file and pandas installation persist
Different modes affect sandbox resource allocation:
// Flash mode - lightweight sandboxconst flash = await agentbase.runAgent({ message: "Quick calculation: 123 * 456", mode: "flash"});// Base mode - standard sandbox (default)const base = await agentbase.runAgent({ message: "Analyze this data and create a chart", mode: "base"});// Max mode - enhanced sandbox resourcesconst max = await agentbase.runAgent({ message: "Process large dataset and train ML model", mode: "max"});
# Flash mode - lightweight sandboxflash = agentbase.run_agent( message="Quick calculation: 123 * 456", mode="flash")# Base mode - standard sandbox (default)base = agentbase.run_agent( message="Analyze this data and create a chart", mode="base")# Max mode - enhanced sandbox resourcesmax = agentbase.run_agent( message="Process large dataset and train ML model", mode="max")
Sandboxes are created automatically on the first request:
const result = await agentbase.runAgent({ message: "Hello, create a file called test.txt"});// Sandbox created: agent_session_xyz789// File test.txt exists in sandbox
// File from previous request still existsconst result2 = await agentbase.runAgent({ message: "Read the contents of test.txt", session: result.session});// Successfully reads the file
const dev = await agentbase.runAgent({ message: "Create a React component with TypeScript, install dependencies, and test it"});// Sandbox provides Node.js, npm, and isolated workspace
const analysis = await agentbase.runAgent({ message: "Download this CSV, analyze it, and create visualizations"});// Each analysis runs in its own sandbox - no data leakage
// Step 1: Setupconst setup = await agentbase.runAgent({ message: "Install required packages for web scraping"});// Step 2: Execute (reuses sandbox)const scrape = await agentbase.runAgent({ message: "Now scrape data from these 5 websites", session: setup.session});// Step 3: Process (reuses sandbox)const process = await agentbase.runAgent({ message: "Process the scraped data and create a report", session: setup.session});
const test = await agentbase.runAgent({ message: "Test this algorithm with different inputs and show results"});// Sandbox isolation prevents any side effects
// Good: Reuse session for related workconst session = result1.session;const result2 = await agentbase.runAgent({ message: "Continue from previous step", session});// Avoid: Creating new sandbox for each stepconst result2 = await agentbase.runAgent({ message: "Continue from previous step" // No session ID - creates new sandbox});
Use New Sessions for Unrelated Tasks
// Good: New session for independent taskconst taskA = await agentbase.runAgent({ message: "Process customer data"});const taskB = await agentbase.runAgent({ message: "Generate marketing report" // Different task - don't reuse session});
Store Session IDs for Long-Running Workflows
// Store session ID in databaseawait db.workflows.update({ id: workflowId, sessionId: result.session});// Resume laterconst workflow = await db.workflows.get(workflowId);const continued = await agentbase.runAgent({ message: "Continue the workflow", session: workflow.sessionId});
Mode Selection: Use flash mode for simple tasks, base for standard workloads, and max only when you need advanced reasoning capabilities. This optimizes both cost and performance.
// Optimize by choosing the right modeconst modes = { simple: "flash", // Quick calculations, simple queries standard: "base", // Most development and analysis tasks complex: "max" // Advanced reasoning, complex workflows};const result = await agentbase.runAgent({ message: "Your task here", mode: modes.standard});
Sandboxes provide the execution environment for file operations:
const result = await agentbase.runAgent({ message: "Create multiple files and organize them into folders"});// Sandbox provides the file system where files are created
// Each user's data is isolatedconst userA = await agentbase.runAgent({ message: "Process confidential data"});const userB = await agentbase.runAgent({ message: "Process different confidential data"});// userA and userB run in completely separate sandboxes
Sensitive Data: While sandboxes provide isolation, avoid storing long-term sensitive credentials in sandbox files. Use environment variables or secure parameter passing instead.
// Good: Pass credentials securelyconst result = await agentbase.runAgent({ message: "Connect to database using the provided credentials", system: `Database credentials: ${secureCredentials}`});// Avoid: Writing credentials to filesconst result = await agentbase.runAgent({ message: "Write these credentials to config.json: ..."});
Problem: Session expired or invalidSolution: Create a new session or verify session ID
// Check if session is validconst result = await agentbase.runAgent({ message: "test", session: maybeInvalidSession}).catch(() => { // Session invalid, start fresh return agentbase.runAgent({ message: "test" });});
Disk Space Exceeded
Problem: Sandbox reached 10GB storage limitSolution: Clean up large files or start new session
const cleanup = await agentbase.runAgent({ message: "Delete large temporary files and downloads", session: existingSession});
Slow Performance
Problem: Sandbox running slowSolution: Check mode selection and resource usage
// Use appropriate mode for task complexityconst result = await agentbase.runAgent({ message: "Simple task", mode: "flash" // Don't use "max" for simple tasks});