Sub topic 5
Custom agents: a specialist with its own setup
This chapter covers custom agents: what they are, what a definition contains, how an agent differs from a prompt file, and the errors people hit most often. It assumes you have used an AI coding assistant, can read Markdown and simple YAML, and know what a context window is. Field names and file locations differ between products. The examples here are illustrative, so check your tool's documentation for the exact schema.
What a custom agent is
A custom agent is a named configuration that sets up a separate worker for one kind of job. Instead of typing "act as a careful reviewer, only read files, and report by severity" at the start of every session, you save that setup once and select it by name.
A custom agent usually bundles four things:
- A name, so you or the main agent can select it.
- A description, which says what the agent is for and when to use it.
- A role prompt, which holds the instructions the agent follows.
- A tool list, which limits what the agent can do.
Many tools also let an agent pick its own model, set a working directory, or define hooks. These extras vary a lot, so treat them as optional.
A prompt file, covered in the previous chapters, is a saved message that you send into your current session. A custom agent is different. It is a saved setup for the worker that handles the task. A prompt file changes what you ask. A custom agent changes who answers, what that worker is allowed to do, and what it carries in its context.
Two ways agents are used
Tools run custom agents in one of two ways. Some tools support both.
- Selected as the session's agent. You start a session with that agent, or switch to it. Its role prompt and tool list apply to the whole conversation.
- Delegated to as a subagent. The main agent hands a task to the custom agent, which works in its own fresh context and returns a summary. The main conversation sees only the result.
The second mode matters for context. A reviewer subagent can read thirty files and run several searches. Your main session receives only its findings, so the main context window stays small. The cost is that the subagent starts with none of your conversation. It knows only its own instructions and the task it was given.
Anatomy of a definition
Two common formats exist: Markdown with YAML frontmatter, and JSON. Both express the same idea.
Here is a Markdown definition:
---
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 frontmatter is the part between the two --- lines. It holds configuration. The text below it is the role prompt, which becomes the agent's instructions.
Here is the same idea as JSON:
{
"name": "docs-writer",
"description": "Writes and updates project documentation",
"prompt": "You write concise technical documentation. Match the existing style in the docs folder.",
"tools": ["fs_read", "fs_write"]
}Notice that the two examples name their tools differently (Read, Grep, Glob against fs_read, fs_write). Each product has its own tool names. A tool list copied from one product into another often fails silently or is rejected, so always use the names your tool documents.
| Field | Purpose | Common mistake |
|---|---|---|
| name | Identifier used to select or delegate | Spaces or capitals that the tool does not accept |
| description | Tells a human or the main agent when to use it | Vague text such as "helps with code" |
| prompt or body | Role and instructions | Pasting the whole style guide into it |
| tools | Allowed capabilities | Leaving the field out and getting every tool |
Writing a focused role
The role prompt is read at the start of every run, so each line costs context and competes for attention. A focused role does better than a general one.
A good role prompt does the following:
- States one job. "Reviews code changes" is a job. "Helps with development" is not.
- Gives the steps in order, so the agent does not invent a process.
- Says what the output looks like, such as "a list grouped by severity".
- States the limits in plain words, such as "Never modify files".
It should not repeat what the project's persistent instructions file already says. The agent normally loads that baseline file too, so duplicating build commands or style rules wastes budget and creates two places to update.
A weak and a strong role compare like this:
| Weak | Strong |
|---|---|
| You are a helpful senior engineer who knows everything about our codebase. | You review staged changes. List likely bugs with file and line references, then missing tests. Do not edit files. |
The strong version is shorter, and it can be checked. You can look at an output and decide whether the agent followed it.
Description: the routing text
When delegation is available, the main agent reads each custom agent's description to decide whether to hand off a task. The description therefore works as routing text. It should say what the agent does and when to use it.
The reviewer example does this: "Reviews code changes for correctness, readability, and test coverage. Use after code has been modified." The first sentence names the job. The second names the trigger.
If two agents have overlapping descriptions, the main agent may pick the wrong one or never pick either. Write each description so that it is clear which task belongs to which agent.
Limiting tools
The tool list is the main safety control of a custom agent. A reviewer that cannot write files cannot damage your repository, whatever its prompt says or whatever text it reads while working.
Apply least privilege:
- List the actions the job needs. A reviewer reads and searches. A docs writer reads and writes files.
- Grant only those tools.
- Add shell or network access only if the job truly requires it, and then restrict it if your tool supports allow lists for commands or paths.
Compare two controls. The sentence "Never modify files" in the prompt is a request, and the model can fail to follow it, especially after reading misleading text in a file. Leaving out the write tool is enforced by the tool. Use both, and rely on the tool list.
Check what happens when the tools field is omitted. In some products, an omitted list means the agent inherits all tools from the main session. In others, it means no tools. Do not guess. Write the list explicitly.
Common errors
- The agent is never chosen. The description is vague or overlaps with another agent. Rewrite it to name the task and the trigger.
- The agent lacks context. As a subagent it starts fresh. Make sure the delegation brief includes the paths, the goal, and the expected output, or tell the agent in its prompt where to look first.
- The agent does too much. The role covers several jobs. Split it into two agents.
- Tool names are rejected or ignored. The names come from another product. Check the schema.
- The prompt contradicts the baseline instructions. Both are in context, and the model must guess which wins. Keep one source of truth for each rule.
- The file is not found. The definition is in the wrong folder or has invalid frontmatter. Check the location your tool scans and validate the YAML, since a missing colon or unquoted special character can break loading.
A worked decision
Suppose you want a weekly pass that checks recent commits for missing tests. Ask these questions:
- Does it need a different tool set from your normal session? Yes, read-only.
- Does it produce a lot of intermediate reading that you do not want in your main context? Yes.
- Is it a recurring role rather than a one-off message? Yes.
All three answers point to a custom agent. If only the first answer were no, and you just wanted a repeatable message, a prompt file would be simpler. The later chapter on choosing a mechanism develops this comparison in full.
Exercise
You define an agent called release-notes with the prompt "You write release notes. Never run shell commands." You leave the tools field out. Your tool treats an omitted list as "all tools available". Why is this definition risky, and what should you change?
Show answer
The sentence in the prompt is only a request, while the omitted tools field grants every tool, including shell access. The agent could run commands if it is misled or makes a mistake. Add an explicit tools list with only what the job needs, for example read and write file tools, and leave out shell access. Then the restriction is enforced by the tool instead of depending on the model's compliance.
Exercise
A subagent called test-finder is rarely used, even when you ask the main agent to "check for missing tests". Its description is "Helps with testing." Rewrite the description and explain the change.
Show answer
For example: "Finds public behavior that has no test by comparing recent changes against the test files. Use after code has been modified or before opening a pull request." The new text names the specific job and the situation that should trigger it, so the main agent can match it to requests like yours. The old text was too general to tell it apart from ordinary testing help.