Velamuvi AIAcademy

Lesson 3 of 9 · How It Works

The Tool Call Round Trip

Follow a tool call end to end: the request with tools, the model's tool_use reply, running the tool, the tool_result with its ID and the final answer.

  • 6 min read
  • Intermediate
  • Reviewed
In this lesson you will learn
  • Follow the messages of a complete tool call round trip
  • Return a tool result linked to the right call ID
  • Use stop reasons to know when the model wants a tool
  • Compare the round trip in Anthropic's and OpenAI's APIs

Two requests for one answer

A question that needs a tool takes (at least) two API requests:

  1. Request 1: the user's message and the tool definitions. Response 1: a tool call, and a stop reason saying so.
  2. Your code runs the tool.
  3. Request 2: the whole history, now including the model's tool call and your result. Response 2: the final answer (or another tool call).

The API is stateless: it remembers nothing between requests. Request 2 must include the tools and the full conversation again.

The messages, step by step

Try it yourselfPython
import json

tools = [{
    "name": "get_order_status",
    "description": "Look up an order's delivery status by order ID (format A-1234).",
    "input_schema": {"type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"]},
}]

# ---- Request 1: the question, plus the tools the model may use ----
messages = [{"role": "user", "content": "Where is my order A-7731?"}]

# ---- Response 1 (Anthropic Messages API format): the model asks for a tool ----
response_1 = {
    "stop_reason": "tool_use",
    "content": [
        {"type": "text", "text": "Let me check that order."},
        {"type": "tool_use", "id": "toolu_01", "name": "get_order_status", "input": {"order_id": "A-7731"}},
    ],
}

# ---- Your code: keep the model's turn, run the tool, add the result ----
messages.append({"role": "assistant", "content": response_1["content"]})
call = response_1["content"][1]
result = {"order_id": call["input"]["order_id"], "status": "out for delivery", "eta": "today 14:00-16:00"}
messages.append({"role": "user", "content": [
    {"type": "tool_result", "tool_use_id": call["id"], "content": json.dumps(result)},
]})

# ---- Request 2 sends the whole history (and the tools again) ----
for m in messages:
    print(m["role"].upper())
    blocks = m["content"] if isinstance(m["content"], list) else [m["content"]]
    for block in blocks:
        print("   ", json.dumps(block))

# ---- Response 2: the model answers using the result ----
print("\nFINAL ANSWER: Your order A-7731 is out for delivery and should arrive today between 14:00 and 16:00.")

Three things make this work:

  • The assistant message containing the tool call is added to the history before the result. The model must see its own request to understand the answer.
  • The result goes in a user message as a tool_result block, carrying the call's ID (toolu_01).
  • The result's content is text, so data is sent as a JSON string.

The same round trip in OpenAI's API

The idea is identical; the field names differ:

StepAnthropic Messages APIOpenAI Responses API
Tools in the requesttools=[{"name", "description", "input_schema"}]tools=[{"type": "function", "name", "description", "parameters"}]
Model asks for a toolContent block type: "tool_use" with id, name, input (an object)Output item type: "function_call" with call_id, name, arguments (a JSON string)
How you knowstop_reason == "tool_use"The output contains function_call items
Your result{"type": "tool_result", "tool_use_id": ..., "content": ...} in a user message{"type": "function_call_output", "call_id": ..., "output": ...} in the next input

Common mistakes

Practice exercise

Complete the round trip: add the model's tool request and your tool result to the history, in the right order.

Your solutionPython
import json

response = {
    "stop_reason": "tool_use",
    "content": [{"type": "tool_use", "id": "toolu_42", "name": "get_order_status", "input": {"order_id": "A-1001"}}],
}
result = {"status": "delivered"}
messages = [{"role": "user", "content": "Has A-1001 arrived?"}]

# Add the two messages needed before calling the model again:
# 1. the assistant message with the model's content
# 2. a user message with one tool_result block for toolu_42 (content = json.dumps(result))
# Then this prints the roles and the id the result answers.
print([m["role"] for m in messages])
print(messages[-1]["content"][0]["tool_use_id"])
Knowledge check

Why must the second request include the model's tool-call message as well as your tool result?

Summary

  • A tool-using answer needs at least two requests: one that ends in a tool call, one that includes the result.
  • The API is stateless: re-send the tools and the whole history each time.
  • Add the assistant tool-call message, then the tool result with the matching ID.
  • Anthropic and OpenAI differ in field names, not in the flow; an adapter keeps your code provider-neutral.

Next: handling several tool calls in one response.

Finished reading? Tick it off to track your progress.