Code Mode Explained: How AI Agents Use Sandbox Code to Reduce Repeated Tool Calls
Key point
The post explains how Code Mode lets AI models run programs against tools in a sandbox, reducing repeated tool-call context for tasks with many calls.
Details
- Code Mode replaces most repeated model-facing tool-call turns with a program the model writes and runs in a sandbox; only the required results return to the model.
- Cloudflare’s
@cloudflare/codemodeexposes tools through a TypeScript API; the program calls them from a Worker while their results stay inside the sandbox. - In the author’s test, a program made 20 host calls to read GitHub issues in 6 ms and returned two issues to the model.
- The author’s token analysis finds that Code Mode can reduce context overhead as tasks chain more calls, but direct calls can be cheaper for short tasks.
How it works
- Cloudflare’s
search()andexecute()design lets an agent look up relevant parts of an API specification before running code, rather than loading the whole specification into context. - Anthropic offers a similar approach through programmatic tool calling, where Python code calls tools and only the final output reaches the model.
Costs and limits
- On Anthropic’s 75-tool project-management benchmark, programmatic tool calling cut billed input tokens by about 38 percent with no change in accuracy; on τ²-bench, scores were unchanged and cost was about 8 percent higher.
- In the Cloudflare design described, credentials remain on the host and the sandbox has no network access by default, but the author found that a synchronous infinite loop could evade the executor’s timer unless the runtime enforces a CPU limit.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.