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
- The loop, in ten lines. Call model → check for tool call → run tool → append to context → repeat.
- The three termination conditions. No tool call, budget exceeded, tool said stop. Which to check and in what order.
- State between iterations. The message list is the state — why that’s both the simple truth and the source of every bug.
- Budgets that matter. Max iterations, max tokens, wall-clock — and how to set each one for real workloads.
- The stuck agent. Symptoms of a loop that’s not making progress and how to detect them from telemetry.
- 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.