Sub topic 6
Writing a focused agent role and instructions
This chapter assumes you have read the earlier chapters on the context window, prompt files, and the basic idea of a custom agent. You should be comfortable with Markdown and simple YAML frontmatter. File names and field names differ between products, so check your tool's documentation for the exact schema.
What the role prompt does
A custom agent's role prompt is the body of its definition file. It is loaded at the start of the agent's session and stays in the context window for the whole session. The model reads it before every reply, so each sentence costs space and attention.
A good role prompt answers four questions:
- Who is the agent, in one line?
- When it is invoked, what does it do, in order?
- What does it produce, and in what format?
- What must it never do?
A focused example
Here is the reviewer agent from the spine, with each part labeled by the question it answers.
---
name: code-reviewer
description: Reviews code changes for correctness, readability, and test coverage. Use after code has been modified.
tools: Read, Grep, Glob
---
You are a careful code reviewer.
When invoked:
1. Inspect the recent changes.
2. Check correctness first, then readability, then test coverage.
3. Report findings grouped by severity, each with a concrete suggestion.
You only read code. Never modify files.The first line is the role. The numbered list is the procedure. The third step is the output format. The last line is the boundary. The tools list backs that boundary up, because the agent cannot edit files even if it ignores the sentence.
Common errors
| Error | Why it hurts | Fix |
|---|---|---|
| A broad role such as "expert developer" | The agent has no clear scope, so its behavior drifts | Name one job, such as reviewing or documenting |
| A long list of style rules | It uses context and competes with the project's persistent instructions | Leave project-wide rules in the shared instructions file |
| Vague steps such as "be thorough" | The model cannot tell when it is done | State observable steps and an output format |
| Only "don't" rules | The agent knows what to avoid but not what to do | Pair each limit with the action you want |
| Restrictions only in prose | Prose can be ignored | Enforce important limits with the tool list |
Keep the description separate from the role
The description field tells the main agent or the user when to pick this agent. The body tells the agent how to work once it has been picked. Do not repeat the procedure in the description. Write the description as a trigger: what the agent does and when to use it.
Writing guidelines
- Put the role in the first sentence. Everything after it is read in light of it.
- Use numbered steps for a procedure and a short list for rules.
- Specify the output. "A Markdown list ordered by severity" is testable. "A good review" is not.
- State what to do when information is missing, for example "If no changes are found, say so and stop."
- Cut any line that you cannot link to a failure you have seen. You can add it back when a test shows it is needed.
Exercise
A custom agent's role prompt says "You are a helpful senior engineer. Be careful and thorough. Never change files." The tool list includes file-writing tools. Name two problems and one fix for each.
Show answer
First, the role is broad and the steps are vague, so the agent has no defined job or output. Fix it by naming one task, giving numbered steps, and stating the output format. Second, "Never change files" is only a sentence, and the tool list still lets the agent write files. Fix it by removing the write tools from the tool list so the limit is enforced.