The Agent Loop Cheatsheet
The runtime of an AI agent on one page — the loop, context, failure modes, and defenses.
The loop
- Plan
- Model reads context, decides the next move (or that it’s done)
- Act
- Model emits a tool call; the harness runs it
- Observe
- Result appended to context; loop repeats
- Stop when
- Final answer, max steps, budget hit, or stuck-detected
Context management
- What fills it
- System prompt + tools + history + RAG + output reserve
- Compaction
- Summarize old turns before overflow; keep recent in full
- Memory
- Short-term = context; long-term = write/retrieve store
- Prompt caching
- Stable prefix first → provider skips re-encoding it
Failure modes → fixes
- Infinite loop
- Step limit + stuck detection
- Hallucinated tool/args
- Strict schemas + validation
- Prompt injection
- Instruction hierarchy, sanitizing, output filter, tool gate
- Runaway cost
- Model routing, context trimming, caching