Agent Loop

Cite this page

What is an agent loop?

An agent loop is the repeating cycle an AI coding agent runs until a task is done: it reads its context, decides on one action (a tool call — read a file, run a command, search the codebase), executes it, observes the result, and feeds that result back in for the next turn. Think-act-observe, over and over, until the model decides it's finished or you stop it. The important detail — the one that decides your bill — is that the context isn't sent once. On every turn the agent re-sends the entire accumulated transcript: the system prompt, your instructions, every tool definition, and every observation it has collected so far. The model is stateless between calls, so "remembering" what it did three steps ago means re-paying for those tokens at full input price on turn four. A loop that takes fifteen turns doesn't cost fifteen units; it costs something closer to the sum of a growing pyramid, because each turn carries the weight of all the ones before it. That's the mechanism, and it's why agent loops are the single biggest reason agentic coding is expensive. The model isn't smarter for re-reading the same 400-line file on every turn — you're just paying for it again.

Why it matters in 2026

Because the loop is where money leaks, and most of the leak is invisible. A single "fix this bug" task can fan out into a dozen turns, and if turn one dumped a giant file into the context window, every subsequent turn re-bills it. At Claude Opus 4.8 input pricing of $5 per million tokens (and Sonnet 5 at $2, GPT-5.5 at $5), a loop that lets context balloon turns a trivial task into a line item you notice. There are two honest levers. The first is prompt caching: the stable prefix of the loop — system prompt, tool defs — can be cached, and a cache read costs roughly 10% of the normal input price. That helps the unchanging part. The second lever, which is where I spend my time, is making each turn's new context lean: feed the agent a skeleton instead of a whole file, filter command output down to the lines that matter, and use semantic code search to fetch only the relevant functions instead of reading three files to find one. This is the whole bet behind Tokenade. It sits between your agent and the model — Claude Code, Cursor, Codex, Copilot, Windsurf, whatever you run — and trims what each turn of the loop carries: semantic search instead of blind reads, output filtering, skeleton compression, and lazy MCP loading so unused tool definitions don't ride along on every turn. There's a dashboard so you can watch the savings instead of taking my word for it. It's source-available (MIT), free up to about 10M tokens a month, and $24.90/mo (excl. tax) for Pro with unlimited machines. If you want the longer version, I wrote up how to reduce a coding agent's token usage.

When the loop framing doesn't help you

  • One-shot prompts. If you're asking a single question with no tool calls, there's no loop — context is sent once, and loop-level optimizations buy you nothing. Optimize the prompt, not the cycle.
  • Short loops over tiny context. A two-turn loop over a 50-line file isn't where your spend lives. Chasing it is busywork; go find the long loops dragging huge files instead.
  • When the bottleneck is reasoning, not context. Sometimes the model genuinely needs many turns because the problem is hard. Trimming tokens won't fix a loop that's long because the task is long — it just makes each turn cheaper.

See also