Sub topic 6
Writing a focused agent role and instructions
A custom agent is only as good as its role prompt. This chapter covers how to write that prompt so the agent stays on task, produces predictable output, and does not waste context.
This chapter assumes you know what a context window is, can read Markdown with YAML frontmatter, and have seen the custom agent examples earlier in this tutorial. File names and frontmatter fields differ between products, so check your tool's documentation for the exact schema.
What the role prompt is for
A custom agent bundles three things: a role prompt, a tool list, and metadata such as a name and description. The role prompt is the part that shapes behavior. It is loaded into the context window whenever the agent runs, so every word costs space that could hold code, logs, or conversation.
A good role prompt answers four questions:
- Who is the agent, and what is its single job?
- What process should it follow?
- What must it never do?
- What should its reply look like?
Compare two prompts for the same agent.
You are a helpful assistant that is great at code. Review code and make it better.
Be thorough and careful.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 prompt names no process, sets no boundary, and gives no output format. "Better" and "thorough" are not testable. The second one can be checked: you can run it on a diff and see whether it followed the order, grouped by severity, and left the files alone.
One agent, one job
Give each agent one narrow responsibility. A reviewer, a documentation writer, and a test generator should be three agents, not one agent with three modes.
Narrow agents help in three ways:
- The prompt stays short, so less of the context window is spent on instructions.
- The tool list can be small, because the job needs few tools.
- The description is easy to write, which matters when a parent agent or a person must decide which agent to pick.
If you find yourself writing "if the user asks for X, do this, but if they ask for Y, do that", you probably have two agents.
Anatomy of a role prompt
A reliable structure has four parts. You do not need headings for each one in a short prompt, but the content should be there.
| Part | Purpose | Example |
|---|---|---|
| Role | Sets perspective and scope | "You are a careful code reviewer." |
| Procedure | Orders the work | "Check correctness first, then readability, then test coverage." |
| Constraints | Marks the boundaries | "You only read code. Never modify files." |
| Output format | Makes replies predictable | "Group findings by severity." |
Role
State the role in one sentence. Add a short note on the audience or project only if it changes behavior. "You write concise technical documentation. Match the existing style in the docs folder." works because the second sentence tells the agent where to look for style, instead of listing style rules that may go stale.
Procedure
Use a numbered list when order matters and a bulleted list when it does not. Start steps with verbs. Keep each step to one action the agent can complete and, ideally, you can verify.
Ordering is an instruction in itself. "Check correctness first" tells the agent what to do when it finds many small issues and one real bug: report the bug first.
Constraints
Say what the agent must not do, and say it plainly. Constraints in the prompt are guidance, not enforcement. The model can still ignore them. Where a limit matters, back it with the tool list (see the next chapter section on tool scoping). For example, "Never modify files" is far safer when the agent has no write tool at all.
Prefer a constraint with a reason or a fallback over a bare prohibition:
- Weak: "Do not run commands."
- Better: "Do not run commands. If you need information a command would give, say which command and why, and stop."
The better version tells the agent what to do instead, so it does not stall or improvise.
Output format
Name the shape of the reply. "Reply with a short Markdown list ordered by severity" is enough for many cases. If another agent or a script reads the output, specify the exact fields.
A complete example
Here is a custom agent that checks dependency updates. It shows all four parts together, with a small tool list.
---
name: dependency-auditor
description: Audits dependency files for outdated or risky packages. Use when the user asks about upgrading, auditing, or pinning dependencies.
tools: Read, Grep, Glob
---
You audit dependency declarations in this repository.
When invoked:
1. Find dependency files such as package.json, requirements.txt, or go.mod.
2. List each direct dependency with its declared version range.
3. Flag ranges that are unpinned, use "latest", or point to a Git branch.
4. For each flag, suggest one specific change.
Constraints:
- You only read files. Do not edit them or run package managers.
- If you cannot tell whether a version is outdated, say so. Do not guess.
Output: a Markdown table with columns Package, Declared, Concern, Suggested change.
Finish with one sentence naming the highest-risk item.Notice what the prompt leaves out. It does not explain what a dependency is, and it does not list every package manager's quirks. The model already knows these. Spend words on decisions specific to your project.
Writing the description
The description is separate from the role prompt, but it belongs in this discussion because it decides whether the agent is ever chosen. A parent agent, or a person scanning a list, sees the description before anything else.
A good description says what the agent does and when to use it. Compare:
- Weak: "Helps with code."
- Strong: "Reviews code changes for correctness, readability, and test coverage. Use after code has been modified."
The role prompt speaks to the agent ("You are..."). The description speaks about the agent, to whoever is choosing it. Do not copy one into the other.
Common errors
Too many rules
Long lists of rules dilute each other. When twenty instructions compete, the model weights them unpredictably, and the important one ("never modify files") sits among style preferences. Keep the prompt to what changes the agent's behavior. Move stable project conventions to the persistent project instructions file, which every agent can share, and refer to it instead of repeating it.
Vague quality words
"Be thorough", "write clean code", and "use best practices" cannot be checked. Replace them with observable behavior: "Check every changed function for a matching test", or "Keep functions under 40 lines unless the existing file does otherwise."
Contradictions between layers
The agent sees project instructions, its own role prompt, and the user's request. If the project file says "always add tests" and the agent's role says "only read code", the agent has to guess. Read the layers together once after writing, and remove conflicts.
Instructions that depend on hidden state
"Use the usual format" means nothing to a fresh agent. Name the file that shows the format, or include a two-line example.
Prohibitions with no enforcement
Writing "never delete files" while granting a shell tool does not prevent deletion. Treat prompt constraints as a second line of defense behind tool restrictions.
A short checklist
Before you commit a role prompt, check that it:
- names one job in its first sentence
- gives ordered steps with verifiable outcomes
- states at least one boundary, and that the tool list backs it up
- specifies the reply format
- avoids repeating what the project instructions already say
- is short enough that you could read it aloud in a minute
Summary
Write a role prompt as a short brief for a specialist: one job, an ordered procedure, firm boundaries, and a defined reply format. Spend words on what is specific to your project. Use the tool list to enforce what the prompt only requests. Then test the agent on a real task and trim anything it did not need.
Exercise
A custom agent called test-writer has the instruction "Be careful and write good tests. Never touch production code." It has read, write, and shell tools. Name two problems with this setup and fix each one.
Show answer
First, "careful" and "good tests" are vague and cannot be checked. Replace them with observable steps, such as "Read the function, list its branches, write one test per branch, and run the test file". Second, "Never touch production code" is only a request, and the write and shell tools can still modify any file. Enforce it by limiting writes to the test directory if the tool allows path scoping, or by reviewing every change, and give the shell tool only if the agent must run tests. Also add an output format so the reply lists the tests added.
Exercise
You have a role prompt of 400 words, and 150 of them describe your team's naming and formatting conventions. Where should those 150 words go, and why?
Show answer
Move them to the persistent project instructions file and refer to it from the role prompt, for example "Follow the conventions in AGENTS.md." Then every agent shares one copy, the role prompt stays focused on the agent's job, and an update to the conventions needs only one edit.