Agent Loop
The repeating think → act → observe cycle an AI agent runs until its goal is reached or a stop condition fires.
Short definition
The agent loop is the repeating cycle an AI agent runs to accomplish a goal that takes more than a single model call. Each turn has three stages: think (the LLM reasons about the current state), act (it calls a tool, returns text, or hands control to another agent), and observe (the result is appended to the message history). The loop continues — with the updated context — until the model is done, max turns are hit, or a stop condition fires.
How it works
┌─────────────────────────────────────┐
│ │
▼ │
[ Think ] → [ Act ] → [ Observe ] ───────┘
│
└── done / max_turns / stop signal
→ return final output
The classic paper formalizing this is ReAct (Yao et al., 2022, ICLR 2023), which interleaves Thought → Action → Observation so the model can both reason and query the world in the same trace. Every modern framework — LangGraph, OpenAI Agents SDK, Claude, AWS Bedrock Agents, AutoGen — implements some version of this loop.
A minimal Python implementation is roughly 30 lines: a while True that calls the model, appends the response to a message list, runs any tool calls, appends the tool results, and breaks when the model stops requesting tools. See the OpenAI Agents SDK source for a clean reference.
Example trace (ReAct)
Question: Who founded the company that makes the PlayStation?
Thought 1: I need to find which company makes the PlayStation.
Action 1: Search["PlayStation manufacturer"]
Obs 1: Sony Interactive Entertainment, a division of Sony Group.
Thought 2: Now I need the founders of Sony.
Action 2: Search["Sony Group founders"]
Obs 2: Masaru Ibuka and Akio Morita, founded 1946 in Tokyo.
Action 3: Finish["Masaru Ibuka and Akio Morita."]
Each step pulls in fresh information from the outside world, so the model doesn't have to rely on (and can't hallucinate) facts it memorized at training time.
Why it matters
The loop is what turns an LLM from a text generator into an agent — a system that can take actions, observe results, and recover from errors. Without it, a model is bounded by its own weights and a single forward pass. With it, the model can chain tools, call APIs, browse the web, run code, and stitch results together across many turns.
Limitations
- Infinite loops: without a
max_turnscap, a confused model can burn tokens forever. - Stale context: tool results accumulate and can crowd out the original goal.
- Cost: every turn is another LLM call. A 20-turn loop on a large model is expensive.
- Termination ambiguity: the model has to decide it's "done", which it sometimes gets wrong.
The interesting engineering work is not the loop itself but everything around it: state management, tool schemas, stop conditions, cost controls, observability, and recovery from failure.
See also
- ReAct — the original
Thought → Action → Observationpaper - Reflexion — adds a self-reflection step inside the loop
- Tool use — the "act" stage of the loop
- Multi-agent — loops that hand control to specialist sub-agents
- LangGraph — state-machine formalism of the loop
- OpenAI Agents SDK — cleanest open-source implementation (~50 lines)
Further reading
A curated set of 27 resources — papers, frameworks, official docs, and practitioner blogs — is rendered below the article. Each card links to the original source.
References
- ReAct: Synergizing Reasoning and Acting in Language Models —
arxiv.orgYao et al., 2022 (ICLR 2023). The paper that defined theThought → Action → Observationloop. - Building Effective Agents —
anthropic.comAnthropic's working definition of agents vs. workflows, with the loop's design space mapped out. - LangGraph docs —
langchain-ai.github.ioTreats the loop as a state machine. Best reference for production-grade agent control flow. - OpenAI Agents SDK —
openai.github.ioThe cleanest open-source implementation of the loop —Runner._run_implis ~50 lines worth reading.
Foundational papers
The five papers every agent loop practitioner should have actually read. They define the vocabulary — thought, action, observation, reflection — that the rest of the field now borrows from.
The original ReAct paper. Introduces the interleaved Thought → Action → Observation cycle that most modern agent loops still implement under a different name.
The precursor that showed step-by-step scratchpads make large models reason. Required reading before you can argue about whether the agent loop is just structured CoT.
Demonstrated LLMs self-supervising on tool calls. The training recipe is heavy, but the framing — "the model decides when to call which tool" — is the conceptual seed of every modern agent loop.
Adds a self-reflection step to the loop: the agent reads its own failed trace, writes a verbal critique, and retries. The pattern still underpins most debugging agents in 2026.
An early modular blueprint — a router model plus specialist tools. Useful for understanding why "agent loop" and "tool use" often get conflated in product marketing.
Open-source agent loop frameworks
Implementations of the agent loop pattern that you can install today. Sorted loosely by adoption; each entry has the canonical repo plus a one-line take on where it sits in the landscape.
A graph-based runtime where each node is a step in the agent loop and each edge is a control-flow branch. The closest thing to an industry default right now.
A deliberately tiny agent loop (~1k LOC of real logic) that ships with the ToolCallingAgent and CodeAgent recipes. The best place to read a minimal loop implementation.
Multi-agent conversations as the unit of computation. The conversational loop is richer than single-agent tool use and harder to debug — start here for role-play patterns.
Roles, tasks, crews. A more opinionated framing of multi-agent loops that's friendlier to product teams than AutoGen but narrower in scope.
A full software-engineering agent loop with a sandboxed runtime, a planner, and a code-edit action space. Closest open-source analog to Devin.
An in-IDE agent loop that lives inside VS Code and JetBrains. Worth reading for the dev-tooling slice of the agent loop ecosystem.
A terminal-native coding agent loop with a strong repo map and a well-known benchmark record. The benchmark numbers are the only honest ones in the space.
VS Code agent loop with explicit tool approvals per step. Useful as a reference for the human-in-the-loop variant of the agent loop.
An agent loop that runs code locally on the user's machine rather than in a sandbox. A good cautionary reference for capability / safety trade-offs.
A from-scratch re-implementation of the Claude Code agent loop, written for readability. Best entry point if you want to read a small production loop end-to-end.
Official documentation
Vendor docs that explain how each lab wants you to think about the agent loop. Read these for the constraint vocabulary — tool schemas, context windows, max-turn limits — that the marketing posts skip.
The Claude-side reference for the agent loop. Best document on the web for designing tool schemas that survive multi-turn execution.
The 2024 essay that drew the now-famous line between "workflows" and "agents". Anchors most discussions of when an agent loop is overkill.
The function-calling contract used by most non-Anthropic agent loops in production. Read alongside the Anthropic docs to see where the two ecosystems disagree.
The mental-model docs (state, nodes, edges, persistence) for the most popular agent loop framework. Pairs with the repo link above.
A walkthrough that demystifies the agent loop by stripping it down to its minimum viable form.
Google's take on the agent loop, with first-class Gemini and Vertex AI integration. Useful as a data point for the Google-side vocabulary.
Authoritative blog posts & deep reads
Long-form pieces from practitioners who actually shipped agent loops. Each is independently written; we link because the framing is durable, not because it's fresh.
The single most-cited primer on the modern agent loop. The planning / memory / tool-use taxonomy is still the lingua franca three years later.
An ongoing critical reading of every agent loop that ships. Best signal-to-noise ratio on the web for separating demos from real systems.
A book-length treatment that situates the agent loop inside the broader reasoning-models landscape. Recommended as the second read after Lilian Weng.
How to actually evaluate an agent loop without lying to yourself. The SWE-bench and tau-bench sections are the ones to read twice.
A vendor-flavoured but technically honest snapshot of where agent loops converge. Good for calibrating your mental model against the ecosystem.
A free, code-first course that walks through implementing an agent loop from scratch. The unit-1 notebook is the canonical "first agent" exercise.