Topic 2 of 5 · 5 min read
Building a request from messages and roles
Goal: Write a request with a system instruction and a multi-turn history that the API accepts and the model follows.
A chat API call is one self-contained request: a model id, a system instruction, the whole conversation so far as a list of role-tagged messages, and a cap on output length.
Check yourself · 1 of 3
Why does a chat app built on a raw LLM API send the earlier turns with every new request?
Why it matters
The API has no memory. The model does not remember your last call, so a "conversation" exists only because your code sends the earlier turns again each time. If you forget that, the model answers a follow-up like "make it shorter" with no idea what "it" is. The same fact explains cost and limits: every resent turn counts as input tokens and must fit in the context window.
The parts of a request
- model: the id of the model to run, such as a Claude, GPT or Gemini model name. It decides quality, speed and price.
- system instruction: standing rules for the whole exchange, such as role, tone and output format. In the Claude Messages API it is a top-level
systemfield. OpenAI calls itinstructionsin the Responses API, and Gemini calls itsystemInstruction. - messages: the turns, each with a
roleand content.useris what the person said.assistantis what the model said earlier. Roles alternate, and the last message is the one the model answers. - max tokens: the most the model may write in this reply. In the Claude Messages API
max_tokensis required. It is a ceiling, not a target.
How to do it
- Write the system instruction as short rules, not a story.
- Start the list with the first user message.
- After each reply, append the assistant's text, then the next user message, and send the full list again.
- Set
max_tokensto fit the answer you expect, with some room.
history = [{"role": "user", "content": "Name a fast sorting algorithm."}]
r1 = client.messages.create(model=MODEL, max_tokens=300, system="Answer in one sentence.", messages=history)
history.append({"role": "assistant", "content": r1.content[0].text})
history.append({"role": "user", "content": "What is its worst case?"})
r2 = client.messages.create(model=MODEL, max_tokens=300, system="Answer in one sentence.", messages=history)
Before and after
Weak: the second call sends only [{"role": "user", "content": "What is its worst case?"}]. The model has to guess what "its" means. Fixed: it sends the full history above, so the follow-up has its referent.
What goes wrong
- Sending only the newest message and getting answers that ignore the conversation.
- Putting rules in the first user message instead of the system field, so they get buried as the history grows.
- Two user messages in a row or a history that starts with an assistant turn, which some APIs reject.
- Never trimming history. A long chat grows each call until it is slow, expensive or over the context limit.
Examples
The same system rule in three APIs
Claude Messages: system="Answer in one sentence."
OpenAI Responses: instructions="Answer in one sentence."
Gemini: systemInstruction: "Answer in one sentence."
The idea is identical; only the field name and its position in the request change.
A prefilled assistant turn
messages=[
{"role": "user", "content": "List three HTTP methods as JSON."},
{"role": "assistant", "content": "["},
]
Ending the list on an assistant turn asks Claude to continue from that text, which nudges it to start with a JSON array. Check your provider and model: some do not support prefilling.
Key points
- The API is stateless: you resend the whole conversation on every call.
- The system instruction holds standing rules; user and assistant messages hold the turns.
- The last message in the list is the one the model answers.
- Claude requires max_tokens, and it is a ceiling on the reply, not a goal.
- Long histories cost input tokens on every call, so trim or summarize old turns.
Common mistakes
- Assuming the server remembers the previous call and sending only the new question.
- Writing role instructions into a user message and expecting them to hold for twenty turns.
- Setting max_tokens to a tiny number "to save money" and cutting answers mid-sentence.
- Appending the model reply to the history as a user message instead of an assistant message.
Check yourself · 2 of 3
In a message list, which role holds the text the model produced in an earlier turn?
Check yourself · 3 of 3
A support bot must always answer in two sentences. Where does that rule belong?
Practice items, not counted towards your mastery.
Start here
- Claude API overview (opens in a new tab)DocsFree
Authentication headers, the Messages endpoint and the official SDK list in one place.
- Working with messages (Claude docs) (opens in a new tab)DocsFree
Roles, multi-turn requests, response fields and stop reasons with runnable examples.
- OpenAI developer quickstart (opens in a new tab)DocsFree
The same first call in the Responses API, with its SDK setup and key handling.