Vercel's AI SDK Ships Code Mode, Cutting Agent Token Use by 99.9%

Vercel's AI SDK adds an experimental Code Mode tool that lets models write sandboxed JS or TS to orchestrate tool calls in a single shot.

·
·
Vercel's AI SDK Ships Code Mode, Cutting Agent Token Use by 99.9%
AuthorAI SDK
Read2 min
  • AI SDK adds experimental Code Mode, letting models write JS/TS that calls tools inside a QuickJS sandbox.
  • One generated program can Promise.all, branch, and transform, replacing multiple tool-call round-trips.
  • Shipped as @ai-sdk/code-mode; requires Node.js 22, not for browser or edge runtimes.
  • New experimental_toolCallers API controls which tools are reachable via code versus directly.
  • Sandbox blocks process, fetch, filesystem, and eval; every capability must be exposed as a tool.
  • Follows Cloudflare's Code Mode pattern, which cut MCP token usage by up to 99.9%.

Vercel's AI SDK just picked up an experimental feature that changes how models drive tool use. Instead of the usual back-and-forth where a model emits one JSON tool call, waits for the result, then emits the next, Code Mode lets the model write a small JavaScript or TypeScript program that calls your tools directly. The generated code runs in an isolated QuickJS sandbox and returns a JSON-serializable result.

The pattern itself is not new. It traces back to the CodeACT idea and was popularized by Cloudflare's Code Mode, which argued that LLMs are better at writing code to call MCP, than at calling MCP directly. What is new is that this pattern is now a first-class primitive inside one of the most widely used TypeScript agent SDKs.

Why chaining tools as code beats chaining them as JSON

In classic tool calling, every intermediate result has to travel through the model. With the traditional approach, the output of each tool call must feed into the LLM's neural network, just to be copied over to the inputs of the next call, wasting time, energy, and tokens. Code Mode collapses that loop: the model writes the plan once, the sandbox executes it, and only the final answer comes back.

Cloudflare's follow-up work found this can be dramatic at scale. In their MCP experiment exposing the full Cloudflare API, Code Mode reduces the number of input tokens used by 99.9%. An equivalent MCP server without Code Mode would consume 1.17 million tokens , more than the entire context window of the most advanced foundation models.

Keep reading

Don't miss what's next in AI

Join 300,000+ engineers and researchers who get the signal, not the noise. Create a free account to read the rest of this story.

  • Full access to in-depth AI research breakdowns
  • Be the first to know what's trending before it hits mainstream
  • Daily curated papers, repos, and industry moves