What is the Chat Completions API? messages, roles and parameters
The short version
- The Chat Completions API takes a model id and a conversation, and returns the model's next message. You call it with
POST /v1/chat/completions. - OpenAI published the format, and most providers and gateways now accept it. When a service says it has an "OpenAI-compatible API", this is the format it means.
- The request carries the whole conversation each time. The server keeps no state between calls.
- In the response, read three things:
choices[0].message,finish_reasonandusage.
How a request works
You send a JSON body with the model you want and the conversation so far. The model reads all of it and writes what comes next.
The unit of a request is the conversation, which can be a single question. That is why the same endpoint serves a chatbot and one-shot jobs like summaries, classification and code generation: a one-shot job is a conversation of one message.
The messages array and its three roles
The body of the request is the messages array. Each item has a role (who is speaking) and content (what they said).
| role | What it is for |
|---|---|
system | Standing instructions for the model: tone, persona, what to refuse |
user | What the person asks or tells the model |
assistant | What the model said earlier, sent back as history |
The smallest valid request is one user message. A real conversation usually opens with a system message and then alternates user and assistant. For how to write the system part, see what a system prompt is.
You resend the history every time
The API is stateless. The server does not remember your last call, so the second turn has to include the first turn's user and assistant messages:
{
"model": "anthropic-claude-haiku-4-5",
"messages": [
{"role": "system", "content": "You are an assistant that answers briefly."},
{"role": "user", "content": "How tall is the Eiffel Tower?"},
{"role": "assistant", "content": "The Eiffel Tower is about 330 meters tall."},
{"role": "user", "content": "And the Statue of Liberty?"}
]
}
Send only "And the Statue of Liberty?" and the model has no idea what you are asking about. With the history, it knows you want a height.
The cost of this design: the longer the conversation, the more input you send on every call, and the more input tokens you pay for. What a context window is covers where that ends, and what an LLM token is covers how it is counted.
Reading the response
The response has a fixed shape too:
{
"id": "chatcmpl-...",
"object": "chat.completion",
"model": "anthropic-claude-haiku-4-5",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The Statue of Liberty is about 93 meters tall, including its pedestal."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 58,
"completion_tokens": 17,
"total_tokens": 75
}
}
The token counts above are illustrative. The three fields that matter:
choices[0].messageis the answer. Itsroleisassistantand the text is incontent.finish_reasonsays why generation stopped.stopmeans the model finished.lengthmeans it hitmax_tokensand the answer is cut off.tool_callsmeans the model wants to call a function.usageholds the input and output token counts. Billing is calculated from these numbers, so log them if you care about cost.
The parameters to learn first
| Parameter | What it does |
|---|---|
model | The model id. On FastMetal, use an id from the model catalog as written |
messages | The conversation, as described above |
max_tokens | The most output tokens the model may write. It stops runaway answers and runaway bills |
temperature | How random the output is. Near 0 is steady, higher is more varied |
stream | Set it to true to receive the answer piece by piece as it is written |
tools | Function definitions the model may call. The entry point for agent-style work |
For how temperature behaves in practice, see temperature and top_p. For streaming, see the streaming tutorial.
Calling it through FastMetal
FastMetal serves the same format at https://api.fastmetal.ai/v1. With an API key:
curl https://api.fastmetal.ai/v1/chat/completions \
-H "Authorization: Bearer $FASTMETAL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic-claude-haiku-4-5",
"max_tokens": 200,
"messages": [
{"role": "system", "content": "You are an assistant that answers briefly."},
{"role": "user", "content": "Explain the Chat Completions API in one sentence."}
]
}'
With the OpenAI Python SDK, change base_url and nothing else:
from openai import OpenAI
client = OpenAI(
api_key="<FASTMETAL_API_KEY>",
base_url="https://api.fastmetal.ai/v1",
)
resp = client.chat.completions.create(
model="anthropic-claude-haiku-4-5",
max_tokens=200,
messages=[
{"role": "system", "content": "You are an assistant that answers briefly."},
{"role": "user", "content": "Explain the Chat Completions API in one sentence."},
],
)
print(resp.choices[0].message.content)
print(resp.usage)
To try another model, change the model value. The code stays the same, and one key reaches frontier and open-weight models alike, so comparing models costs you an edit. If this is your first call, the first completion tutorial walks through it. A new account's API key opens after its first top-up ($5 minimum); the web chat works before that.
FAQ
How is the Chat Completions API different from the Completions API?
The Completions API is the older format: you send one block of text and the model continues it. The Chat Completions API takes a conversation with roles. Current models are built for the chat format, and new code should use it.
How much history should I send?
Send what the model needs to understand the request. Sending everything grows your input tokens and your bill, so common practice is to keep the last few turns, or to replace older turns with a summary.
Does the request change when I switch models?
For models that support the format, you change model and nothing else. Support for tool calls and image input varies by model, so check the model's page in the model catalog.
Summary
You put the conversation in messages and read the answer from choices[0].message. Learn that shape once and the same code runs against any service that offers the format. To try it, start with the docs and send your first request.