Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 15 min · revision 3

What Really Happens in a Tool Call

Follow one tool call across the wire: the model returns a structured request, your code runs it and sends the result back. Compare the Claude and OpenAI formats, choose when to force, allow or disable tools, and see how provider-hosted tools differ.

Contentsand lesson path

What you will learn

  • Explain what happens in a tool call (the model emits a structured request, your code runs it) and when to force, allow or disable tool use.

Prerequisites: LLM API Integration

01. Why it matters

"Calls a tool" sounds as if the model reaches into your systems. For the tools you define, it does not. The model returns a small structured message that says "please run get_order with these arguments" and stops. Your program decides whether to run it, runs it, and sends the result back in the next request.

Getting this picture right decides where you put your safeguards. Teams that picture the model "using the API" write permission rules into the prompt, trust whatever arguments arrive, or forget to send a result back and get an error from the API. Teams with the right picture treat every tool call as untrusted input to their own code, and keep the checks there.

In this lesson you trace one call through both major wire formats (Claude Messages and the OpenAI Responses API), learn the settings that force, allow or disable tool use, and see the one family of tools that does run on the provider side: hosted tools such as web search.

02. The round trip: the model asks, your code acts

A tool call takes two requests to the model API and one step in your code:

  1. You send the request with tool definitions. Each tool has a name, a description and a JSON Schema for its arguments. The provider turns these into text the model sees (Claude's docs describe a special system prompt built from your tool definitions), so names and descriptions work like part of the prompt.
  2. The model replies with a call instead of an answer. The reply holds the tool name, the arguments and an id. Claude marks this with stop_reason: "tool_use"; the OpenAI Responses API returns an output item of type function_call. A reply can hold several calls at once, which lesson 3 handles.
  3. Your code acts. It checks the arguments, checks that this user may do this, runs the function and catches its errors. The model is not involved in this step and cannot see your code.
  4. You send the result back, linked to the call by its id, in a new request.
  5. The model reads the result and either answers or asks for another tool. Repeating steps 2 to 5 is the agent loop that later skills build on.

Who remembers the conversation? The Claude Messages API and OpenAI Chat Completions are stateless: every request carries the whole history, including the assistant turn that contains the call. The OpenAI Responses API can keep that history on the server when you pass previous_response_id, and then you only send the new function_call_output items. Either way, the model must be able to see both the call and its result, matched by id.

03. Two wire formats, one idea

The script below holds trimmed copies of what each API returns when the model decides to call get_order, then does step 3 and step 4 for both. It needs only Python 3.12 or later and no API key. Save it as round_trip.py and run it.

Python · round_trip.py
"""One tool call, two wire formats: read the call, run it, build the reply."""
import json

# What each API returns when the model decides to call get_order.
# (Trimmed copies of real response shapes; ids are made up.)
CLAUDE_RESPONSE = {
    "stop_reason": "tool_use",
    "content": [
        {"type": "text", "text": "Let me look that up."},
        {"type": "tool_use", "id": "toolu_01", "name": "get_order",
         "input": {"order_id": "ORD-1044"}},
    ],
}
OPENAI_RESPONSE = {
    "output": [
        {"type": "function_call", "call_id": "call_01", "name": "get_order",
         "arguments": "{\"order_id\": \"ORD-1044\"}"},
    ],
}


def get_order(order_id: str) -> dict:
    """Your code. The model never runs this; you do."""
    return {"status": "shipped", "carrier": "dhl"}


# Claude: arguments arrive as an object; the reply is a *user* message
# whose content holds one tool_result block per tool_use block.
calls = [b for b in CLAUDE_RESPONSE["content"] if b["type"] == "tool_use"]
claude_reply = {"role": "user", "content": [
    {"type": "tool_result", "tool_use_id": c["id"],
     "content": json.dumps(get_order(**c["input"]))}
    for c in calls
]}

# OpenAI Responses API: arguments arrive as a JSON *string*; the reply is
# an input item of type function_call_output with the same call_id.
items = [i for i in OPENAI_RESPONSE["output"] if i["type"] == "function_call"]
openai_reply = [
    {"type": "function_call_output", "call_id": i["call_id"],
     "output": json.dumps(get_order(**json.loads(i["arguments"])))}
    for i in items
]

print("Claude reply:", json.dumps(claude_reply, indent=1))
print("OpenAI reply:", json.dumps(openai_reply, indent=1))
Terminal
python3 round_trip.py
Expected output
Claude reply: {
 "role": "user",
 "content": [
  {
   "type": "tool_result",
   "tool_use_id": "toolu_01",
   "content": "{\"status\": \"shipped\", \"carrier\": \"dhl\"}"
  }
 ]
}
OpenAI reply: [
 {
  "type": "function_call_output",
  "call_id": "call_01",
  "output": "{\"status\": \"shipped\", \"carrier\": \"dhl\"}"
 }
]

The idea is the same, but the details differ in ways that cause real bugs:

  • Claude returns a tool_use content block with an id and an input object. You answer with a message whose role is user and whose content holds one tool_result block per call, each with the matching tool_use_id. When the model made several calls, all their results go in that one message, before any text. A failed tool is still answered, with "is_error": true on its block.
  • OpenAI Responses API returns a function_call item with a call_id and arguments as a JSON string, which can fail to parse. You answer with a function_call_output item carrying the same call_id. There is no error flag: you say in the output text that the call failed.
  • OpenAI Chat Completions, the older endpoint, puts calls in tool_calls on the assistant message and takes each result as a message with role tool and a tool_call_id.

In real code, read the model name from configuration rather than hard-coding a snapshot that will be retired, for example ANTHROPIC_MODEL=claude-sonnet-5-5 or OPENAI_MODEL=gpt-6-astra, and check each provider's models page when you upgrade.

04. Force, allow or disable tool use

The tool_choice request parameter tells the model how free it is on this turn. Both providers offer the same four ideas under slightly different names:

  • Allow (auto), the default: the model decides whether to call a tool or answer in text. Use it when a request may or may not need a tool, which is most of the time.
  • Disable (none): the tools stay defined but cannot be called. Use it on a step that must answer from what has already been gathered, or to measure how the model does without tools (lesson 5).
  • Require any tool: required on OpenAI, {"type": "any"} on Claude. The model must call at least one tool but picks which.
  • Force one tool: {"type": "function", "name": "get_order"} on OpenAI, {"type": "tool", "name": "get_order"} on Claude. Use it when a pipeline step must always end in that call.

Two more knobs control parallel calls. On OpenAI, parallel_tool_calls: false limits a reply to zero or one call. On Claude, "disable_parallel_tool_use": true goes inside the tool_choice object; with auto it means at most one call. OpenAI also has allowed_tools, which limits this turn to a subset of the defined tools.

Forcing a tool guarantees that a call is made, where forcing is supported. It does not guarantee that the arguments match your schema. That is what strict mode is for: "strict": true on an OpenAI function (all properties listed in required, additionalProperties: false, optional fields typed as "value or null") or "strict": true on a Claude tool. Strict mode constrains the shape; it cannot know whether ORD-1044 exists or belongs to this customer, so lesson 4 still validates against real data.

Support also varies by model. Claude Fable 5.2, Claude Opus 5.5 and Claude Sonnet 5.5 (and the older Fable 5.1) return HTTP 400 for any and tool. With those models, use auto, name the tool in the instructions, mark it strict, and check that a call was actually made (retry once if not). If you only wanted structured data back, use the provider's structured output mode instead (see Structured outputs).

05. Tools the provider runs for you

Everything above is about client tools, the ones your code runs. Providers also offer hosted tools, which Anthropic calls server tools: Claude's web_search, web_fetch and code_execution, and OpenAI's web_search, file_search and remote MCP tools, among others. You switch one on by adding its type to the tools list. When the model uses it, the provider runs it inside the same API request and the response already contains the result: Claude returns server_tool_use and web_search_tool_result blocks, OpenAI returns a web_search_call item.

That changes your job in four ways:

  • You do not see the arguments before the tool runs, so control comes from configuration: allowed or blocked domains, a maximum number of uses, and simply leaving the tool off.
  • Each use can cost money on top of tokens, for example a fee per search.
  • The result is text from the outside world. Treat it as untrusted, exactly like a tool result you fetched yourself.
  • Failures usually arrive as a result block that describes the error, not as an exception in your code.

Not every provider tool is hosted. Claude's bash, text editor and computer use tools have schemas defined by Anthropic but run in your environment, so your code handles them like any client tool. Switch hosted tools on only when the task needs information your own tools cannot give, and measure what they add: lesson 5 counts hosted calls per 100 requests with the tool on and off.

06. Check your understanding

Practice check · 3 questions

Reading the wire format

Use what you saw in round_trip.py and the sections above. Each question has one best answer.

Q1. In the OpenAI Responses API, what is the `arguments` field of a `function_call` item, and what must your code do with it?
Q2. A refund step must let Claude make at most one tool call per reply, and still answer in text when no tool is needed. What do you send?
Q3. You add Claude's hosted `web_search` tool to a request. Where does the search run, and how do you control it?
Practice only: not graded, not saved, not evidence.

07. Apply it

Start the practice project for this skill, a mock order system with three tools: look up an order, list a customer's orders and request a refund.

  1. For each tool, write one line on who runs it, what it reads or changes, and whether it has a side effect.
  2. Decide the tool_choice for each step of a support conversation: the opening request, the step after a refund is confirmed, and the final summary. Write down why.
  3. Log every raw tool call (name, arguments, id) and every result you send back, so you can replay a conversation when something goes wrong.

08. Review prompt

Without looking back:

  1. List the five steps of one tool call and say which of them involve the model.
  2. How does a Claude tool result differ from an OpenAI Responses API tool result in where it goes, how it is linked to the call, and how an error is marked?
  3. When would you set tool choice to none, and when would you force a tool? What does each one not guarantee?
  4. Name two ways a hosted tool such as web search changes your responsibilities compared with a tool you run.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check