Loop engineering with Claude Code /goal: why the evaluator only reads the transcript, and how to add checks that catch what the agent misses.
If you use Claude Code every day, you already run a loop. You prompt, it builds, you look, you say "the button's still wrong", it builds again. The building runs on its own. What eats your evening is the looking. Loop engineering means handing that checking step to the agent too, so you make one call at the end: done, or keep going.
That changes how you spend a week. It's also where people get burned, because a loop is only as honest as its check. A year ago this barely mattered, since models lost the plot after a few minutes. Now a session can grind on one goal for hours, which means a weak check can waste hours too.
Open the slash menu in Claude Code and you'll see both. They sound like the same thing. They aren't.
/loop runs a prompt on a timer. /loop 10m check the deploy fires every ten minutes whether anything changed or not. It's good for polling a build or babysitting a PR. Recurring loops also expire after seven days, which has saved me from at least one I forgot about.
/goal runs until a condition holds. After every turn, a small fast model reads the conversation and returns a verdict: not yet met, met, or impossible. "Not yet met" starts another turn, and the evaluator's reason becomes guidance for it. This is the one you want for "build this feature until it's right."
/goal features/landing-page/spec.md: every item under Acceptance is checked off with evidence in features/landing-page/verification, `npm run verify` exits 0, no files outside app/(marketing) changed, or stop after 30 turns
That last clause matters. Without a turn or time cap, a fuzzy goal will happily burn tokens all night. Running /goal with no arguments shows the turn count and token spend so far, and I check it more than I'd like to admit.
Also, a goal doesn't change your permission mode. In Manual mode it still stops to ask before running your test command. If you want it to run while you sleep, start it in auto mode, and make sure your allow and deny rules are in a state you'd trust without watching.
This is the gotcha I'd tell a colleague first. The /goal evaluator doesn't run commands and doesn't read files. It judges only what the working agent has put in the conversation. When the agent writes "all tests pass", the evaluator sees a sentence. It can't tell whether npm test actually ran, ran against the right folder, or skipped the flaky spec.
Most of the time the agent is honest and the transcript has real test output in it. But "most of the time" is not what you want at hour three of a run nobody is watching. So I put a deterministic check under the model's judgement. /goal is itself built on a prompt-based Stop hook, and you can add your own command-based Stop hook next to it. If your script exits with code 2, Claude can't stop, and it gets your stderr as the reason to keep working.
I only want this gate during loop runs, not every time I ask a quick question, so it checks for a marker file first:
#!/usr/bin/env bash
# .claude/hooks/verify-gate.sh
cat > /dev/null # we don't need the hook input, just drain stdin
cd "$CLAUDE_PROJECT_DIR" || exit 0
[ -f .claude/loop-active ] || exit 0 # gate only while a loop runs
attempts_file=.claude/loop-attempts
n=$(( $(cat "$attempts_file" 2>/dev/null || echo 0) + 1 ))
echo "$n" > "$attempts_file"
if [ "$n" -gt 8 ]; then
echo "verify-gate: 8 failed attempts, letting the session stop" >&2
exit 0
fi
if output=$(npm run verify 2>&1); then
rm -f "$attempts_file"
exit 0
fi
echo "npm run verify failed. Fix this before you stop:" >&2
echo "$output" | tail -n 40 >&2
exit 2
Wire it up in .claude/settings.json, with a timeout long enough for your test suite:
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/verify-gate.sh",
"timeout": 600
}
]
}
]
}
}
And give verify a real meaning in package.json. Note it calls eslint directly, since next lint is gone in recent Next.js versions:
"scripts": {
"verify": "tsc --noEmit && eslint . && playwright test"
}
Touch .claude/loop-active before you start a goal and delete it when you're done. Now there are two judges. The script decides whether the code works. The evaluator decides whether the spec's intent was met. The attempt counter stops one broken test from trapping the agent forever. I learned that one the hard way, with a test that depended on the system clock.
A loop needs to know what "right" looks like before it starts. I keep a features folder where each feature gets a spec and an empty verification folder that the agent fills with screenshots and test output as it works:
features/
landing-page/
spec.md
verification/
session-reviews/
spec.md
mock.html
verification/
queue.md
The spec is both the build instructions and the checklist the loop grades itself against. So acceptance items have to be things that can be shown, not felt. "The hero feels lively" can't be checked. This can:
## Acceptance
- [ ] Hero headline animates in within 600ms of first paint
- [ ] Mascot blinks at least once every 4s (see e2e/mascot.spec.ts)
- [ ] Lighthouse performance score stays at or above 90 on /
- [ ] "Book now" routes to /book with no full page reload
- [ ] Respects prefers-reduced-motion: no animation when it's set
Two habits help. Before writing the spec, have the agent interview you until it has no open questions. Fifteen annoying questions up front beat an hour of the loop guessing what you meant. And once you've written one goal-ready spec, turn the pattern into a skill so you never type the "write this as a goal" boilerplate again. A skill is just instructions in a markdown file. Nothing magic.
A small setup tip while you're in there: keep a one-line CLAUDE.md that points to AGENTS.md. Most other coding agents read AGENTS.md, so your rules aren't locked to one tool when you want to try a second agent on the same repo.
One rule I agree with strongly: don't loop your MVP. To loop anything you have to define the end state first, and at the MVP stage you don't know it. Building version one by hand, with you in the chat, is faster and teaches you what the product actually is. A good sign that something deserves a loop is that it took a lot of back and forth last time.
Screenshot checks are the default way agents verify UI, and they're fine for layout. But a screenshot is a single frame. Anything that happens between frames doesn't exist to it.
Take a GSAP-heavy landing page where the mascot should blink every few seconds. The loop can run for most of an hour, grade every screenshot, and declare victory with the blink still broken. Two screenshots will almost never land inside a 150ms blink. More looping won't fix a check that can't see the bug.
Here's where I got stuck next. I reached for document.getAnimations() to assert the blink was running, and got an empty array. GSAP doesn't use CSS animations or the Web Animations API. It writes inline styles on every animation frame. So the test has to sample over time:
// e2e/mascot.spec.ts
import { test, expect } from '@playwright/test';
test('mascot blinks at least once every 4 seconds', async ({ page }) => {
await page.goto('/');
const eyes = page.getByTestId('mascot-eyes');
await expect(eyes).toBeVisible();
const heights = await eyes.evaluate(async (el) => {
const samples: number[] = [];
const end = performance.now() + 4500;
while (performance.now() < end) {
await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
samples.push(el.getBoundingClientRect().height);
}
return samples;
});
const max = Math.max(...heights);
const min = Math.min(...heights);
// A blink squashes the eyes (scaleY) to well under half their open height
expect(min).toBeLessThan(max * 0.5);
});
getBoundingClientRect reflects transforms, so a scaleY tween shows up as a height change. Put a data-testid on the element and this runs in a couple of seconds per browser. The same idea covers more than animation: debounced search, toast timeouts, optimistic updates that roll back. If the bug lives in time, your check has to sample time.
Once single-feature loops are reliable, the next step is a queue. You plan several features in one sitting, put them in a table, and set a goal that only holds when the table is empty:
| feature | status | branch | pr |
|------------------|----------|------------------------|-----|
| services-page | done | feat/services-page | #14 |
| session-reviews | building | feat/session-reviews | |
| customer-login | todo | | |
/goal work through features/queue.md top to bottom until no row has status todo or building, every done row links an open PR, and npm run verify passed on each branch, or stop after 60 turns
The main session doesn't build anything itself. For each row it hands the work to a subagent on its own branch, then gives that branch to a separate reviewer subagent with a fresh context. The thinking behind it: the agent that wrote the code shouldn't grade it. It has the same blind spots that produced the bug, and it reads its own output kindly.
Before the loop starts, each feature also gets a clickable HTML prototype, the mock.html in the tree above. It's a fake but working version of just the screens that feature changes. Clicking through it tells you whether you wanted the thing you described, which is a surprisingly common "no". And it gives the reviewer something concrete to compare against, instead of prose.
Where I'd push back on the usual advice: "the reviewer should assume there's a bug" is right up to a point. A reviewer told it must always find something will always find something, and you get six rounds of renaming a variable. Give it a severity bar:
---
name: adversarial-reviewer
description: Reviews a finished feature branch against its spec and mock before merge. Use after a builder subagent reports done.
tools: Read, Grep, Glob, Bash
---
Assume the branch has at least one real defect and go looking for it.
Compare behavior against features/<name>/spec.md and mock.html.
Run npm run verify yourself. Never trust a claim that it passed.
Only return BLOCK for: a failed acceptance item, a failing check,
a security or data-loss risk, or behavior that differs from the mock.
Everything else (naming, style, small refactors) goes under NOTES
and must not block. End with exactly one line: BLOCK or PASS.
If you ever let builders run in parallel, give each one its own git worktree. Two subagents on two branches in one checkout will step on each other's files, and the resulting failures look exactly like real bugs.
Each finished feature ends as a pull request with screenshots attached. You read it, maybe check out the branch locally and click around, then merge. Vercel deploys main, and the feature is live. That merge is the decision you keep.
Planning features and approving merges. Neither strictly needs a keyboard. /goal also works through Remote Control and in headless mode with claude -p, so you can describe a feature from your phone, answer the interview questions, and kick off the queue before bed. My own line: I'll approve a copy change or a new page from my phone. Anything touching auth, payments or a database migration waits until I can run the branch myself.
If you want one place to start this week, pick the feature that took the most back and forth last sprint. Write its acceptance list as things a script can check, add the Stop hook gate, and run one goal with a turn cap. Watch the first run. The second one you can probably leave alone.