
Messages, roles, and the system prompt
The four roles in a conversation, the Go type that holds them, and how to write a system prompt.
On the agent compass Stage 1 of 4 · Build the requestEverything you send a model is one list of messages, and everything an agent does adds to that list. Each message has a role, which tells the model who is speaking. There are four, and knowing who writes each one is most of what you need to read an agent’s code.
The four roles
| Role | Who writes it | What it holds |
|---|---|---|
system |
You, the developer: written once, sent at the start of every request | Instructions: the job, the rules, how to finish |
user |
The person using the program, or your program on their behalf | The request or the task |
assistant |
The model | Its reply: text, a request to run tools, or both |
tool |
Your program, after running a tool | What the tool returned, tagged with the call it answers |
A plain chat uses the first three.
An agent adds the fourth: when the model’s reply asks for a tool, your program runs it and appends a tool message with the result, and the next request carries that result back to the model.
Tool calling on the wire shows those messages as JSON.
The list is the model’s whole view of the conversation. The model keeps nothing between requests, so your program sends the full list every time, oldest message first. On the way in, the provider joins the messages into a single text, with markers for where each role starts and ends; How models pick the next token shows that text.
The messages in Go
The companion code holds a conversation as a slice of one small struct, and a request as that slice plus the tools the model may call:
// Request is what the harness sends the model on each turn.
type Request struct {
Messages []Message
Tools []ToolDef
MaxTokens int // longest reply; 0 means the provider's default
}
// Message is one entry in the conversation.
type Message struct {
Role string // "system", "user", "assistant", or "tool"
Content string // empty when the message has tool calls
ToolCalls []ToolCall // set on assistant messages that call tools
ToolCallID string // on tool messages: the call this answers
IsError bool // set on tool messages that report a failure
}
Content holds the text, ToolCalls holds the tool requests in an assistant message, and ToolCallID ties a tool message to the request it answers.
The client turns these into the JSON the API expects and back again, so the rest of the program never builds message JSON itself; only a tool call’s arguments arrive as JSON for the tool to decode.
The system prompt
The system message, usually called the system prompt, is where you tell the model what job it’s doing. In a chat program it can be one sentence; the first model call used a single line asking for short answers aimed at Go programmers.
An agent’s first task can get by without one.
The bug-fixing agent in the early chapters sends a single user message, two short sentences saying the project’s tests fail and asking the model to find the cause and fix it, and that’s enough, because the task says everything the model needs and the tools do the rest.
A system prompt earns its place once the agent has a standing job that every task shares.
The crash-triage agent is the book’s running example from chapter 7 on: it reads crash reports from remind, a small reminders web service with bugs seeded in it on purpose, and works out what went wrong in the service’s code.
You haven’t met it yet, and you don’t need to; the crash-triage guide shows it at work.
This is the system prompt it sends before every batch of crash reports:
You triage crash reports for remind, a Go web service. Its source is in your workspace; paths are relative to its module root, such as store/store.go.
For each problem:
1. Group the crash reports that share a cause.
2. Find the root cause: the function whose code is wrong.
3. Write a test that reproduces the crash: a new _test.go file whose test fails on the current code.
4. Fix the code, then check that your test and the whole test suite pass.
5. File one issue for the cause.
How to work: start with crash_list, then read reports with crash_read, crash_logs, and crash_goroutines. Investigate with code_search, code_list, and code_read. Change code and create test files with code_patch, and check your work with run_tests. Search the issue tracker with issue_search before filing with issue_file. Don't change or delete existing tests.
When you are done, call submit_diagnosis once, with one entry for every group. If you can't find a cause, file an issue with what you found and give the group the status needs_human.
It’s short, and each part does a specific job:
- What and where. The first paragraph names the job, the service, and where its code is, so the model doesn’t have to guess at paths.
- The steps. A numbered list of what “done” means for each problem, in order, including the test that proves the cause.
- How to use the tools. Which tool to start with and which tools fit which step. The tools’ own descriptions say what each one does; the prompt says when to reach for it.
- One rule. “Don’t change or delete existing tests”, since an agent that deletes a failing test has made the tests pass without fixing anything.
- How to finish, and what to do when stuck. Without a defined ending, a model may stop with a summary in prose that your program can’t read.
What a system prompt can’t do
The model reads the system prompt as instructions, and follows them most of the time. It also reads everything else in the conversation, including text that came from tools, files, and web pages, and some of that text may give it instructions of its own. So a rule that matters for safety, such as “never edit this file” or “never spend more than a dollar”, belongs in your Go code, where the model can’t argue with it. Prompt injection and sandboxing and human approval cover how.
Rules for the list
In the chat completions format, the API checks the list’s shape and refuses a request that breaks it:
- The
systemmessage comes first (Anthropic’s own API, called the Messages API, takes it as a separate field instead; API differences lists the rest). - Every tool call in an assistant message is answered by exactly one
toolmessage, with the call’s ID, before the next assistant message.
The API can’t check one more rule, so the program has to: messages stay in the order they happened. Reordering or dropping one, to save space, can leave a tool result with no call or a call with no result, and the API refuses that.
The agent loop keeps all three by appending the model’s reply before running its tools, and context management covers how to shorten a long list without breaking them.
type Request struct {
Messages []Message
Tools []ToolDef
MaxTokens int // longest reply; 0 means the provider's default
}
Request is what the harness sends the model on each turn.
Request is what the harness sends the model on each turn. type Request struct { Messages []Message Tools []ToolDef MaxTokens int // longest reply; 0 means the provider's default }. In ch02/agent/types.go, line 24.
field Messages []Message
field Messages []Message. In ch02/agent/types.go, line 25.
type Message struct {
Role string // "system", "user", "assistant", or "tool"
Content string // empty when the message has tool calls
ToolCalls []ToolCall // set on assistant messages that call tools
ToolCallID string // on tool messages: the call this answers
IsError bool // set on tool messages that report a failure
}
Message is one entry in the conversation.
Message is one entry in the conversation. type Message struct { Role string // "system", "user", "assistant", or "tool" Content string // empty when the message has tool calls ToolCalls []ToolCall // set on assistant messages that call tools ToolCallID string // on tool messages: the call this answers IsError bool // set on tool messages that report a failure }. In ch02/agent/types.go, line 33.
type ToolDef struct {
Name string
Description string
Parameters jsontext.Value // a JSON Schema
}
ToolDef describes a tool to the model.
ToolDef describes a tool to the model. type ToolDef struct { Name string Description string Parameters jsontext.Value // a JSON Schema }. In ch02/agent/types.go, line 46.
field MaxTokens int
longest reply; 0 means the provider's default
longest reply; 0 means the provider's default field MaxTokens int. In ch02/agent/types.go, line 27.
type int int
int is a signed integer type that is at least 32 bits in size.
int is a signed integer type that is at least 32 bits in size. type int int. Documented at pkg.go.dev, builtin.int.
field Role string
"system", "user", "assistant", or "tool"
"system", "user", "assistant", or "tool" field Role string. In ch02/agent/types.go, line 34.
type string string
string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text.
string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text. type string string. Documented at pkg.go.dev, builtin.string.
field Content string
empty when the message has tool calls
empty when the message has tool calls field Content string. In ch02/agent/types.go, line 35.
field ToolCalls []ToolCall
set on assistant messages that call tools
set on assistant messages that call tools field ToolCalls []ToolCall. In ch02/agent/types.go, line 36.
type ToolCall struct {
ID string
Name string
Arguments jsontext.Value // JSON the model wrote; may be invalid
}
ToolCall is the model asking the harness to run a tool.
ToolCall is the model asking the harness to run a tool. type ToolCall struct { ID string Name string Arguments jsontext.Value // JSON the model wrote; may be invalid }. In ch02/agent/types.go, line 53.
field ToolCallID string
on tool messages: the call this answers
on tool messages: the call this answers field ToolCallID string. In ch02/agent/types.go, line 37.
field IsError bool
set on tool messages that report a failure
set on tool messages that report a failure field IsError bool. In ch02/agent/types.go, line 38.
type bool bool
bool is the set of boolean values, true and false.
bool is the set of boolean values, true and false. type bool bool. Documented at pkg.go.dev, builtin.bool.

