Sub topic 1
Background: Messages and Roles in a Chat Request
Tool calling is built on the chat message format. Before you define any tools, you need a clear picture of what a chat request contains, who "speaks" in it, and why your code has to resend the whole conversation on every call. This chapter covers that foundation and shows where tool calls will later fit.
Prerequisites
This chapter assumes you already have the following:
- Basic programming in Python or a similar language.
- Familiarity with JSON objects and arrays.
- Experience calling an HTTP API or an SDK, including sending a request body and reading a response.
- A general idea that chat-style LLM APIs take a list of role-tagged messages. This chapter makes that idea precise.
You do not need any tool-calling experience. Later chapters build on the vocabulary introduced here.
The chat request is a list of messages
A chat-style LLM API accepts a list of messages. Each message is an object with at least a role and some content. The model reads the whole list from top to bottom and produces the next message.
[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "What is the capital of Norway?"}
]A reply from the model has the same shape:
{"role": "assistant", "content": "The capital of Norway is Oslo."}The details differ by provider. Some providers take the system text as a separate top-level field instead of a message. Some allow content to be a list of typed blocks (text, images, and later tool calls) instead of a plain string. The examples in this tutorial use a simplified, provider-neutral shape. When you move to a real SDK, check its reference for the exact field names.
The roles
The role tells the model who produced a message and how much weight to give it.
| Role | Who produces it | Purpose |
|---|---|---|
| `system` | You, the developer | Standing instructions: tone, rules, background. Some providers call this `developer`. |
| `user` | The end user (or your code on their behalf) | The request or question. |
| `assistant` | The model | Replies. Later chapters show that this role can also carry tool-call requests. |
| `tool` | Your code | Results of tool calls. Some providers send these inside a `user` message instead. |
The first three roles are enough for ordinary chat. The tool role is new for this tutorial. It carries the output of a function that your program ran, and it is how the model learns what the function returned.
Roles are conventions, not security boundaries
Models are trained to treat system text as more authoritative than user text, but this is a learned tendency, not an enforced rule. Nothing in the format stops a user message from saying "ignore your instructions." Treat the roles as hints that shape the model's behavior. Do not rely on them as access control. The security chapter returns to this point, because tool results are another place where untrusted text can enter the conversation.
The API is stateless
The model does not remember earlier requests. Every call must include all the context you want it to use. Your application owns the conversation history, and a multi-turn chat works like this:
- Start with a list holding the system message.
- Append the user's message.
- Send the list and receive an assistant message.
- Append that assistant message to your list.
- When the user writes again, append the new user message and send the whole list again.
def chat_turn(call_model, history, user_text):
history.append({"role": "user", "content": user_text})
reply = call_model(history)
history.append(reply)
return reply["content"]
def fake_model(messages):
# Stand-in for a provider SDK call. It echoes how much context it saw.
return {"role": "assistant", "content": f"I can see {len(messages)} messages."}
history = [{"role": "system", "content": "You are a concise assistant."}]
print(chat_turn(fake_model, history, "Hello"))
print(chat_turn(fake_model, history, "Hello again"))Running this prints I can see 2 messages. and then I can see 4 messages.. The second call sees the system message, the first user message, the first reply, and the new user message. The history grows with every turn.
This design matters for tool calling in three ways:
- Tool calls and tool results are also messages. You append them to the same list, and the model sees them on the next call.
- Every extra step in a tool loop resends the growing history, so cost and latency rise with each step.
- If you drop or reorder messages, the model loses context. Providers also reject some invalid sequences, such as a tool result with no matching request.
Where tool calling fits
A tool-calling exchange is an ordinary chat that uses two extra kinds of message content. The assistant message may contain a request to run a function. A tool message carries the answer back. The diagram shows one exchange.
The model never runs anything. It produces text, including structured text that asks for a function call. Your code decides whether to run it. That split is the central idea of this tutorial, and the message list is the channel that connects the two sides.
Here is the same exchange as a message list, previewing the format used in later chapters:
[
{"role": "user", "content": "What is the weather in Oslo?"},
{"role": "assistant", "tool_calls": [
{"id": "call_1", "name": "get_weather", "arguments": {"city": "Oslo"}}
]},
{"role": "tool", "tool_call_id": "call_1", "content": "{\"temperature\": 7, \"unit\": \"celsius\"}"},
{"role": "assistant", "content": "It is 7 degrees Celsius in Oslo."}
]Notice three details:
- The assistant message has
tool_callsand no textcontent. A message can hold a request instead of prose. - The
toolmessage carries atool_call_idthat matches the request'sid. This link is how the model pairs each result with each request. - The final assistant message is the answer for the user. Its lack of
tool_callsis the signal that the exchange is finished.
Common errors with messages
These mistakes appear often, even before tools are involved:
- Sending only the newest message. The model then answers without context. Resend the full history.
- Forgetting to store the assistant reply. The next turn then lacks the model's own earlier words, and it may contradict itself or repeat work.
- Putting changing data in the system message of a long chat. Editing early messages can break provider-side prompt caching and confuse the history. Prefer adding new information as later messages.
- Treating message order as flexible. Providers expect a valid pattern, usually system first, then alternating user and assistant turns. Invalid orderings return an error or behave unpredictably.
- Assuming the history is unlimited. Each model has a context window, a maximum amount of text it can read in one request. Long histories must be trimmed or summarized. When you trim, never leave a tool result without its matching request.
Summary of the working model
- A request is a list of role-tagged messages, and a reply is one more message.
- The API is stateless, so your code stores the history and resends it.
- Roles guide the model but do not enforce security.
- Tool requests and tool results are normal messages in the same list, linked by ids.
- The model proposes, and your code decides and executes.
Exercise
A user asks a question, the model replies, and then the user asks a follow-up that depends on the first answer. Your code sends only the follow-up to the API, and the model answers as if it never saw the first question. What went wrong, and how do you fix it?
Show answer
The API is stateless, so the model only sees the messages in the current request. Your code must keep the full history (system message, first user message, first assistant reply, and the follow-up) and send all of it on every call. Fix it by appending each user and assistant message to a list and sending that list each turn.
Exercise
In the Oslo weather example, what links the tool message to the assistant's request, and what would go wrong if that link were missing or wrong?
Show answer
The tool_call_id in the tool message must equal the id of the tool call in the assistant message (here call_1). Without a matching id, the model cannot tell which request the result answers, and many providers reject the request as an invalid message sequence. With several calls in one turn, a wrong id would attach a result to the wrong request.
Exercise
A teammate says, "The system prompt says never reveal internal data, so users cannot trick the model into revealing it." Evaluate this claim.
Show answer
The claim is wrong. Roles are conventions the model has learned to weight, not enforced boundaries. A user message or a later tool result can still push the model to ignore the system text. Protect sensitive data by not giving the model access to it, and by limiting what your tools and code can do, instead of relying on instructions alone.