12 November 2026
Code Mode و programmatic tool calling: خلّ الـ agent يكتب برنامج واحد بدل عشرات الرحلاتCode Mode and Programmatic Tool Calling: Let Your Agent Write One Program Instead of Dozens of Round Trips
Understand why calling MCP tools one at a time burns tokens, and try the new way: the agent writes one program that runs in a sandbox and returns only the result, with Cloudflare, the Claude API and Goose.
Who this guide is for
Anyone building agents or connecting Claude to MCP tools who feels tokens and time going into round trips.
Key terms
- MCP
- An open protocol that lets an agent discover tools and call them. Every tool's description goes into the context.
- Round trip
- Each time the agent calls a tool, the result goes back to the model, which then decides the next step. Every trip costs tokens and time.
- Code Mode
- Cloudflare's approach: tools become a TypeScript API, the model writes code that calls it, and the code runs in a sandbox that returns only the final result.
- Programmatic tool calling
- The same idea in the Claude API: Claude writes code inside code execution that calls your tools, and intermediate tool results stay out of its context.
- Sandbox
- A locked environment where code runs with no access to your files or the internet except through the tools you allow.
The steps
Step 1
Count your agent's round trips
Open the log of any long task your agent runs and count how many times it called a tool and went back to the model.
When steps depend on each other, like find the customer, then fetch their orders, then filter them, each step is a full trip, and its whole result lands in the context even when you need one line of it.
Here are the steps my agent runs for a task: [paste the steps or the log]. Count the tool calls, point out where intermediate results are large but only partly needed, and suggest which steps could be combined into one program.PromptAsk Claude to analyse the taskTip: Tasks that loop over many items or filter large data save the most.
Step 2
Try Code Mode with Cloudflare's server
Cloudflare published an MCP server that covers over 2,500 endpoints of its API with just two tools: search and execute. Per its blog, it uses around 1,000 tokens instead of 1.17 million if every endpoint were its own tool.
Add the server to any MCP client that supports remote servers at https://mcp.cloudflare.com/mcp. Sign in with OAuth and pick the permissions, or use an API token from your account.
Then give it a task on your account and watch it search the API and write code to run.
Use the search tool to find the endpoints that list the [domains / Workers] in my account, then write one program with the execute tool that returns a short table: [name, status, last modified]. Change nothing, read only.PromptA first read only requestTip: Start with read only permissions until you know what it does.
Step 3
Turn on programmatic tool calling in the Claude API
If you build your own agent on the Claude API, add the code execution tool at version code_execution_20260120 or later, and add the allowed_callers field to every tool you want callable from code.
Claude writes code that calls your tools inside the container. Intermediate results stay out of the context; only the code's final output comes back.
The allowed_callers field in a tool definition.Source: platform.claude.com { "name": "[tool_name]", "description": "[what the tool returns and in what shape]", "input_schema": {"type": "object", "properties": {"[param]": {"type": "string"}}, "required": ["[param]"]}, "allowed_callers": ["code_execution_20260120"] }PromptA tool callable from codeTip: Describe the exact shape of the data each tool returns, because the code depends on it.
Step 4
Or run Code Mode in Goose
Goose has a Code Mode: instead of calling extension tools directly, it writes JavaScript that calls them, and tools are discovered and loaded into context only when needed.
Open the Code Mode guide in the Goose docs, turn it on for your extensions, then run the same task and compare.
Tip: Run the same task twice, normal and Code Mode, and compare tokens and time.
Step 5
Write tool descriptions like API docs
In the new way the model writes code, so it needs field names, types and the shape of the response.
For each tool: one line on what it does, the inputs with types, and an example response. Models have read a lot of real code, so the closer your description is to API docs, the more accurate the code.
Here is my list of MCP tools with their descriptions: [paste the list]. Rewrite each description as short API docs: what it does, inputs and types, and the response shape with a small JSON example.PromptTurn your tools into clear docsStep 6
Measure before and after
Run the same task both ways and record three numbers: round trips, input tokens and time.
If the gap is small, your task is probably short and doesn't need the new way. The big win is in long tasks with many tools.
Compare these two runs of the same task: [normal run log] and [code run log]. Give me a table: tool calls, approximate tokens, time, and whether the result is the same.PromptComparison template
Ready prompts to copy
The takeaway
MCP stays. The way changed: let the model write one program, and let the sandbox hand back only the result.
Liked this guide?
Join Mohammad's community
72 guides like this one, and a new one every week. One subscription opens them all.
Approximate price in your currency. You pay in AED
- A permanent archive. Open any guide, any time
- 20% off private build sessions
- Cancel any time, no commitment
Or without a subscription
Most value
Start building with me now
We sit together, break down your problem, and I build the AI solution with you step by step
- A private session with you
- Real building during the session
- Results you take with you when we finish
100% free
A free consultation about your problem
