
The Model Doesn't Run Your Function: How Tool Calling Really Works
- كاتب موثّق
محتويات المقال
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
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.
What actually happens
The model writes a request and stops
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
Send a request with your toolsand the user's message.Claude responds with stop_reason: "tool_use"and one or moretool_useblocks.Run each tool and format the outputs as tool_resultblocks.Send a new request with the earlier messages, the assistant's response and a user message holding the results. Repeat while stop_reasonis"tool_use".
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
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
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
strict: true guarantees that Claude's inputs match your schema. That guarantees the shape of a call, not that the call is allowed.
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
is_error.
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"))import anthropicclient = anthropic.Anthropic()messages = [{"role": "user", "content": "Refund order 1042, please."}]while True: reply = client.messages.create( model="claude-opus-5-5", max_tokens=16000, tools=[REFUND_TOOL], messages=messages, ) messages.append({"role": "assistant", "content": reply.content}) if reply.stop_reason != "tool_use": break # end_turn, max_tokens, refusal...: handle each one results = [] for call in [b for b in reply.content if b.type == "tool_use"]: try: # YOUR code runs the tool, as the signed-in customer out = {"content": refund_order(call.input["order_id"], customer)} except PermissionError as err: out = {"content": str(err), "is_error": True} results.append({"type": "tool_result", "tool_use_id": call.id, **out}) messages.append({"role": "user", "content": results})print(next(b.text for b in reply.content if b.type == "text"))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.
tool_use request; your code calls the API | |
strict: true guarantees the shape, not permission; your code decides | |
stop_reason isn't "tool_use" |
How to apply it in your project
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.Put a policy check in front of every tool Check 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. Gate side effects Read-only tools can run automatically. Refunds, deletions and outgoing emails should need a confirmation step, or run in a dry-run mode first. Log and cap the loop Log every tool_useandtool_resultwith its id, and stop after a fixed number of iterations so a confused run can't loop forever.Invest in definitions Write 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_userequest and stops.Your application executes the tool, returns a tool_resultwith the matching id and calls the model again untilstop_reasonisn'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.
التعليقات (0)