Sub topic 1
Background: messages and roles in a chat request
Tool calling builds on the shape of a chat request. This chapter covers that shape so the later chapters on tool definitions, calls, and results make sense.
Prerequisites
This chapter assumes you have:
- Basic programming skills in Python or a similar language.
- Familiarity with JSON objects and arrays.
- Experience calling an HTTP API or an SDK.
You do not need any prior experience with tool calling.
A chat request is a list of messages
Chat-style model APIs take a list of messages. Each message is an object with a role and some content. The model reads the whole list and returns the next message.
The API is stateless. The model does not remember earlier requests. Your code sends the full conversation every time, and appends each new reply to the list before the next call.
The roles
| Role | Who writes it | Purpose |
|---|---|---|
| `system` | You, the developer | Sets rules, tone, and background for the whole conversation |
| `user` | The end user | Asks questions or gives tasks |
| `assistant` | The model | Replies to the user, and later proposes tool calls |
| `tool` | Your code | Carries the result of a tool the model asked for |
Some providers differ in details. A few take the system prompt as a separate parameter, and some send tool results inside a user message. The role names above are the common pattern, and this tutorial uses them.
A basic exchange
Here is a conversation with no tools. Your code sends the first two messages. The model returns the third.
[
{"role": "system", "content": "You are a concise travel assistant."},
{"role": "user", "content": "Name a city in Norway."},
{"role": "assistant", "content": "Oslo."}
]To continue, you append the next user message and send all four messages again.
messages = [
{"role": "system", "content": "You are a concise travel assistant."},
{"role": "user", "content": "Name a city in Norway."},
{"role": "assistant", "content": "Oslo."},
{"role": "user", "content": "Name another one."},
]
print(len(messages))Why roles matter for tool calling
With tools, a turn is no longer a single question and answer. The model may reply with a request to run a function instead of text. Your code runs it and sends the output back in a tool message. Then the model writes the final answer.
Each step is a message in the same list, so the list records the whole exchange.
Common errors
- Sending only the newest message. The model then has no context. Always send the full list.
- Dropping the assistant message. When the model proposes a tool call, that message must stay in the list. The tool result refers to it.
- Treating
toolcontent as trusted. Tool output is data that came from outside the model. Later chapters cover why this matters for security.
Exercise
A user asks a follow-up question, but the model answers as if the first question never happened. Name the most likely cause in your request code.
Show answer
The code sent only the new user message instead of the full list, including earlier user and assistant messages. The API is stateless, so the model sees only what each request contains.
Exercise
Which role carries a tool's output back to the model, and who creates that message?
Show answer
The tool role. Your code creates it after running the function the model asked for.