Skip to content
mentoor

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?

Choose one answer

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 system field. OpenAI calls it instructions in the Responses API, and Gemini calls it systemInstruction.
  • messages: the turns, each with a role and content. user is what the person said. assistant is 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_tokens is required. It is a ceiling, not a target.

How to do it

  1. Write the system instruction as short rules, not a story.
  2. Start the list with the first user message.
  3. After each reply, append the assistant's text, then the next user message, and send the full list again.
  4. Set max_tokens to 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?

Choose one answer

Check yourself · 3 of 3

A support bot must always answer in two sentences. Where does that rule belong?

Choose one answer

Practice items, not counted towards your mastery.

Start here