Why does token cost in a naive agent loop grow quadratically with step count?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Every loop turn re-sends the whole growing transcript, so per-turn input scales with step number and the summed cost over N turns is quadratic.
Imagine you hire someone with no memory between minutes. Each time you ask them to do the next part of a job, you must re-read aloud everything that happened so far before they can act. On step one you read one paragraph. On step ten you read all ten paragraphs again, even though only the last one is new. The further you get, the longer your re-reading takes, because the story keeps growing and you always start from the top. An agent loop works exactly like this. The model forgets nothing only because you resend the entire transcript on every turn. Early turns are cheap, late turns are expensive, and the total reading time across a long task piles up far faster than the number of turns alone would suggest.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Start from the stateless model and append-only transcript, show per-turn input scaling with the step index, derive the triangular sum and simplify it to O(N²), rule out pricing and parameter count, then cover prompt caching and the context-trimming techniques that pull cost back toward linear.
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Blaming per-token pricing or model size. Cost grows because each turn resends the full accumulated context, not because tokens get more expensive as the window fills.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.