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
- Context rot
- Quality degrades as the window fills with stale turns — compact and prune aggressively
- Silent wrong result
- Independent verification — a checker that is not the actor (tests, a reviewer subagent)
Tool calling
- Who runs the tool
- The harness (your code), never the model — the model only emits a structured request
- Tool schema
- Name, description, typed params (JSON Schema) — the model picks and fills from it
- Result handling
- Append the observation to context; on error, feed the error back so the model can retry
- Least privilege
- Grant only the tools/permissions a task needs — an injected instruction can only reach what you exposed