What is Agent Code Mode?
Agent Code Mode is a pattern where LLMs write Python code to accomplish tasks instead of making sequential tool calls. The LLM generates code that calls your tools as functions, and Monty executes it safely in a sandboxed environment.This approach is faster, cheaper, and more reliable than traditional tool calling because:
- The LLM can express complex logic in code instead of chained tool calls
- Multiple operations can run in parallel with
asyncio.gather - Intermediate results stay in the sandbox, reducing token usage
- Type checking catches errors before execution
Why Code Mode?
Instead of asking an LLM to make 50 sequential tool calls to process a list, you can ask it to write a loop. The LLM naturally expresses:- Loops and iteration
- Conditional logic
- Data transformations
- Parallel execution
- Error handling
- Blocks filesystem, network, and environment access
- Only allows calling functions you explicitly provide
- Enforces resource limits (memory, time, stack depth)
- Starts in microseconds, not milliseconds
How It Works
LLM Generates Code
Ask the LLM to write Python code that uses your tools as functions. The code can include loops, conditionals, async/await, and more.
Type Check
Monty validates the generated code against type stubs you provide, catching errors before execution.
Safe Execution
The code runs in Monty’s sandboxed interpreter. When it calls external functions, Monty pauses and hands control back to you.
Example: Iterative Execution
Example: Async External Functions
For async functions, userun_monty_async() which handles the pause/resume loop automatically:
Learn More
For deeper context on why this pattern is valuable, see:- Codemode from Cloudflare
- Programmatic Tool Calling from Anthropic
- Code Execution with MCP from Anthropic
- Smol Agents from Hugging Face
Monty is designed for this single use case: running code written by agents safely and efficiently.
Next Steps
Explore the example implementations:- Web Scraper - Extract structured data from websites
- Data Analysis - Analyze expenses with async tool calls
- SQL Playground - Query CSV files and analyze sentiment
