Sub topic 2
How provider APIs differ
This chapter assumes you can read Python, know HTTP and JSON, and have called at least one hosted chat completion API. It compares what two common provider styles ask for and return. The next chapters use that comparison to design the neutral types.
Why the differences matter
Every hosted provider offers the same basic service: send messages, get text back. The wire formats still differ in small ways. Each difference is a place where code written for one provider breaks on another. Your abstraction has to cover each one.
The examples below compare the OpenAI Chat Completions API with the Anthropic Messages API. Other providers fall mostly into one of these two styles or add their own variations.
The same request in two shapes
Both payloads below ask the same question with the same system instruction.
openai_request = {
"model": "gpt-4o-mini",
"messages": [
{"role": "system", "content": "Answer in one sentence."},
{"role": "user", "content": "What is a Protocol in Python?"},
],
"max_tokens": 200,
}
anthropic_request = {
"model": "claude-3-5-haiku-latest",
"system": "Answer in one sentence.",
"messages": [
{"role": "user", "content": "What is a Protocol in Python?"},
],
"max_tokens": 200,
}OpenAI treats the system prompt as a message with the role system. Anthropic takes it as a separate top-level system field and does not accept that role inside messages. Anthropic also requires max_tokens. OpenAI makes it optional.
The same response in two shapes
The two providers also return results differently.
openai_response = {
"choices": [
{"message": {"role": "assistant", "content": "A Protocol is..."},
"finish_reason": "stop"}
],
"usage": {"prompt_tokens": 24, "completion_tokens": 18},
}
anthropic_response = {
"content": [{"type": "text", "text": "A Protocol is..."}],
"stop_reason": "end_turn",
"usage": {"input_tokens": 24, "output_tokens": 18},
}
def openai_text(resp: dict) -> str:
return resp["choices"][0]["message"]["content"]
def anthropic_text(resp: dict) -> str:
return "".join(b["text"] for b in resp["content"] if b["type"] == "text"))OpenAI nests the text under choices. Anthropic returns a list of typed content blocks, so the text is joined from the blocks of type text. The token counts and stop reasons have different names, and the stop reason values differ too (stop against end_turn).
A summary of the differences
| Area | OpenAI-style | Anthropic-style |
|---|---|---|
| System prompt | A message with role `system` | Top-level `system` field |
| Output limit | Optional | `max_tokens` required |
| Reply location | `choices[0].message.content` | List of `content` blocks |
| Token usage | `prompt_tokens`, `completion_tokens` | `input_tokens`, `output_tokens` |
| Stop reason | `finish_reason` | `stop_reason` |
| Temperature range | 0 to 2 | 0 to 1 |
| Tool call arguments | JSON string in `tool_calls` | Object in a `tool_use` block |
| Streaming | Chunks with `delta` fields, ending in `[DONE]` | Named events such as `content_block_delta` |
Check the current documentation for each provider before you rely on a value. Parameter names and limits change between model versions.
Differences beyond field names
Renaming fields is the easy part. These differences need real decisions:
- Streaming. Both providers use server-sent events, but the event types and the way a stream ends differ. A parser written for one will not read the other.
- Tool calls. The model returns a request to run a function. One provider gives you the arguments as a JSON string that you must parse. The other gives you an already parsed object.
- Errors. Both use HTTP status codes such as 429 for rate limits, but the error bodies differ. Some statuses are specific to one provider, for example Anthropic's 529 for an overloaded service.
- Limits. Context window size, maximum output tokens and rate limits vary by provider and by model.
What to take into the design
The neutral types in the next chapter must hold the union of what applications need. That means a system prompt kept separate from the messages, a required output limit, a list-shaped reply, and token counts under one set of names. Fields that only one provider supports should be optional.
Exercise
A request that works on an OpenAI-style API fails on an Anthropic-style API. It has a system message in the messages list and no max_tokens. Name the two changes an adapter must make.
Show answer
Move the system message out of messages into the top-level system field, and always send max_tokens, using a default value when the caller did not set one.