The agent loop: act, observe, decide, and the part everyone skips
I build Settl, an expense-splitting app, solo with Claude, and for months I was the loop. I ran the tests, read the failure, pasted my summary of it back, and wondered why simple bugs took nine turns. The model was never allowed to look. Here is how to hand the looking back.
Why observation is the whole difference
A model has no memory between turns. Each turn it reads one block of text and predicts what comes next. True of a chatbot, true of an agent. Only the contents of the block differ.
A chatbot's block holds your request and its own previous words. Nothing in it came from your machine, so any fix it writes is a guess about a codebase it has never seen run.
An agent's block holds something a chatbot's never does: the literal output of a command that
already executed. expected 200, got 500. A stack trace with your file paths in it. The model could
not have invented that text, so the next turn is conditioned on evidence rather than on optimism.
That is the whole upgrade. Same model, same weights, one extra turn with the result in front of it.
The four parts of one turn
Every loop, whatever wrapper it ships in, is four steps:
- Act. The model emits a tool call: run a command, read a file, hit an endpoint.
- Observe. Your harness runs it and appends the unedited result to the transcript.
- Decide. The next turn reads that result and picks the next act.
- Stop. A condition you own: a success check, a turn budget, or a human.
turn 1 assistant -> run_tests()
turn 1 tool <- 2 passed, 1 failed
test_split_rounding: expected 33.34, got 33.33
turn 2 assistant -> read_file("src/split.ts", 40, 60)
turn 2 tool <- (file contents)
turn 3 assistant -> edit_file(...) ; run_tests()
turn 3 tool <- 3 passed
turn 4 assistant -> stop condition met
Delete the tool lines and you have a chat window. Those lines are the agent.
Where your loop is actually broken
Any one of these turns the loop back into a conversation:
- You are the transport layer. You run the command elsewhere and paste back what you judged relevant, so it reads your model of the bug instead of the bug. More in the error-pasting guide.
- The tool returns only an exit code.
exit 1says a failure happened and nothing about where. - Long output is cut from the bottom. Stack traces put the useful frame last. Truncate the head, never the tail.
- The result arrives without its command. Output with no invocation attached has no provenance, so the next turn guesses which of three commands produced it.
- It asks you "did that work?" That question is the model telling you it has no way to look.
- Nothing ends it. With no success check it stops on a guess; with no cap it grinds on a flaky test until you notice.
Hand back the raw result
The highest-value change is the smallest: let it run the command and read the output unedited.
Run `npm test -- --reporter=verbose` yourself and read the full output. Do not
ask me to run it and report back. When a test fails, quote the exact assertion
line and the file:line from the trace before proposing any change.
Give it a stop condition you own
"Until the tests pass" is a predicate a machine can evaluate. "Until it looks right" is not. Put the exit in the prompt:
Loop until `npm test` exits 0, with a hard cap of 8 tool calls.
After each run print one line: attempt number, tests passing, tests failing.
If the same test fails three times under three different changes, stop and tell
me what you have ruled out. Do not keep going.
That last clause earns its place more often than the cap does. A loop with real observation and no escape hatch spends all eight turns re-patching the same wrong layer, for reasons covered in the stuck-loop guide.
When not to close the loop
- Irreversible acts. Deletes, migrations, deploys, payments. Observation cannot undo them, so they stay behind a human. That split is the whole design in the overnight QA setup.
- No cheap oracle. With no command that returns pass or fail there is nothing to observe. Write the failing test first, then loop.
- Slow feedback. A 12-minute suite makes every turn cost 12 minutes. Loop on one file.
Run this on your codebase
Paste this into your agent, in your repo:
Audit whether my setup gives you a real feedback loop or only looks like one.
1. List every command you can run yourself here (tests, typecheck, lint, build,
dev server, database queries), and for each say whether you get its full
stdout and stderr or only an exit code.
2. Give me the fastest command that returns a true pass or fail for the code I
change most often, as an exact invocation scoped to one file.
3. Name what you cannot observe here at all: anything that fails only in the
browser, in CI, or in production.
4. Name any action you can take that no later command can undo.
Do not suggest fixes yet and do not change files. Output a table: capability,
observable, evidence I would see.
Then give me the one-paragraph instruction to paste at the start of my next
debugging session so you run the checks yourself instead of asking me to.