Skip to main content

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
Monty makes this safe by providing a minimal, secure Python interpreter that:
  • 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

1

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.
2

Type Check

Monty validates the generated code against type stubs you provide, catching errors before execution.
3

Safe Execution

The code runs in Monty’s sandboxed interpreter. When it calls external functions, Monty pauses and hands control back to you.
4

Resume & Return

You execute the function call in your host environment and resume Monty with the result. This repeats until the code completes.

Example: Iterative Execution

import pydantic_monty

code = """
data = fetch(url)
len(data)
"""

m = pydantic_monty.Monty(code, inputs=['url'])

# Start execution - pauses when fetch() is called
result = m.start(inputs={'url': 'https://example.com'})

print(type(result))
#> <class 'pydantic_monty.FunctionSnapshot'>
print(result.function_name)  # fetch
#> fetch
print(result.args)
#> ('https://example.com',)

# Perform the actual fetch, then resume with the result
result = result.resume(return_value='hello world')

print(type(result))
#> <class 'pydantic_monty.MontyComplete'>
print(result.output)
#> 11

Example: Async External Functions

For async functions, use run_monty_async() which handles the pause/resume loop automatically:
import pydantic_monty

code = """
result = await fetch_data(url)
result['count']
"""

m = pydantic_monty.Monty(code, inputs=['url'])

async def fetch_data(url: str) -> dict:
    # Your async logic here
    return {'count': 42}

output = await pydantic_monty.run_monty_async(
    m,
    inputs={'url': 'https://api.example.com'},
    external_functions={'fetch_data': fetch_data}
)
print(output)  # 42

Learn More

For deeper context on why this pattern is valuable, see:
Monty is designed for this single use case: running code written by agents safely and efficiently.

Next Steps

Explore the example implementations:

Build docs developers (and LLMs) love