All Guides
GuideAI DevelopmentNovember 28, 2026

Why Agents Should Write Code, Not Call Tools

Take this guide with you.

Drop your email and we'll send the Why Agents Should Write Code, Not Call Tools straight to your inbox.

No spam. Just the guide. Unsubscribe anytime.

Why Agents Should Write Code, Not Call Tools

A field guide to Code Mode: why standard MCP tool definitions and round trips eat context, how exposing tools as a typed TypeScript API with search and execute cuts usage by ~99.9%, and when it's worth switching.

What's inside
  • Why every MCP tool definition costs context
  • 1.17M tokens just to describe 2,500 tools
  • Code Mode: a typed API plus search and execute
  • One script in a sandbox — about 1,000 tokens
  • Best for 50+ tools and chained workflows

The Hidden Token Tax on MCP

Expose an API over MCP the usual way and every operation becomes a tool definition, with a name, description, parameters and schema, all loaded into context. At scale that explodes. Cloudflare found that describing its 2,500-endpoint API took 1.17 million tokens, more than most context windows. Code Mode is its fix.


Why Standard Tool Calling Gets Expensive

  • The manifest: every tool's definition sits in context before the agent does any work.
  • The round trips: each tool call goes back to the model before the next one, and the back-and-forth adds up fast.

Code Mode: Let the Model Write the Loop

Instead of hundreds of tool definitions, Code Mode exposes the tools as a typed TypeScript API and gives the model just two tools: search and execute. The model writes one script that chains everything together and runs it in a sandbox, so the loops, filters and joins never touch the context.

The same job takes about 1,000 tokens, a 99.9% cut. The same idea already ships as Anthropic's Programmatic Tool Calling and in Block's Goose.


When to Use It

This isn't "MCP is dead"; it's still MCP underneath. Code Mode pays off with big tool sets (50+) and chained workflows. With fewer than about 10 tools, plain tool calls are simpler. Also note that review happens on the generated code, not on each individual call.


What's in the Guide

Where MCP's token cost comes from, how Code Mode turns tools into a typed API the model scripts against, the 1.17M-to-1,000 token comparison, when it's worth switching, and a link to Cloudflare's write-up.

The full field guide is in the PDF.


Get the Guide

Drop your email below and we'll send it straight to your inbox.

Don't leave without it.

Drop your email and get the Why Agents Should Write Code, Not Call Tools in your inbox.

No spam. Just the guide. Unsubscribe anytime.

Build Something Real

If you can describe it, you can build it.