Lesson 03.11 · Phase 03 · The Agent Loop

Cost and step budgets

Every step re-sends the whole history. Count it, cap it, and warn the model before the cap, not after.

Learning objectives

  • Explain why an agent's input tokens grow with every step, and why a run's total cost grows roughly with the square of its length.
  • Implement a budget with several limits (steps, input tokens, output tokens, dollars, seconds) that records a per-step cost table.
  • Warn the model when a budget is nearly spent, so it can wrap up instead of being cut off.
  • Measure real token counts, and cut them by trimming bloated observations.

Lesson 3 capped steps. Steps are a proxy. What you actually pay for is tokens, and tokens per step aren't constant: each model call re-sends the system prompt, the task, every earlier reply and every tool result. A tool that returns a 4,000-character report makes every later step 1,000 tokens more expensive.

Loading the full lesson…