الانتقال إلى المحتوى
أساسيات الـ AIAI Agents· 8 دقائق للقراءة

The Model Doesn't Run Your Function: How Tool Calling Really Works

The model only writes a request. Your code runs it, checks it and sends the result back, one round trip at a time.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A support bot gets a refund_order tool. In the demo, the customer writes "Refund order 1042, please" and the bot answers "I'll refund order 1042 now." Two days later, finance asks why the money never reached the card. The logs show nothing, because the code printed the model's text and returned. The refund never ran. The model can't run it: it had asked the application to, and nobody answered.

The common misconception

"With function calling, the LLM executes my code or calls my API by itself." It's an easy picture to form:

  • The names suggest it. "Function calling" and "tool use" sound like the model is the one calling.
  • Chat products seem to act: assistants search the web, run code and edit files, and you never see who did the work.
  • SDK helpers hide the loop. Anthropic's SDKs include a tool runner, in beta, that runs your functions when Claude calls them, so the round trips are invisible.
  • Some tools really do run without your code: Anthropic runs server tools such as web search and code execution on its own infrastructure.

The half-truth: tools do let a model cause actions in the world. But the model's only output is tokens. Some program, yours or Anthropic's, reads the request, runs the operation and feeds the result back. The model never touches your database.

What actually happens

The model writes a request and stops

Anthropic's docs describe as a contract: you specify which operations exist and what shape their inputs take, and Claude decides when to call them. In their words, "The model never runs anything on its own." When Claude wants your tool, the response has stop_reason: "tool_use" and a tool_use block with an id, the tool's name and an input object that follows your schema. That's the entire action on the model's side: a JSON request inside a reply. As the Understanding LLMs path showed, a model predicts tokens; a tool call is tokens in a structured shape.

Your code runs it and reports back

For tools you define, the docs spell out the loop:

  1. Send a request with your tools and the user's message.
  2. Claude responds with stop_reason: "tool_use" and one or more tool_use blocks.
  3. Run each tool and format the outputs as tool_result blocks.
  4. Send a new request with the earlier messages, the assistant's response and a user message holding the results.
  5. Repeat while stop_reason is "tool_use".

Each tool_result carries the tool_use_id it answers. The results must come right after the assistant message that asked for them, and before any text in that user message. When Claude calls several tools at once, all the results go back together in one user message. And since the API is stateless, each round trip resends the whole conversation.

Research systems work the same way

This isn't an API quirk. Toolformer, a 2023 model from Meta AI trained to insert API calls into its own text, still needed the decoding to stop: when the model produced the token meaning "the API's response goes here", generation was interrupted, the API was called, and decoding continued with the response inserted. ReAct prompted models to interleave reasoning with actions such as search[entity] against a simple Wikipedia API, and the environment's observation was added before the next step. In both, the model proposes and a harness executes.

Server tools: someone else runs the loop

For web_search, web_fetch, code_execution and tool_search, Anthropic's servers run the operation and feed the output back to the model before the response reaches you. That's where "the model ran a search" comes from: the same contract, with Anthropic's code as the executor. That loop has an iteration limit, and when it's reached the response ends with stop_reason: "pause_turn"; you re-send the conversation to let Claude continue.

The definition is all the model knows

Claude never receives your implementation, only the schema you provide and the result you return. So the definition is your real interface. The docs call detailed descriptions "by far the most important factor in tool performance" and suggest at least 3–4 sentences: what the tool does, when to use it and when not to, what each parameter means and its limits. Adding strict: true guarantees that Claude's inputs match your schema. That guarantees the shape of a call, not that the call is allowed.

Python tools.py
REFUND_TOOL = {    "name": "refund_order",    "description": (        "Refunds a paid order in full to the original payment method. Use "        "it only when the customer asks to refund one specific order. Fails "        "if the order is unpaid, already refunded or not the customer's."    ),    "strict": True,    "input_schema": {        "type": "object",        "properties": {"order_id": {"type": "string"}},        "required": ["order_id"],        "additionalProperties": False,    },}

The bug, and the loop that fixes it

The first version sends the request and prints the reply's text. The second drives the loop: it runs the tool in your own code, as the signed-in customer, and reports failures back with is_error.

Python support_bot.py
import anthropicclient = anthropic.Anthropic()reply = client.messages.create(    model="claude-opus-5-5", max_tokens=16000, tools=[REFUND_TOOL],    messages=[{"role": "user", "content": "Refund order 1042, please."}],)# Prints "I'll refund order 1042 now." and nothing is refunded:# stop_reason is "tool_use" and the request is never answered.print(next(b.text for b in reply.content if b.type == "text"))

In the first version, reply.content still holds an unanswered tool_use block: the model asked, and the program walked away. In the second, refund_order is your function, called with the customer from your session rather than an identity the model supplied. The model only chose the order_id.

MythReality
The model calls my APIIt returns a tool_use request; your code calls the API
The model knows how my function worksIt sees only the name, description and schema you send
A schema-valid call is safe to runstrict: true guarantees the shape, not permission; your code decides
Web search proves models execute codeAnthropic's servers run server tools, in a loop you don't see
One API call completes a tool taskEach call is a round trip; you loop until stop_reason isn't "tool_use"

How to apply it in your project

  1. Branch on `stop_reason`Never read only the text. Run tools while it's "tool_use", continue paused server turns on "pause_turn", and handle "max_tokens" and "refusal" explicitly.
  2. Put a policy check in front of every toolCheck the arguments, the caller's permissions and your business rules in code before executing. Treat the model like an untrusted client of your own API.
  3. Gate side effectsRead-only tools can run automatically. Refunds, deletions and outgoing emails should need a confirmation step, or run in a dry-run mode first.
  4. Log and cap the loopLog every tool_use and tool_result with its id, and stop after a fixed number of iterations so a confused run can't loop forever.
  5. Invest in definitionsWrite descriptions of 3–4 sentences or more, add strict: true, and test on real requests that Claude picks the right tool with the right arguments.

Key takeaways

  • The model never runs your function; it returns a tool_use request and stops.
  • Your application executes the tool, returns a tool_result with the matching id and calls the model again until stop_reason isn't "tool_use".
  • Server tools run on Anthropic's infrastructure: the same contract with a different executor.
  • Claude sees only your schema and description, so they're your real interface.
  • Because your code executes every call, validation, permissions and confirmation belong in your code.

📚 المصادر

  1. How tool use works — Claude API Docs
  2. Handle tool calls — Claude API Docs
  3. Define tools — Claude API Docs
  4. Schick et al., Toolformer: Language Models Can Teach Themselves to Use Tools, arXiv:2302.04761
  5. Yao et al., ReAct: Synergizing Reasoning and Acting in Language Models, arXiv:2210.03629

التعليقات (0)