Sub topic 13
Beyond three roles: tool and developer messages
This chapter assumes you can build a multi-turn history list in Python, know the system, user, and assistant roles, and understand that chat APIs are stateless. It covers two roles that sit beside those three: tool and developer. Exact names and fields vary by provider, so check your provider's documentation before relying on them.
Why more roles exist
System, user, and assistant cover plain conversation. Two situations need more:
- The model asks your code to run a function, such as looking up the weather, and your code must report the result back.
- A provider wants to separate its own highest-priority rules from the instructions an application developer writes.
The tool role handles the first case. The developer role handles the second.
The tool role
A tool is a function your program runs on the model's behalf. The model cannot run code itself. It can only reply with a request to call a tool, using a name and arguments. Your code runs the function and sends the output back as a tool message.
The loop has four steps:
- You send the history plus a description of the available tools.
- The assistant replies with a tool call instead of final text.
- Your code runs the function and appends a message with the role
toolthat holds the result. - You send the history again, and the model writes its answer using that result.
Because the API is stateless, the assistant's tool-call message must stay in the history. The tool message refers to that call by an ID, so a result with no matching call is usually rejected.
The sketch below shows the shape of that history in the OpenAI style. It is a data example, so it runs as is.
history = [
{"role": "user", "content": "What is the weather in Oslo?"},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "get_weather", "arguments": "{\"city\": \"Oslo\"}"},
}
],
},
{"role": "tool", "tool_call_id": "call_1", "content": "{\"temp_c\": 4, \"sky\": \"cloudy\"}"},
]
print([m["role"] for m in history])The next request sends this whole list. The model then answers in plain language, for example that Oslo is 4 °C and cloudy.
Anthropic's API handles the same idea differently. There is no tool role. The tool result goes inside a user message as a content block of type tool_result. The pattern is the same, but the message shape differs.
The developer role
Some OpenAI models accept a developer role. It carries the instructions that the application's author writes, and in those models it takes over the job the system message does elsewhere. The provider's own platform rules rank above it, and the user role ranks below it.
messages = [
{"role": "developer", "content": "Answer in formal English. Never reveal internal ticket IDs."},
{"role": "user", "content": "Where is my order?"},
]Treat developer as the newer name for the role that holds your application's instructions. Older models and other providers may reject it, and some accept system for the same purpose.
Trust and tool output
Tool results are data from the outside world. A web page fetched by a tool may contain the text "ignore your instructions." Treat tool messages like user input. They inform the answer, but they do not get to change the rules. Keep your rules in the system or developer message, and validate tool output before you act on it.
Common errors
| Mistake | Result |
|---|---|
| Dropping the assistant's tool-call message from history | The API rejects the request because the tool result has no matching call |
| Sending a `tool` message with a wrong or missing ID | Error, or the result is attributed to the wrong call |
| Using `developer` on a model that does not support it | The request fails or the role is ignored |
| Using `tool` with Anthropic | Invalid role; use a `tool_result` block in a `user` message |
Exercise
A model returned a tool call with the ID "call7" asking for getweather. Which two messages must your next request add to the history, and in what order?
Show answer
First the assistant message that contains the tool call (ID "call7"), then a message with the role tool, toolcallid "call7", and the function's result as content. Then send the whole history to the model for its final answer.