Definition
The agent loop is the fundamental architectural pattern underlying all AI agent systems. It reduces to a simple while loop: observe context → decide action → execute → update state → repeat. Both Anthropic’s published agent definition and HumanLayer’s 12-factor agents converge on this identical pattern, despite being independently developed.
Key Points
- Both Anthropic and HumanLayer define agents as:
while True: observe → think → act → update state - Anthropic’s version:
action = llm.run(system_prompt + env.state); env.state = tools.run(action) - HumanLayer’s version:
next_step = await llm.determine_next_step(context); if done: return; else: execute - The loop is the oldest and simplest programming construct — yet it is the foundation of the most advanced AI systems
- Frameworks (LangChain, CrewAI, AutoGen, Pydantic AI) add abstraction layers over this loop that can become obstacles for specific use cases
- Most companies building production agents end up rolling their own loop implementation rather than using frameworks
Related Concepts
- context-engineering — What information the loop feeds into the LLM at each iteration
- intent-classification — The loop’s decision mechanism for determining the next action
- graceful-recovery — How the loop handles failures at each iteration
- rag — A specific implementation of the observe step (retrieve context before reasoning)
- llm-as-os — Karpathy’s framing of LLMs as OS kernels; the agent loop is the kernel’s main process
Related Entities
- humanlayer — Published the 12-factor agents principles defining the loop pattern
- orbio-ai — Vertical AI startup building production agents using first-principles loops
Implications
This matters because it demystifies agent architecture. Anyone who understands a while loop can build an agent — the complexity is not in the loop itself but in what you put inside it (conversation design, intent handling, error recovery, context management). This lowers the barrier to building production agents and shifts the focus from framework selection to conversation design quality.
Open Questions
- At what point does a custom loop become complex enough to justify a framework?
- How do multi-agent systems compose multiple loops? Is there a loop-of-loops pattern?
- Does the loop pattern hold for streaming/real-time agents, or does it need adaptation?