Overview
This example demonstrates how to build a sophisticated AI agent workflow using LangGraph and OpenSandbox. The integration creates a state machine that orchestrates sandbox operations, including creation, file operations, command execution, error handling with retries, and AI-powered result summarization.Prerequisites
- OpenSandbox server running locally or remotely
- Docker with the code-interpreter image
- Anthropic API credentials
- Python with
uvpackage manager
Setup
1. Pull the Code Interpreter Image
2. Start OpenSandbox Server
3. Install Dependencies
Workflow Architecture
The LangGraph workflow implements a state machine with the following nodes:Implementation
State Definition
First, define the workflow state that will be passed between nodes:Node Implementations
Create Sandbox Node
Prepare Workspace Node
Run Job Node with Retry Logic
Decision Node
Inspect Results with AI
Cleanup Node
Building the Graph
Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
SANDBOX_DOMAIN | No | localhost:8080 | Sandbox service address |
SANDBOX_API_KEY | No | - | API key for authentication |
SANDBOX_IMAGE | No | opensandbox/code-interpreter:v1.0.1 | Docker image |
ANTHROPIC_API_KEY | Yes* | - | Anthropic API key |
ANTHROPIC_AUTH_TOKEN | Yes* | - | Anthropic auth token |
ANTHROPIC_BASE_URL | No | - | Custom API endpoint |
ANTHROPIC_MODEL | No | claude-3-5-sonnet-latest | Model name |
ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN, not both.
Running the Example
Key Features
State Machine Workflow
- Graph-based execution flow with conditional branching
- Automatic retry logic with fallback commands
- Clean state management across nodes
Error Handling
- Automatic retry on command failures
- Fallback command execution (e.g.,
python3→python) - Guaranteed cleanup even on failures
AI Integration
- Claude-powered result summarization
- Context-aware analysis of execution outputs
- Seamless LangChain integration
Resource Management
- Proper sandbox lifecycle management
- Cleanup guarantees with try/finally patterns
- Async operations for efficiency
Use Cases
- Multi-step AI Workflows: Chain multiple AI operations with state
- Code Execution Pipelines: Run, test, and analyze code automatically
- Error Recovery: Implement sophisticated retry and fallback logic
- Result Analysis: Use AI to summarize and interpret execution results
- CI/CD Integration: Build automated testing and deployment workflows