An Interactive Field Guide · № 2

Inside the loop

An AI agent is not a smarter model. It's an ordinary model trapped in a very well-designed while-loop. Step through a real one and watch it think, act, fail, and recover.

Nawa · Full-stack AI engineer ≈ 8 min 4 interactive exhibits

A chatbot answers in a single pass: your words in, its words out, done. An agent is a different animal — it persists. It reads the situation, does something, looks at what happened, and goes around again until the job is finished or a limit says stop. Strip away the mystique and the architecture is almost embarrassingly small: one language model, a handful of tools, and a loop.

The 2026 way of saying it — now that "loop engineering" has become a discipline of its own — is blunt: you're not the model; you're the loop. The model is rented. The loop — what it's allowed to do, when it stops, how it recovers — is where all the engineering actually lives.

So let's build the mystique back up from parts. Our lab rat: a support agent for Peak Bean Coffee Co. (yes, the same imaginary company as essay № 1). A customer named Jamie has just written in: “My espresso machine arrived leaking. I want my money back. — order #PB-2417.”

Exhibit A · What actually goes over the wire
At turn 3 of a conversation, does the model remember turn 1?

This is the entire API request an "agent" sends. Flip between turn 1 and turn 3 and watch what changes.

payload — tokens API state kept between calls — 0 bytes
Observe The API is stateless. Nothing lives on the other side between calls — so the "agent" re-sends the system prompt, every tool schema, and the entire conversation, every single turn. "Memory" is just the loop stapling history into the next request. Hold that thought: it's why context management becomes the whole game in Exhibit C.

§1Around the loop, one click at a time

Here is Jamie's ticket, run through the agent — every model thought, every tool call, every result, exactly as the loop sees them. Nothing is skipped: you'll watch it consult the policy index (retrieval! — inside an agent), hit a real API error, and recover without human help.

Exhibit B · The loop, live

Press ⏭ step (or ▶ run) and watch both panels: the conversation the model sees, and where we are in the loop.

step 0 / 14
The policy allows a refund and a replacement. Jamie asked for money back. What does the model do next?
system220 tok
You are Peak Bean's support agent. Resolve the ticket using the tools provided. Cite policy for any money decision.
tools · 4 schemas380 tok
lookup_order search_policy issue_refund send_reply
user · jamie60 tok
My espresso machine arrived leaking. I want my money back. — order #PB-2417
USER MODEL(stateless) TOOLSlookuppolicyrefundreply call result final
660
ctx tokens
0
model turns
$0.000
spend (toy)
every model turn re-sends the whole context — spend = Σ context × toy rate. watch it accelerate.
Observe Three things worth replaying: the model chose tools in an order nobody scripted; step 8's API error didn't kill the run — the model read the error like any other observation and repaired its own call; and the loop exited not on a timer but because the goal was verifiably done. Choice, recovery, termination — that's the whole trick.

§2The loop eats its own tail

Exhibit A showed that nothing is remembered for free — history is re-sent every turn. Exhibit B quietly demonstrated the consequence: the context ticker only ever goes up. Run a long session and the loop starts drowning in its own past: old tool dumps crowd out the goal, every turn costs more than the last, and eventually you hit the window limit mid-job.

Exhibit C · The window
If the conversation gets twice as long, the cost of the next turn…

Jamie's finished ticket is loaded below. Drag the slider to pile more tickets into the same session — then click any block to compact it (summarize-and-shrink), the way production agents survive.

system + tools user model tool results compacted
/ 4,000 tokens window used — next-turn cost —
Observe Cost scales with everything you've ever said, not with what you're saying now — that's the statelessness tax. And notice what you just did to survive: you threw information away on purpose. Compaction, summarization, pruning — deciding what the model gets to forget is a core loop-engineering job, not an afterthought.
Toy note: real windows run 128k–1M+ tokens and compaction is done by a model, not a click — but the shape of the problem is exactly this.

§3What separates a demo from production

Exhibit B's agent lived in a kind world: one error, one clean retry. Production is meaner — flaky APIs, rate limits, tools that time out at 4 a.m. The difference between an agent that ships and an agent that embarrasses you is rarely the model. It's two numbers you choose: how many times to retry, and when to stop trying.

Exhibit D · The guardrail playground
Tools fail 30% of the time, zero retries, step cap 8. Out of 30 tickets, how many resolve?

Set the world's cruelty and the loop's guardrails, then run 30 tickets. Same knobs → same outcome (seeded), so experiment away.

Observe Retries turn flaky tools back into reliable ones — one retry at 30% flakiness roughly doubles your resolution rate. The step cap never solves a ticket; it converts silent disasters into clean, cheap "I couldn't finish" — which a human can pick up. And "no cap" is how you get the doom loop: an agent burning tokens at 4 a.m., retrying a dead API into infinity.

The loop engineer's checklist

  • a goal the loop can verify, not vibe about
  • tools with crisp descriptions (the model chooses by reading them)
  • structured observation of every result — errors included
  • termination: step caps, token budgets, no-progress detection
  • recovery that adds information, never blind repetition

§4Where loops go feral

Every production agent horror story is one of these three, and you've now met them all:

🌀

The doom loop

Retrying the same failing call with the same inputs, forever. Recovery must change something — Exhibit B's agent added the missing field; that's what made its retry legitimate.

→ uncap Exhibit D and watch
🎈

Context bloat

Fifty turns in, the goal is a needle in a haystack of stale tool dumps. The model starts answering the haystack. Compaction isn't optional hygiene — it's how long sessions stay sane.

→ you fixed this in Exhibit C
🔀

The wrong tool

Vague tool descriptions make the model guess — search_policy vs lookup_order for "where's my order?" is obvious to you, not to a model reading two sloppy one-liners. Schemas are UX for machines.

→ reread the schemas in Exhibit A

§5What to keep

  1. An agent = model + tools + loop. The loop is the product; the model is rented.
  2. The API is stateless — you re-pay for every token of history, every turn. Memory is an engineering choice, not a feature.
  3. Errors are just observations. Good agents recover by adding information; bad ones repeat themselves.
  4. Termination is designed, never emergent: step caps, budgets, verifiable goals.

If you stepped through Exhibit B, you've now watched every component the frameworks wrap in acronyms: the reasoning turn, tool dispatch, observation, recovery, exit. The rest is hardening — the part I do for clients, and the part that makes the difference between 25% and 85% of tickets actually resolved.

N

Nawa — full-stack AI engineer. I design agent loops, RAG pipelines, and the guardrails that keep them boring in production. This essay is № 2 of an experiment: blogs you operate instead of read. If the loop finally clicked, that's the format working.

Colophon — The agent transcript is scripted (a real run, re-enacted deterministically so every reader sees the same teaching moments); the ticket simulator uses a seeded generator so identical knobs give identical outcomes. Real loops add streaming, parallel tool calls, and model-driven compaction — the shape is what you saw. Built with agent assistance; every interaction hand-verified.