E3 · Lesson 03
One tool call, three wire formats
Every provider agrees on the idea of a tool call and disagrees on how it looks on the wire. An agent that is moved from a local model to a hosted one, or from one hosted API to another, breaks at those disagreements, usually on the first parallel call or the first tool with no arguments. The cure is the usual one: one internal format, and an adapter at each edge.
The same call, three ways
"Read notes/a.txt", as the model's reply and as the result your loop sends back:
Chat Completions (OpenAI's older API; served by Ollama, llama.cpp, vLLM and LM Studio):
{"role": "assistant", "content": "",
"tool_calls": [{"id": "call_1", "type": "function",
"function": {"name": "read_file", "arguments": "{\"path\": \"notes/a.txt\"}"}}]}
{"role": "tool", "tool_call_id": "call_1", "content": "alpha"}
Responses (OpenAI's newer API): the reply is a list of output items, and the tool schema is flattened.
{"type": "function_call", "call_id": "call_1", "name": "read_file", "arguments": "{\"path\": \"notes/a.txt\"}"}
{"type": "function_call_output", "call_id": "call_1", "output": "alpha"}
Anthropic Messages: a reply is a list of content blocks, and there is no tool role.
{"role": "assistant", "content": [{"type": "tool_use", "id": "toolu_1", "name": "read_file",
"input": {"path": "notes/a.txt"}}]}
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "toolu_1", "content": "alpha"}]}
The differences that carry bugs
| Chat Completions | Responses | Anthropic | |
|---|---|---|---|
| arguments | a JSON string | a JSON string | an object |
| tool schema key | function.parameters |
parameters (flat) |
input_schema |
| system prompt | a message | instructions |
a top-level system field |
| tool result | role tool |
an input item | a tool_result block in a user message |
| several results | one message each | one item each | all in one user message |
| call id | id |
call_id |
id, answered by tool_use_id |
| usage | prompt_tokens, completion_tokens |
input_tokens, output_tokens |
input_tokens, output_tokens |
Three of these are behaviour, not spelling.
- String or object. Only the string can be malformed (lesson 2). An adapter from Anthropic to Chat Completions must serialise
input, and one going the other way must parsearguments. An empty string means{}, and the Anthropic side needs an object. So does a string that does not parse:runkeeps a refused call in the history (lesson 2), and the adapter sends it withinput: {}, since thetool_resultafter it already says what was wrong. - One user message for all results. Anthropic's documentation says that sending each result in its own user message "teaches" the model to stop making parallel calls. The history is a few-shot prompt, and its format is part of what the model learns from it.
- The system prompt moves. Leave a
systemmessage in Anthropic'smessageslist and the request fails. Drop it silently and you lose the instructions.
An adapter at the edge
loop/formats.py keeps Chat Completions inside the loop and converts at the edges:
body = to_anthropic_request(messages, tools.schemas, model="...", max_tokens=1024) # history out
message, usage = from_anthropic_response(resp) # reply in
to_anthropic_request lifts the system messages into system, turns each assistant message into a text block followed by tool_use blocks with their arguments parsed, and gathers consecutive tool messages into one user message of tool_result blocks. from_anthropic_response does the reverse and renames the usage fields so that the budget (lesson 6) charges the same way whatever served the call. The Responses adapter is provided, for comparison.
The check runs these adapters on recorded shapes, not live APIs: the course needs no account. If you have one, send to_anthropic_request's output to the real endpoint once. Your adapter is right when the reply's tool_use comes back through from_anthropic_response and your loop runs it.
Formats move
Wire formats are not stable. OpenAI added Responses beside Chat Completions. In October 2026 Google's function-calling guide for Gemini describes a newer "Interactions" API, while older pages still describe functionCall parts. A course that taught one format as the format would be out of date within a year. The lasting skill is the adapter, with a test of recorded shapes for every format you support, so that you notice when a provider changes one.
Check yourself
- An assistant turn has two tool calls. How many messages carry their results in Chat Completions, and how many in Anthropic Messages?
- A Chat Completions tool call arrives with
"arguments": "". What must the Anthropic adapter put ininput, and why is the empty string not a JSON object? - Your budget counts
prompt_tokens. What goes wrong if the Anthropic adapter passes the usage through without renaming it?
Answers
- Two in Chat Completions (one
toolmessage per call) and one in Anthropic Messages: a single user message holding twotool_resultblocks. {}. The empty string is not valid JSON of any type:json.loads("")raises. Servers send it to mean "no arguments", so the adapter must map it to the empty object, which Anthropic'sinputrequires.- The budget reads
prompt_tokensandcompletion_tokens, finds neither, counts zero for every call, and the token and cost limits never fire. The run is unbounded, with no error. That is why the adapter renamesinput_tokensandoutput_tokens, and why the build checks a token budget on recorded usage.