Testing your subagents: patterns that catch bad prompts | Claude Code Toolkit
Deep dives

Testing your subagents: patterns that catch bad prompts

A subagent prompt is code, and code needs tests. Six patterns for testing subagents in ways that catch prompt regressions before they land in production.

MK
Mateo K. September 4, 2026 · 11 min read

Subagent prompts are code that runs in production, but most teams treat them as prose that gets tweaked when someone notices bad output. This gap becomes visible around month six: your carefully-designed subagent has been patched fifteen times over the year, and you can't tell whether the current version is better than three versions ago. You just know it's different.

This post is about the discipline gap. Subagent prompts should be tested like any other code — regression suites, adversarial cases, checked in CI. Not with heavyweight ML evaluation infrastructure (that's overkill for most teams), but with lightweight patterns that catch the specific failures subagents actually have.

Why subagents drift

Left alone, subagents get worse over time. Not the model — the prompt. Here's the typical arc:

  1. Subagent works well when first written
  2. Someone reports "it missed X in a real case"
  3. You add a bullet to handle X
  4. A month later, someone reports "it missed Y"
  5. You add a bullet for Y
  6. Twelve months later, prompt is 200 lines of patches
  7. Subagent produces worse output than the original
  8. Nobody remembers what the "good" version looked like

The pattern isn't inevitable — but it happens when there's no way to verify that "adding a bullet" actually made things better. Without tests, every change is a hope. With tests, changes get evaluated.

Any prompt that's been patched more than five times without a corresponding test suite is probably worse than the original version. The patches address specific failures but degrade behavior on the cases that were already working.

Pattern 1: Golden output regression tests

The simplest useful pattern. Assemble a set of representative inputs. Run the subagent against each. Save the outputs. Rerun on every prompt change and check the outputs are still approximately correct.

yaml
# .claude/tests/subagents/pr-reviewer/cases.yaml
cases:
  - name: obvious-missing-null-check
    input:
      pr_diff: |
        function getUser(id) {
          const user = db.query(...);
          return user.name;  // no null check
        }
    expected_categories:
      - MISSING_NULL_CHECK
    expected_line_reference: true

  - name: clean-refactor-no-issues
    input:
      pr_diff: |
        // renamed variable for clarity
        - const x = getPrice();
        + const price = getPrice();
    expected_categories: []
    expected_output: "no mechanical issues found"

The tests don't check exact output (LLM outputs vary run-to-run). They check that the right categories were flagged, or the right elements appeared in the output. Loose matching, but consistent enough to catch regressions.

Run the suite before merging any prompt change. If a change makes previously-working cases fail, either the change is wrong or the test is wrong. Deciding which is a valuable moment.

Pattern 2: Adversarial cases specifically for false-positive rate

Regression tests catch "did we break what was working." Adversarial cases catch "did we start flagging things we shouldn't."

Every subagent has a false-positive failure mode. A PR reviewer that's too aggressive comments on things that are fine. A design reviewer that's too strict rejects designs that are just terse. An incident investigator that's too speculative posts hypotheses without evidence. Adversarial cases test for these specifically.

yaml
cases:
  - name: intentional-empty-body-in-test-file
    description: |
      Test files sometimes have empty function bodies (mock 
      implementations). Reviewer should NOT flag as "missing 
      implementation."
    input:
      file: /test/mocks/logger.mock.ts
      content: |
        export function mockLog() { }
    expected_categories: []
    should_NOT_flag:
      - MISSING_IMPLEMENTATION
      - EMPTY_FUNCTION

  - name: single-letter-var-in-math-function
    description: |
      Single-letter variables are fine in math contexts. 
      Reviewer should not flag "x, y, z" in a distance function.
    input:
      diff: |
        function distance(x, y, z) { ... }
    should_NOT_flag:
      - POOR_NAMING

The should_NOT_flag assertion is the key. It catches the failure mode "prompt got too aggressive after the last update." Without adversarial cases, this failure mode ships silently — subagent starts producing lots of comments, users get annoyed, adoption drops.

Pattern 3: The "actual production case" archive

When your subagent produces bad output in production, don't just fix the prompt — add the case to a test archive. This creates institutional memory: the specific failure that motivated the last five patches is now testable.

markdown
## Case: incident-investigator hallucinated a service dependency

Date: 2026-06-15
Real incident: SENTRY-4471

What happened: 
The subagent claimed payments-svc depends on checkout-svc, but the 
actual dependency direction is checkout → payments. Investigation 
went down the wrong path for 15 minutes.

Root cause of subagent error:
Prompt didn't explicitly tell the subagent to verify dependency 
direction from actual code, not from names.

Fix applied:
Added: "Verify dependency direction from actual import statements 
or service calls, not from service names."

Test added:
Case includes two mock services with reversed-seeming names. 
Subagent should correctly identify which imports which.

Two months later, when someone proposes another change to the incident-investigator prompt, they can verify the reversed-dependency case still works. The specific failure that cost 15 minutes six months ago becomes a permanent regression check.

Pattern 4: A/B against the previous prompt

When you're considering a prompt change, run both versions against your test suite. Compare. The tests should tell you: does the new version produce better output on the cases that motivated the change, without regressing on other cases?

Common finding: the new prompt fixes the reported case but introduces subtle problems elsewhere. This is exactly the failure mode that "patch-driven prompt evolution" produces at scale. A/B testing against the suite catches it before the change ships.

The rollback signal

If A/B testing shows your latest prompt version is worse than the previous one on the aggregate suite, rollback. Not because the new version is definitely bad — but because "we changed something, the metrics got worse, we're not sure why" is a sign to slow down. Investigate before proceeding.

Pattern 5: Sanity checks on structural output

For subagents that produce structured output (JSON, YAML, specific templates), test the structure independently of the content. Did the subagent produce valid JSON? Did it include the required fields? Are the fields the right type?

python
def test_incident_investigator_output_structure():
    output = run_subagent('incident-investigator', {'incident_id': 'TEST-001'})
    parsed = yaml.safe_load(output)
    
    assert 'hypotheses' in parsed
    assert isinstance(parsed['hypotheses'], list)
    assert len(parsed['hypotheses']) >= 1
    
    for h in parsed['hypotheses']:
        assert 'id' in h
        assert 'likelihood' in h
        assert h['likelihood'] in ['high', 'medium', 'low']
        assert 'description' in h
        assert isinstance(h['evidence'], list)

This catches the "prompt drifted and now output doesn't parse" problem — the failure mode where the subagent returns something that looks reasonable but breaks any downstream processing. Structural tests are cheap and catch this quickly.

Pattern 6: Cost regression checks

Prompt changes can affect token consumption in surprising ways. Adding a long explanation might make output shorter (subagent is more focused) or longer (subagent expands more). Track token usage per test case as part of your regression suite.

yaml
# In the CI output
Regression suite: 42 cases
- Correctness pass rate: 40/42 (95.2%) — up from 39/42 (92.9%)
- Token usage: 168,432 total — up 12% from previous (147,530)
- Alert: cost regression >10% — needs review

The new prompt is more accurate but 12% more expensive.
Decide whether the accuracy gain is worth the cost increase.

This surfaces the cost/quality trade-off explicitly. Sometimes you accept it (a more expensive prompt that produces better results); sometimes you look for a version that gets the accuracy without the cost. But you can only make the decision if you're measuring both.

What we don't bother with

Some testing patterns exist that aren't worth the overhead for most teams:

  • Semantic similarity scoring: comparing outputs by embedding similarity. Useful for research; overkill for production regression tests. Our category-based checks catch what we need.
  • Statistical significance tests: LLM outputs are variable, so any single run is noisy. But running each case 10 times to average is expensive. Multiple deterministic tests are more informative than one probabilistic one.
  • Full LLM-as-judge evaluation: having another Claude evaluate the subagent's output. Useful in research; produces circular reasoning in production ("the judge Claude approves of what the tested Claude did").

Not because these are bad techniques — they're valuable in research contexts. For production regression testing, simpler patterns catch enough.

The bar to aim for

You know your subagent testing is working when:

  • Every prompt change goes through the suite before merge
  • The suite grows when subagents fail in production
  • Prompt rollbacks happen sometimes (which means you're measuring)
  • You can compare "the version six months ago" to "the current version" concretely
  • Cost regressions get flagged and discussed, not accepted silently

These aren't hard bars to meet. But most teams meet none of them. The gap between "we have subagents in production" and "we're testing them" is where a lot of quality is silently lost.

Every mature subagent workflow eventually reaches this discipline. Might as well start earlier than we did.

MK

Written by

Mateo K.

Mateo focuses on platform engineering, agentic workflows, and turning experimental patterns into infrastructure that scales.

Get cctk running in one command

85 slash commands, 12 subagents, 12 MCP integrations, 12 hooks. All the patterns from this post are shipped in cctk.

npx cctk@latest init Get cctk v1.5.2 →