Sub topic 2
Messages and roles: how a conversation is sent
This chapter assumes you can read Python and JSON, have called an HTTP API, and know that a language model turns text into more text. The previous chapter showed that a model call is stateless. This chapter shows what the harness sends on each call to make up for that.
A conversation is a list
The harness keeps a list of messages and sends the whole list on every call. Each message has a role, which says who produced it, and content. The model reads the list from top to bottom and writes the next message.
Most providers use these roles. Exact names and field layouts vary.
| Role | Who writes it | Typical content |
|---|---|---|
| system | The harness author | Standing rules and project instructions |
| user | The person, or the harness on their behalf | The task, follow-up requests |
| assistant | The model | Text, and requests to call tools |
| tool | The harness | The result of a tool call |
Some providers take the system prompt as a separate parameter instead of a list entry. Some carry tool results inside a user-role message. The idea is the same in both cases.
What one request looks like
Here is a conversation after the model has asked to read a file and the harness has answered.
[
{"role": "system", "content": "You are a coding agent. Work inside the workspace."},
{"role": "user", "content": "What does main.py print?"},
{"role": "assistant", "content": "I will read the file.",
"tool_calls": [{"id": "call_1", "name": "read_file", "arguments": {"path": "main.py"}}]},
{"role": "tool", "tool_call_id": "call_1", "content": "print('hello')"}
]The id on the tool call and the tool_call_id on the result link the two messages. When the model makes several calls in one reply, this link tells it which result belongs to which request.
The next model call receives all four messages and writes the fifth, probably an assistant message saying the file prints hello.
The harness owns the history
The model remembers nothing between calls. The list is the only memory. So the harness appends every new message and resends everything.
def send(messages, call_model):
reply = call_model(messages) # the model sees the full list
messages.append({"role": "assistant", "content": reply["text"]})
return reply
messages = [{"role": "system", "content": "You are a coding agent."}]
messages.append({"role": "user", "content": "Name a Python web framework."})Two consequences follow:
- The history grows with each turn, so every call is larger than the last. The next chapter covers the limit this creates.
- The harness can edit the history. It can drop old messages, shorten them, or replace them with a summary. The model cannot tell the difference. It only sees the list it receives.
Common mistakes
- Sending only the newest message. The model then has no memory of the task, and its answers look unrelated to earlier turns.
- Dropping a tool result. Many providers reject a request where a tool call has no matching result, or the model repeats the call.
- Breaking the order. Messages must stay in the order they happened. A tool result placed before its call confuses the model or causes an API error.
Exercise
A harness sends only the latest user message on each call, so the model keeps forgetting earlier turns. Why does this happen, and what is the fix?
Show answer
Each model call is stateless, so the model only knows what is in the request. The fix is to keep a list of all messages (system, user, assistant, tool) and send the whole list, with the new message appended, on every call.