Sub topic 3
Writing system prompts that steer an agent
This chapter assumes you can write Python, call a web API, and read JSON. It also assumes you know how a prompt reaches a language model. You should have seen the agent loop and tool calling in earlier chapters.
What a system prompt does
A system prompt is the first message in the history, with the role system. It stays at the top of every model call. The model reads it again at every step of the loop, so it is the main place to set stable behavior.
A good system prompt for an agent covers five things:
- Role and goal. Who the agent is and what it is trying to achieve.
- Tool guidance. When to use each tool, and when not to.
- Rules and limits. What it must never do, and when to ask the user.
- Stop condition. How the agent knows it is done.
- Output format. The shape of the final answer.
A worked example
This prompt is for the weather agent from the loop chapter. The code builds the message list and passes it to the loop.
SYSTEM_PROMPT = """You are a weather assistant.
Goal: answer the user's question about current weather.
Tools:
- get_weather(city): use it for any question about current conditions.
Never guess a temperature. If no city is given, ask the user.
Rules:
- Use at most one tool call per city.
- If a tool returns an error, say so plainly. Do not invent data.
Done: reply in one sentence with the city and temperature in Celsius,
and make no further tool calls."""
def start_messages(user_goal: str) -> list[dict]:
return [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_goal},
]
print(start_messages("Weather in Oslo?")[0]["role"])The prompt names the tool and says when to use it. It also tells the model what to do on failure and says when to stop. Each of these lines closes a gap that otherwise shows up as a bug.
Common errors
| Problem | Symptom | Fix |
|---|---|---|
| Vague goal | Agent wanders or does extra work | State one clear goal and a done condition |
| No tool guidance | Wrong tool, or no tool at all | Say when each tool applies |
| Only "don't" rules | Agent avoids the task | Pair each prohibition with what to do instead |
| Very long prompt | Rules are ignored or conflict | Keep it short and remove duplicates |
| Rules only in the prompt | A risky action still happens | Enforce limits in code as well |
Testing a prompt
Treat the prompt like code. Change one thing at a time. Run the same set of test tasks before and after each change. Read the traces to see which instruction the model followed or skipped. A rule that seems clear to you may be read differently by the model.
Exercise
The agent keeps calling get_weather several times for the same city. Name one line to add to the system prompt, and one change outside the prompt that guarantees the limit.
Show answer
Add a rule such as "Use at most one tool call per city." Outside the prompt, enforce the limit in the loop. For example, keep a count of calls per city in runagent and return an error string from runtool when the count passes one. The code limit always holds. The prompt rule is only a request.