DocsCode Mode

Code Mode

Connect tool calls with a small Python program. Keep intermediate results close, then return the observations the agent needs.

GUIDEENABLED BY DEFAULT

How a turn works

  1. The decision model selects execute_code or finish using the latest result.
  2. The executor writes the selected Python program or final answer.
  3. Monty validates the program’s syntax and types, then runs it with an explicit tool bridge.
  4. Tool results and the program’s output return to the agent for the next turn.

On a routing fallback, the executor can choose between both tools. The activity panel shows nested tool calls; failed actions expand to show their errors.

Write a tool program

This is the kind of program the executor sends as the raw code string of an execute_code call:

Python · Code Mode program
listing = await call_tool("list_files", {"path": "src"})
results = []
for path in listing["output"]["files"][:3]:
    result = await call_tool("read_file", {"path": path})
    results.append(result["output"])
results

Use top-level await call_tool(name, arguments) for each tool. Tool names and arguments must match the current catalog; tools are not Python functions. Use Python True, False, and None inside the program.

The final expression and printed output return to the model. Variables stay within one program and do not persist between turns. No asyncio.run wrapper is needed.

Read a tool response

Chat tool replies and call_tool share four fields:

JSON · successful tool response
{
  "ok": true,
  "output": {"content": "1: example text", "has_more": false},
  "error": null,
  "truncated": false
}
Response fields
FieldMeaning
okWhether the tool succeeded.
outputNative JSON data or plain text. Do not JSON-decode it a second time.
errornull on success; otherwise code, message, and a nullable retry_hint.
truncatedWhether output was shortened. Check it before relying on the result.

read_file text is in result["output"]["content"] and includes display line numbers. Strip those prefixes from exact edit patches, and check has_more for additional content.

MCP replies preserve typed content blocks and a separate structured_content value. Unsupported media is represented by an omitted-block marker.

Execution and limits

Programs run in a Monty sandbox without direct host filesystem or network access. Tool calls pass through schema validation, middleware, workspace restrictions, and normal approvals.

Default program limits
ResourceLimit
Tool calls32 per program
Computation5 seconds, excluding tools and approval waits
Memory64 MiB
OutputBounded; inspect truncation metadata

Use --read-only to disable mutating tools. --yes automatically approves actions, MCP connections, and hooks; use it only in trusted workspaces. Press Esc to cancel a running program.

Recover from an error

Missing awaits, undefined names, invalid bridge arguments, and unsupported imports are rejected before tools run. The error includes a phase, a tool log, and a retry_hint.

  • Validation failure: correct the program and submit it again.
  • Runtime or tool failure: inspect the tool log and current workspace state before retrying. A failed tool raises and prevents later calls in that program from running.
  • Partial output: failed commands can retain useful structured output alongside their error.

Actions are not rolled back. Tool calls that succeeded before a runtime failure may already have changed the workspace. Check those changes before repeating the program.