When to Use Code Execution Tools
Use these tools when your agent needs to run dynamic code snippets or scripts.Quick Start
Typical Workflows
- Data Processing: Read a file, execute a Python script to analyze it, and write the results.
- Code Generation & Testing: Have an LLM generate code, execute it in a sandbox to verify correctness, and report back.
- DevOps Automation: Run shell scripts to scaffold projects, lint files, or run tests.
Choosing the Right Executor
| Executor | Use Case | Key Benefit |
|---|---|---|
BuiltInCodeExecutor | Untrusted Code (from LLMs, users) | Security & Isolation |
UnsafeLocalCodeExecutor | Trusted Code (your own scripts) | Performance & Speed |
- Prefer
BuiltInCodeExecutor(Sandboxed) when security is paramount. It isolates execution, allows package installation, and provides a controlled environment. - Use
UnsafeLocalCodeExecutor(Local) for trusted scripts in development or controlled CI environments where you need maximum performance and direct access to local files.
Core Features & Configuration
Both executors share a common API but have different backends.Configuration
- Timeout: Set
timeoutMsto prevent runaway scripts. - Environment Variables: Pass
envsto inject secrets or configuration. - Streaming Output: Use
onStdoutandonStderrfor real-time feedback from long-running tasks.
Supported Languages
Execute code in multiple languages and get structured results:Workflow Example: Agent Integration
Combine code execution with other tools like theFilesystemToolkit to build powerful agents.