AgentCore Code Interpreter Service
Status: ✅ Available
Overview
Amazon Bedrock AgentCore Code Interpreter enables agents to securely execute code in isolated sandbox environments, supporting complex data analysis workflows and computational tasks.
Core Capabilities
Secure Execution
- Isolated Sandboxes: Each code execution runs in a completely isolated environment
- No Cross-Contamination: Sessions are independent with no shared state
- Enterprise Security: Meets enterprise security and compliance requirements
- Resource Controls: Configurable limits and timeout controls for execution
Framework Integration
- Popular Frameworks: Seamless integration with LangGraph, CrewAI, Strands, and other agent frameworks
- Multi-Language Support: Execute code in Python, JavaScript, and other languages
- Advanced Configuration: Extensive customization options for runtime environments
- Custom Runtimes: Support for specialized runtime configurations
Data Processing
- File Operations: Upload and download files for processing
- Multi-Modal Data: Handle structured and unstructured data
- Result Formatting: Format and visualize execution results
- Error Handling: Comprehensive error reporting and debugging support
Use Cases
Data Analysis and Transformation
Enable agents to:
- Process and analyze datasets
- Transform data formats
- Perform statistical calculations
- Generate data insights
Complex Computational Workflows
Support scenarios like:
- Running scientific computations
- Executing business logic calculations
- Processing batch operations
- Performing iterative algorithms
Visualization and Reporting
Allow agents to:
- Generate charts and graphs
- Create formatted reports
- Build visualizations from data
- Export results in various formats
Dynamic Code Testing
Enable agents to:
- Test code snippets dynamically
- Validate logic and algorithms
- Debug code execution issues
- Prototype solutions quickly
Architecture
Execution Flow
Agent Request
↓
┌─────────────────────────────────────────┐
│ Code Interpreter Service │
│ - Parse code execution request │
│ - Validate code and parameters │
│ - Allocate isolated sandbox │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ Sandbox Environment │
│ - Execute code in isolation │
│ - Process data and files │
│ - Generate outputs │
│ - Capture errors and logs │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ Result Processing │
│ - Format execution results │
│ - Package outputs and artifacts │
│ - Return to agent │
└─────────────────────────────────────────┘Security Model
- Sandbox Isolation: Each execution runs in a completely isolated environment
- Resource Limits: CPU, memory, and time limits prevent resource exhaustion
- Network Restrictions: Controlled network access from sandbox environments
- Data Encryption: Data at rest and in transit is encrypted
- Audit Logging: All code executions are logged for compliance
Configuration
Basic Setup
# Configure code interpreter for agent
aws bedrock-agentcore-control configure-code-interpreter \
--agent-id <AGENT_ID> \
--execution-timeout 300 \
--memory-limit 2048 \
--region <REGION>Custom Runtime Configuration
# Set custom runtime environment
aws bedrock-agentcore-control update-code-interpreter-runtime \
--agent-id <AGENT_ID> \
--runtime-config '{
"language": "python3.11",
"packages": ["pandas", "numpy", "matplotlib"],
"environment": {
"CUSTOM_VAR": "value"
}
}' \
--region <REGION>Best Practices
Code Security
- Validate all code inputs before execution
- Implement input sanitization for user-provided code
- Use resource limits to prevent denial of service
- Monitor code execution patterns for anomalies
Performance Optimization
- Cache common dependencies in runtime images
- Use appropriate timeout values for expected workload
- Optimize code for execution within timeout limits
- Batch similar operations when possible
Error Handling
- Implement comprehensive error catching in code
- Provide clear error messages for debugging
- Log execution details for troubleshooting
- Use structured output formats for results
Data Management
- Minimize data transfer in and out of sandboxes
- Use streaming for large data processing
- Clean up temporary files after execution
- Implement data validation before processing
Integration Patterns
With Memory Service
Code Interpreter ←→ Memory Service
- Store execution results in memory
- Retrieve past computation results
- Share data across agent sessionsWith Identity Service
Code Interpreter ←→ Identity Service
- Authenticate code execution requests
- Access credentials for external APIs
- Manage permissions for data accessWith Observability Service
Code Interpreter ←→ Observability Service
- Trace code execution workflows
- Monitor performance metrics
- Log execution events
- Alert on execution failuresTroubleshooting
Common Issues
Execution Timeout
- Symptom: Code execution exceeds timeout limit
- Solution: Increase timeout or optimize code performance
Memory Limit Exceeded
- Symptom: Code runs out of memory
- Solution: Increase memory limit or process data in chunks
Package Import Errors
- Symptom: Required packages not found
- Solution: Configure custom runtime with needed packages
Permission Denied
- Symptom: Cannot access required resources
- Solution: Configure IAM permissions for code interpreter
Monitoring
Key Metrics
- Execution Count: Number of code executions
- Success Rate: Percentage of successful executions
- Average Duration: Mean execution time
- Error Rate: Percentage of failed executions
- Resource Utilization: CPU and memory usage
CloudWatch Integration
# Query execution metrics
aws cloudwatch get-metric-statistics \
--namespace AWS/BedrockAgentCore/CodeInterpreter \
--metric-name ExecutionCount \
--dimensions Name=AgentId,Value=<AGENT_ID> \
--start-time <START> \
--end-time <END> \
--period 3600 \
--statistics SumAdditional Resources
- AWS Documentation: Bedrock AgentCore Code Interpreter
- Security Best Practices: Secure Code Execution
- API Reference: Code Interpreter API
Related Services:
- Runtime Service - Agent execution environment
- Memory Service - State management
- Observability Service - Monitoring and tracing