Claude Platform Docs
MessagesTool infrastructure

Programmatic tool calling

Let Claude call your tools from code in the code execution container, cutting model round trips and token use in multi-tool workflows.

Programmatic tool calling allows Claude to write code that calls your tools programmatically within a code execution container, rather than requiring round trips through the model for each tool invocation. This reduces latency for multi-tool workflows and decreases token consumption by allowing Claude to filter or process data before it reaches the model's context window. On agentic search benchmarks like BrowseComp and DeepSearchQA, which test multistep web research and complex information retrieval, adding programmatic tool calling on top of basic search tools improved performance by an average of 11% while using 24% fewer input tokens (see Improved web search with dynamic filtering).

Consider checking budget compliance across 20 employees: the traditional approach requires 20 separate model round-trips, pulling thousands of expense line items into the context along the way. With programmatic tool calling, a single script runs all 20 lookups, filters the results, and returns only the employees who exceeded their limits, shrinking what Claude needs to reason over from hundreds of kilobytes down to a handful of lines.

Programmatic tool calling requires the code execution tool with tool version code_execution_20260120 or later.

Quick start

Here's an example where Claude programmatically queries a database multiple times and aggregates results. Adding allowed_callers: ["code_execution_20260120"] to a tool definition is what makes that tool callable from within code execution (see The allowed_callers field):

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-5",
    max_tokens=4096,
    messages=[
        {
            "role": "user",
            "content": "Query sales data for the West, East, and Central regions, then tell me which region had the highest revenue",
        }
    ],
    tools=[
        {"type": "code_execution_20260120", "name": "code_execution"},
        {
            "name": "query_database",
            "description": "Execute a SQL query against the sales database. Returns a list of rows as JSON objects.",
            "input_schema": {
                "type": "object",
                "properties": {
                    "sql": {"type": "string", "description": "SQL query to execute"}
                },
                "required": ["sql"],
            },
            "allowed_callers": ["code_execution_20260120"],
        },
    ],
)

print(response)

The response stops with stop_reason: "tool_use", a container ID, and a tool_use block for query_database whose caller field identifies the code execution run that called it. Return the result as shown in Step 3 of the example workflow so the code can finish.

How programmatic tool calling works

When you configure a tool to be callable from code execution and Claude determines that tool is needed:

  1. Claude writes Python code that invokes the tool as a function, potentially including multiple tool calls and pre/post-processing logic
  2. Claude runs this code in a sandboxed container through code execution
  3. When a tool function is called, code execution pauses and the API returns a tool_use block
  4. You provide the tool result, and code execution continues (intermediate results are not loaded into Claude's context window)
  5. Once all code execution completes, Claude receives the final output and continues working on the task

This approach is particularly useful for: