Build your own agentic layer, part 5: a second agent to check the first

Part 5 of a series on building your own agentic layer. Part 4 covers hooks and guardrails.
An agent that just wrote some code is a bad judge of that code. It remembers what it meant to write, the same way you skim right past your own typos. The fix is the same one teams use: get a second pair of eyes. With agents, those eyes are a subagent.
What a subagent is
A subagent is a second agent that the main one hands a job to. It gets:
- its own fresh context window, with none of the main conversation in it
- its own instructions, a markdown file that says what it's for
- its own tool list, which can be narrower than the main agent's
It does the job and hands back one final report. All the files it read and searches it ran stay in its context, not the main one. That's called context isolation, and it's half the reason to use subagents. A big search that would have dumped dozens of files into my main session comes back as a paragraph.
Claude Code has a few built in, like Explore for searching a codebase and Plan for designing. You can define your own as a markdown file in ~/.claude/agents/.
The code reviewer
Trillian's most-used custom subagent is code-reviewer. It's been called 341 times. Here's the top of its file:
---
name: code-reviewer
description: Use to review a working-tree diff or a set of changed files for
correctness and quality before commit/PR — real bugs first, then violations
of the global Universal Principles ... Returns prioritized, location-anchored
findings; it does not edit files.
tools: Read, Grep, Glob, Bash
---
You are a senior code reviewer. You review changes for the things that actually
matter, in priority order, and you do not edit — you report.
Notice the tools. It can read and search, but it has no Edit or Write. It can't "helpfully" fix things itself. It reports, and the main agent decides.
It checks, in order: correctness bugs, swallowed errors, duplicated facts that will drift out of sync, whether the docs in the diff match the code, my general principles, how efficient the database access is, language standards, reuse, and finally whether any comments or docs read like leftover chat. That last one has its own post.
The industry term for using one model to grade another model's output is LLM-as-a-judge.
The five-round review loop
My first version of the rules said: review before handing off, and any edits made in response to review feedback need their own review.
Read that again. It's an infinite loop. It produced a real one: five rounds, about 30 minutes. Rounds four and five only found problems in code written to satisfy rounds two and three. And every round started by confirming the original change was fine.
The rules now:
- One review per body of work. Review once, when the code is done. Fixing the findings doesn't earn another review.
- Only real defects block. Wrong output, crashes, security holes, data loss. Everything else (style, naming, comment wording, nice-to-have tests) is advisory. Fix it in one line or write it down.
- The review covers the diff, not the codebase. If the reviewer finds the same bug in code this change didn't touch, that becomes a new issue. Widening the change to chase it is what turned one review into five.
- Re-check in proportion to the fix. A logic fix gets the type-check and the affected tests. A comment fix gets the formatter. Not the whole suite again.

Wait for it
Subagents run in the background by default, so the main agent can keep working. For the review, that's wrong. The very next step, handing me a commit message, depends on the result.
What happened: the agent said "I've started a review, I'll wait for it," then handed me the commit message anyway, and the review landed after. Saying you'll wait isn't waiting. Now the rule says to run the reviewer in the foreground, so the call doesn't come back until the review is done. There's no "wait" step left to skip.
Don't trust the reviewer blindly either
Two places I've learned this.
Bot reviewers on GitHub. When Copilot leaves review comments on one of my pull requests, Trillian treats them as claims to check, not instructions. Claude sorts each one into valid (fix it), wrong (it misread the code, so draft a short reply with evidence), or debatable (true, not worth it here, I decide). It never posts the replies itself. Without that rule, an agent will cheerfully "fix" correct code because a bot said so.
Research subagents. I had a subagent go through a repo's past pull request reviews and pull out the mistakes reviewers kept catching, so I could turn them into a checklist. Its headline finding said one piece of code was writing a value nothing read. Before it went into the checklist, the reviewer's actual comment got pulled and read. The real problem was different: two separate listeners on the same element, one writing a value and one reading it, with no guarantee which runs first. That's a race condition, not dead code, and the lesson is completely different.
A subagent's report is a summary of work you didn't see. For anything you're about to build a rule on, check the source.
Links
- Claude Code subagents
- Building effective agents, which covers the evaluator pattern
- Writing to the prompter, on the conversation-residue check
- Claude Code common workflows
Drafted with an assistant, edited by @wintaru
0 comments