Agent Loops, Stop Conditions and Token Budgets

Learn how agent loops grow in cost, how to stop them safely, and how to budget tokens, time and money with guards, caching and graceful endings.

Start course →

Syllabus

Anatomy of an Agent Loop

  1. The Structure of a Turn
  2. Reading stop_reason
  3. Why Cost Grows Faster Than Steps
  4. A Loop with Every Guard

Stop Conditions

  1. Defining "Done"
  2. Hard Limits: Steps, Time, Tokens, Money
  3. Detecting No Progress
  4. External Stops and Cancellation

Token Budgets

  1. Counting Tokens
  2. Allocating the Context Window
  3. Setting max_tokens Wisely
  4. Prompt Caching

Controlling Context Growth

  1. Clipping Tool Results
  2. Summarising Old Turns
  3. Offloading to Files and Memory

Failure Modes

  1. A Catalogue of Loop Failures
  2. Retries and Exponential Backoff
  3. Tool Errors and Thrashing

Ending Gracefully

  1. Forcing a Final Answer
  2. Checkpoints and Resume
  3. Escalating to a Human

Observability and Testing

  1. Metrics That Matter
  2. Testing Loops with a Scripted Model

Putting It Together

  1. Case Study: Budgeting a Research Agent
  2. Revision: Cheat Sheet and Self-Check