Thanks to visit codestin.com
Credit goes to trueforge.dev

Skip to main content

Why Code Mode?

When an agent uses tools the standard way, every tool call is a separate round-trip with the model: the agent calls one MCP tool, the full JSON response enters the conversation, the model reasons over that JSON, then it calls the next tool — copying IDs and computing counts in prose along the way. For tasks that involve aggregating tool output, or chaining several calls together, this approach is slow, fills the context window with intermediate JSON the user doesn’t care about, and is prone to small errors like miscounts and typoed IDs. Take a common task: “How many open PRs does each contributor have on this repo?” The user only cares about a small summary table, but the underlying GitHub tool returns a full record per PR — title, labels, reviewers, timestamps, and more. Without Code Mode, every one of those records lands in context and the model has to count from prose. With Code Mode, the agent calls the same tool inside a script, counts the author logins in code, and prints only the table.

What is Code Mode?

Code Mode collapses tool round-trips into a single script. Using the sandbox, the agent writes Python that calls MCP tools through an in-sandbox MCP client (mcp_client), processes the responses in code, and prints only what the user actually needs:
The script runs in the sandbox, but the MCP tool calls are bridged back to the harness, which applies the stored server credentials — the sandbox never holds tokens: Code Mode is available whenever the agent’s sandbox is enabled. The agent decides at runtime whether a task is worth running in code or whether a single direct tool call is enough. Before writing a script, it can call get_tool_output_schema so it knows the exact shape of a tool’s response instead of guessing keys from raw JSON.
Approval policies still apply in Code Mode — a script that calls a tool matching require_approval_for_tools pauses for user approval just like a direct tool call.

When the agent uses Code Mode

Code Mode wins whenever running the work in code is materially better than reasoning over raw JSON in chat:

Aggregate or format tool output

Counts, group-bys, sums, filters, or formatted tables over a tool response.

Chain tool calls

One tool’s output feeds another tool’s input — for example, resolving an entity’s ID before fetching its details.
Only the printed lines enter the agent’s context — the two full JSON payloads never do.