85 agentic tools audited for security incidents · Talk to the lab →
MotionticBook a call
Glossary

What is programmatic tool calling?

Programmatic tool calling is a technique in which a model writes code that calls tools inside a sandbox, so loops, filtering and intermediate results stay in the code environment and only the final output enters the model's context.

In ordinary tool calling, every result passes through the model. Ten lookups mean ten round trips, and every row of every response lands in the context window whether the model needs it or not. Programmatic tool calling moves that work into code: the model writes a short program that calls the tools, loops over results, filters and aggregates them, and hands back only the answer.

Anthropic described the idea in "Code execution with MCP" (November 2025), where presenting MCP servers as code APIs cut one example from about 150,000 tokens to about 2,000, and shipped it as an API feature in "Introducing advanced tool use" later that month.

How it works

  • Opt tools in

    The developer adds a code execution tool and marks which of its own tools may be called from code, using the allowed_callers field.

  • Model writes code

    The model writes Python that calls those tools as async functions, with loops, conditions and data handling around the calls.

  • Code pauses for each call

    When the script calls a tool, execution pauses and the API returns the call to the application, which runs it and sends the result back.

  • Results stay in the sandbox

    Intermediate results go to the running code, not into the model's context, and do not count toward token usage.

  • Final output returns

    When the script finishes, only its output reaches the model, which carries on with the task.

What the numbers say

Anthropic's launch post reported average usage falling from 43,588 to 27,297 tokens, a 37% reduction, on complex research tasks, and an expense example where about 200KB of raw data was reduced to 1KB of results.

The documentation adds a sharper picture of where it pays. On a 75-tool project management benchmark, billed input tokens fell by roughly 38% with no change in accuracy. On τ²-bench, where each turn makes one or two sequential calls, scores were unchanged and cost was roughly 8% higher. The gain comes from many calls and large results, not from every workload.

When it fits and when it does not

  • Fits

    Large data that must be filtered or aggregated, many calls in a loop, and decisions that depend on intermediate results.

  • Does not fit

    Turns with one or two sequential calls, where the extra code step adds cost without saving round trips.

  • Restrictions

    Anthropic's docs list tools with strict schemas, tools from its MCP connector and the computer use toolsets among those that cannot be called this way.

  • Security

    The docs warn that allowed_callers is not a security boundary, and that tool results interpreted as code carry injection risk, so results need validating.

How Motiontic's engineers apply it

Motiontic's engineers reach for programmatic calling where an agent works over bulk data, such as reconciling records or scanning logs, and measure it against direct calling on the client's own eval set before keeping it. The sandbox it runs in gets the same file system and network limits as any other code the agent writes.

Direct tool calling compared with programmatic tool calling

Direct tool calling compared with programmatic tool calling
Direct tool callingProgrammatic tool calling
Who sequences the callsThe model, one turn at a timeCode the model writes
Where intermediate results goInto the context windowInto the running script
Model round tripsOne per call or batchOne per script
FitsFew calls with small resultsMany calls, loops and large data
NeedsTool definitionsTool definitions plus a code sandbox

Common questions

Is programmatic tool calling the same as code execution with MCP?

They share the idea. Code execution with MCP describes presenting MCP servers to an agent as code APIs in a sandbox; programmatic tool calling is the API feature that makes a developer's own tools callable from the model's code.

Does programmatic tool calling reduce cost?

Often, when an agent makes many calls or handles large results. Anthropic's docs report about 38% fewer input tokens on a 75-tool benchmark, but about 8% higher cost on workloads with one or two sequential calls per turn.

Is programmatic tool calling safe?

It runs in a sandboxed container, but the generated code still calls real tools. Results should be validated, and the sandbox should have its own network and file system limits.

Running agents that have to hold up?

Describe the agent, the systems it touches and what it must never get wrong. The first call is with a lab engineer, who will say plainly whether it is ready for production, and whether Motiontic is the right team.