Programmatic Tool Calling

One program calls many tools; only its result reaches the model.

Tools are presented to the model as a code API instead of as individual tool definitions; the model writes one program that calls several tools inside a sandbox, and only what the program logs or returns enters the model's context.

In practiceAn agent asked to attach a meeting transcript from a document store to a CRM record writes one script that fetches the transcript and updates the record; the transcript never passes through the model.

Level
04 · Production
Subdomain
Tool Integration
Also known as
Code Mode, Code Execution with MCP, Code-based Tool Calling
Trade-off
Fewer context tokens and fewer inference passes are gained against the cost and risk of operating a secure code-execution environment.
Native in
Claude Platform, Cloudflare Agents SDK
The mechanism

How it works.

The tools stay where they are; what changes is how the model reaches them. It no longer emits one call at a time but writes a program, and the harness runs that program somewhere safe.

1 · Set up, once

  1. Tool definitionsFrom the MCP servers, as before
  2. Generate a code APIOne function per tool
  3. One tool for the model“Run this code”

2 · Every task

  1. The model writes a programSeveral tool calls, loops, filters
  2. It runs in the sandboxIsolated, with limits
  3. Calls go through the harnessOn to the servers that own the tools
  4. Only the result returnsWhat the program logs or returns
Code API
The tool definitions as functions or files. The model reads only the ones it needs, instead of having all of them in its context.
Sandbox
The isolated environment the program runs in. It reaches tools only through the harness, not the open network.
Returned output
Whatever the program logs or returns. Everything else, including intermediate results, stays outside the context.
Walk it through

A real run, step by step.

TaskAnthropic's example: an agent copies the transcript of a sales meeting from Google Drive into a Salesforce record.
The totals are the ones Anthropic states for this example (2025), not an independent measurement.
StageDirect tool callsAs a program
Tool definitionsAll of them in the context, up frontTwo files read, on demand
Fetch the transcriptThe whole transcript enters the context as a tool resultIt lands in a variable in the sandbox
Update the recordThe model writes the transcript out again as an argumentThe variable is passed on
In total150,000 tokens2,000 tokens
What this shows: the saving comes from two places, the tool definitions that are never loaded and the transcript that never passes through the model. It is bought with a sandbox that has to be operated and secured, and the model no longer sees the transcript it moved.
In code

The pattern in code.

TypeScripttypescript
import * as gdrive from "./servers/google-drive";
import * as salesforce from "./servers/salesforce";

// The transcript lives in a variable. It never enters the model's context.
const transcript = (await gdrive.getDocument({ documentId: "abc123" })).content;

await salesforce.updateRecord({
  objectType: "SalesMeeting",
  recordId: "00Q5f000001abcXYZ",
  data: { Notes: transcript },
});

console.log("Transcript attached.");  // the only line the model sees
Fit

Use it, or not.

When to reach for it

  • The agent has so many tools that their definitions crowd the context window.
  • Intermediate results are large and only a filtered part matters.
  • A task chains several tool calls with loops or conditions.

When it backfires

  • The task needs one or two direct tool calls.
  • No sandbox with resource limits and monitoring is available.
  • The model has to judge each intermediate result before the next call.
Pitfalls

Two ways this pattern will hurt you.

Generated code reaches further than a single tool call

A tool call does one thing. A program can loop, combine tools and move data from one system to another, so an injected instruction has more to work with.

Countermeasure

Run the program in a sandbox without general network access, expose only the tools the task needs, and set limits on time and resources.

The model no longer sees what passes between the tools

Intermediate results stay in the sandbox. A wrong record, an error message or an instruction hidden in a tool result is processed by the program and never read by the model.

Countermeasure

Validate in code what the next step depends on, log what the model has to judge, and record every tool call at the harness.

Framework support

Where Programmatic Tool Calling is native.

Claude PlatformServiceProgrammatic Tool CallingNative
Cloudflare Agents SDKCode ModeNative

Search

Search patterns, frameworks, and pages.