Defending AI Agents
עברית Join waitlist
Menu

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 parse arguments. An empty string means {}, and the Anthropic side needs an object. So does a string that does not parse: run keeps a refused call in the history (lesson 2), and the adapter sends it with input: {}, since the tool_result after 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 system message in Anthropic's messages list 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

  1. An assistant turn has two tool calls. How many messages carry their results in Chat Completions, and how many in Anthropic Messages?
  2. A Chat Completions tool call arrives with "arguments": "". What must the Anthropic adapter put in input, and why is the empty string not a JSON object?
  3. Your budget counts prompt_tokens. What goes wrong if the Anthropic adapter passes the usage through without renaming it?
Answers
  1. Two in Chat Completions (one tool message per call) and one in Anthropic Messages: a single user message holding two tool_result blocks.
  2. {}. 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's input requires.
  3. The budget reads prompt_tokens and completion_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 renames input_tokens and output_tokens, and why the build checks a token budget on recorded usage.