aiengineering.guideaiengineering.guide

Working SWE → AI engineer · Stop 8 of 15 lessons

The agent loop, from scratch

What this lesson covers

Every agent framework — LangChain, CrewAI, LangGraph — is a wrapper around one simple loop. Write it once, from scratch, and you’ll understand every framework that comes after.

The outline

  1. The loop, in ten lines. Call model → check for tool call → run tool → append to context → repeat.
  2. The three termination conditions. No tool call, budget exceeded, tool said stop. Which to check and in what order.
  3. State between iterations. The message list is the state — why that’s both the simple truth and the source of every bug.
  4. Budgets that matter. Max iterations, max tokens, wall-clock — and how to set each one for real workloads.
  5. The stuck agent. Symptoms of a loop that’s not making progress and how to detect them from telemetry.
  6. When to reach for a framework. Cases where the from-scratch loop isn’t enough, and cases where a framework is only overhead.

Coming soon

In outline.

Outline

Enter to go · Esc to close