# Programmatic tool calling

> Programmatic tool calling lets a model write code that calls your tools, so intermediate results stay out of context. How it works, vendor docs and tradeoffs.

Source: https://www.honeyhive.ai/learn/programmatic-tool-calling (updated 2026-11-23)

Programmatic tool calling is a pattern where the model writes a short program that calls your tools, rather than asking for one tool per turn. Anthropic's [documentation](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) describes Claude writing code that calls your tools inside a code execution container, which cuts model round trips and lets Claude filter or process data before it reaches the context window. It builds on ordinary [function calling](/learn/function-calling-llm) and is one way to practice [context engineering](/learn/context-engineering).

## Why it exists

In an engineering post from November 2025, Bin Wu and the Claude Developer Platform team [named](https://www.anthropic.com/engineering/advanced-tool-use) two problems with plain tool calling: intermediate results consume large token budgets, and every call needs a full model inference pass. Anthropic's docs [give an example](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling): checking budget compliance for 20 employees takes 20 model round trips the traditional way, with thousands of expense line items pulled into context. With a script, one run does all 20 lookups, filters them and returns only the employees over their limit.

## How it works on Claude

Anthropic [lists](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) the steps:

1. Claude writes Python that calls the tool as a function, with any preprocessing and post-processing.
2. The code runs in a sandboxed container through code execution.
3. When the code calls a tool, execution pauses and the API returns a `tool_use` block.
4. Your application supplies the result and the code continues. Intermediate results are not loaded into Claude's context window.
5. When the code finishes, Claude receives the final output.

You opt a tool in with an `allowed_callers` field on its definition, and the code execution tool must be present. Anthropic [adds](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) that `allowed_callers` is not a hard block on direct calls, so your client should still handle them. Each `tool_use` block carries a `caller` field that says whether the call was direct or came from code.

## What the numbers say

All figures are Anthropic's own and depend on the task.

- The engineering post [reports](https://www.anthropic.com/engineering/advanced-tool-use) average token use on complex research tasks falling from 43,588 to 27,297, a 37% reduction. It says orchestrating 20 or more calls in one code block removes 19 or more inference passes.
- The docs [say](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) that on BrowseComp and DeepSearchQA, adding programmatic tool calling to basic search tools improved performance by an average of 11 percent while using 24 percent fewer input tokens.

## When it helps and when it does not

The engineering post [says](https://www.anthropic.com/engineering/advanced-tool-use) it fits large datasets where only aggregates matter, workflows with three or more dependent calls and parallel work across many items. It is less useful for a single simple call, or when the model needs to reason over every intermediate result, because the model never sees those results. Anthropic's docs [add](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) that on τ²-bench, where each turn makes one or two sequential tool calls, it left scores unchanged and cost roughly 8 percent more.

## Other vendors

OpenAI [documents](https://developers.openai.com/api/docs/guides/tools-programmatic-tool-calling) the same idea under the same name, with different details.

| | Anthropic | OpenAI |
|---|---|---|
| Language the model writes | [Python](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) | [JavaScript](https://developers.openai.com/api/docs/guides/tools-programmatic-tool-calling) |
| Where code runs | Sandboxed code execution container | Fresh, isolated V8 runtime |
| Opt in | `allowed_callers` naming a code execution version | A `programmatic_tool_calling` tool in the request, and `allowed_callers` including `programmatic` |
| Reach outside | Only through your tools, which pause the code and return to your app | Only through tools enabled in the request |

Anthropic's docs [list](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) tools that cannot be called this way, including MCP connector tools and the computer use and browser use toolsets. OpenAI [says](https://developers.openai.com/api/docs/guides/tools-programmatic-tool-calling) MCP tools can be called from programs, and that its runtime has no Node.js, package installation, subprocesses or state between executions.

## Tradeoffs

- **Sandboxing.** Generated code is code you did not write. Both vendors run it in a hosted sandbox. See [AI agent sandboxing](/learn/ai-agent-sandboxing). The sandbox does not cover your own tools, so OpenAI [advises](https://developers.openai.com/api/docs/guides/tools-programmatic-tool-calling) checking arguments and permissions for each call and requiring application-level approval for high-impact actions regardless of the caller.
- **Fewer visible steps.** Several tool calls now happen inside one model turn, so turn-level records show less. OpenAI [recommends](https://developers.openai.com/api/docs/guides/tools-programmatic-tool-calling) measuring token use, latency, cost, model turns, tool calls, retries and whether the intended route ran, and its `program` items carry a call ID for inspection. See [LLM tracing](/learn/llm-tracing) and [agent tracing](/learn/agent-tracing) for what to capture.
- **Timeouts and retention.** Anthropic's docs [say](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) a pending tool call times out after about 4 minutes and raises a `TimeoutError` inside the code. The feature is not eligible for zero data retention, and data is retained up to 30 days.
- **Feature gaps.** Anthropic [states](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) that `strict: true` tools and `disable_parallel_tool_use` are not supported, and that `tool_choice` cannot force a programmatic call.

Anthropic's docs [list](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) the supported Claude models, going back to Claude Opus 4.5 and Sonnet 4.5.

## FAQ

### What is programmatic tool calling?

It is a pattern where the model writes code that calls tools, instead of making one tool call per turn. Anthropic's [docs](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) describe Claude writing code that calls your tools inside a code execution container, so it can filter data before it reaches the context window.

### Does programmatic tool calling keep tool results out of the context window?

Intermediate ones, yes. Anthropic [says](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) intermediate results are not loaded into Claude's context window, and the model receives the final output of the code.

### Which providers offer it?

Anthropic offers it with Claude through its code execution tool, using Python. OpenAI [documents](https://developers.openai.com/api/docs/guides/tools-programmatic-tool-calling) a version in which the model writes JavaScript that runs in an isolated V8 runtime.

### Is programmatic tool calling safe by default?

The code runs in a sandbox, but your tools still run in your application. OpenAI [advises](https://developers.openai.com/api/docs/guides/tools-programmatic-tool-calling) checking arguments and permissions for each call, even when it comes from a hosted program.
