Sub topic 3
Writing system prompts that steer an agent
This chapter assumes you can write Python, call a chat-style model API, and read the message list used in the agent loop from earlier chapters. You should also know that a model sees a prompt as tokens in a finite context window.
What a system prompt does
A system prompt is the instruction text placed at the start of every request to the model. The user's message says what to do this time. The system prompt says how the agent behaves every time. It is sent again on every turn of the loop, so it costs tokens on every step, and it stays in front of the model while tool results pile up behind it.
A system prompt steers the model but does not enforce anything. The model can ignore a rule, misread it, or lose it in a long history. Rules that must hold, such as "never delete files", belong in code (see the guardrails chapter). Use the prompt for guidance and use code for guarantees.
The parts of an agent prompt
A chatbot prompt can be a single paragraph. An agent prompt has more jobs, because the model acts on its own for several steps. Most reliable agent prompts cover these parts:
| Part | Question it answers | Example |
|---|---|---|
| Role and goal | Who is the agent, and what is it for? | "You are a support agent for an online shop." |
| Tool guidance | When should each tool be used, and in what order? | "Look up the order before promising a refund." |
| Rules and limits | What must it avoid? | "Never reveal another customer's data." |
| Handling uncertainty | What to do when information is missing | "Ask the user. Do not guess an order number." |
| Output format | What the final answer looks like | "Reply in two sentences, then list next steps." |
| Stop condition | When is the task done? | "Stop once the user has an answer. Do not call more tools." |
Tool descriptions already explain what each tool does. The system prompt should not copy them. It should explain how the tools relate to each other and to the task: which to try first, which are expensive, and which need confirmation.
A worked prompt
Here is a prompt for a weather assistant. It is short and specific, and every line changes behavior.
from datetime import date
def build_system_prompt(today: date) -> str:
parts = [
"You are a weather assistant. Answer questions about current "
"conditions in cities.",
"Tools:\n"
"- get_weather(city): returns temperature in Celsius. Call it "
"once per city. Do not call it for a city the user did not name.",
"Rules:\n"
"- If the user does not name a city, ask which city. Do not guess.\n"
"- If a tool returns an error, tell the user what failed. Do not "
"invent a temperature.\n"
"- You cannot give forecasts. Say so if asked.",
"Output: one short sentence with the city and temperature in "
"Celsius. When you have the answer, reply without calling a tool.",
f"Today's date is {today.isoformat()}.",
]
return "\n\n".join(parts)
def start_messages(system_prompt: str, user_goal: str) -> list:
return [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_goal},
]
def recording_model(messages):
# Stand-in model that checks the prompt arrives first.
assert messages[0]["role"] == "system"
return {"content": "Which city do you mean?", "tool_call": None}
prompt = build_system_prompt(date(2026, 11, 28))
messages = start_messages(prompt, "What is the weather?")
print(recording_model(messages)["content"])
print(len(prompt.split()), "words in the system prompt")Some APIs take the system prompt as a separate system parameter instead of a message with the role system. The idea is the same. Build the message list once at the start of the run, then keep appending to it as the loop in the earlier chapter does.
The prompt includes the date because models do not know it. Anything that changes between runs, such as the date, the user's name, or the user's permissions, should be generated into the prompt by code, as build_system_prompt does with today.
How the prompt fits into the loop
The prompt is set once and stays fixed while the history grows after it.
Because the prompt is the first item, it is the part you can least afford to lose when you trim history to fit the context window. Never drop it, and do not summarize it away.
Writing guidelines
- Be specific. "Be careful with refunds" gives the model nothing to act on. "Refunds over 50 EUR need
request_approvalfirst" does. - Say what to do, not only what to avoid. "Do not guess" works better with "ask the user for the missing value".
- Give reasons for important rules. A model that knows the rule exists to protect customer data applies it to cases you did not list.
- Order by importance. Put the role and the hard rules first. Put formatting details last.
- Use clear structure. Labeled sections or short lists are easier for the model to follow, and for you to edit, than one long paragraph.
- Show an example when format matters. One sample of the final answer is often clearer than a paragraph describing it.
- Keep it short. Every line costs tokens on every step, and long prompts bury the rules that matter.
Common errors
- Contradictions. "Always answer fully" and "Keep answers under 20 words" force the model to pick one. Read the whole prompt for conflicts after each edit.
- Rules for enforcement. A prompt line is not a permission check. If a wrong action is costly, block it in code.
- Restating tool schemas. Duplicated tool details drift out of date when you change the tool. Keep them in one place.
- No stop condition. Without one, agents often make extra tool calls "to be sure". Say when to stop. Keep the step limit as a backstop.
- Hidden dependencies. If the prompt mentions a tool that is not registered, the model will request it and
run_toolwill return an unknown-tool error. - Untested edits. A one-line change can alter behavior on tasks you did not check.
Changing prompts safely
Treat the prompt like code. Keep it in version control, change one thing at a time, and run the same set of test tasks before and after. Models respond unevenly to wording, so a change that fixes one case can break another. The evaluation chapter shows how to build that test set. Tracing, covered later, lets you see which messages the model received when it misbehaved, so you can tell whether a rule was missing, unclear, or ignored.
Exercise
A support agent sometimes refunds orders without checking the order status first. You add "Always check the order first" to the system prompt, and it improves but still fails occasionally. Name one change to the prompt and one change outside the prompt that make the behavior more reliable.
Show answer
In the prompt, make the rule specific and give its reason, for example "Call getorder before issuerefund. Refunds on orders that are already refunded cause double payments." Outside the prompt, enforce the order in code: have the refund tool require an order ID that was returned by a previous get_order call, or return an error such as "order not checked" so the model sees the failure and recovers.