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