AAgent World
foundations

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_turns cap, 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 → Observation paper
  • 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 Modelsarxiv.org Yao et al., 2022 (ICLR 2023). The paper that defined the Thought → Action → Observation loop.
  • Building Effective Agentsanthropic.com Anthropic's working definition of agents vs. workflows, with the loop's design space mapped out.
  • LangGraph docslangchain-ai.github.io Treats the loop as a state machine. Best reference for production-grade agent control flow.
  • OpenAI Agents SDKopenai.github.io The cleanest open-source implementation of the loop — Runner._run_impl is ~50 lines worth reading.
5 resources

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.

Paper2022
ReAct: Synergizing Reasoning and Acting in Language Models
Shunyu Yao et al., Princeton & Google Research · ICLR 2023

The original ReAct paper. Introduces the interleaved Thought → Action → Observation cycle that most modern agent loops still implement under a different name.

Read itarxiv.org
Paper2022
Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
Jason Wei et al., Google Brain

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.

Read itarxiv.org
Paper2023
Toolformer: Language Models Can Teach Themselves to Use Tools
Timo Schick et al., Meta AI Research

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.

Read itarxiv.org
Paper2023
Reflexion: Language Agents with Verbal Reinforcement Learning
Noah Shinn et al., Northeastern University

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.

Read itarxiv.org
Paper2022
MRKL Systems: A Modular, Neuro-Symbolic Architecture
Ehud Karpas et al., AI21 Labs

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.

Read itarxiv.org
10 resources

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.

Code
LangGraph
LangChain · TypeScript & Python

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.

Read itgithub.com
Code
smolagents
HuggingFace · Python

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.

Read itgithub.com
Code
AutoGen
Microsoft Research · Python

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.

Read itgithub.com
Code
CrewAI
CrewAI Inc. · Python

Roles, tasks, crews. A more opinionated framing of multi-agent loops that's friendlier to product teams than AutoGen but narrower in scope.

Read itgithub.com
Code
OpenHands (formerly OpenDevin)
All Hands AI · Python

A full software-engineering agent loop with a sandboxed runtime, a planner, and a code-edit action space. Closest open-source analog to Devin.

Read itgithub.com
Code
Continue
Continue Dev · TypeScript

An in-IDE agent loop that lives inside VS Code and JetBrains. Worth reading for the dev-tooling slice of the agent loop ecosystem.

Read itgithub.com
Code
Aider
Aider · Python

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.

Read itgithub.com
Code
Cline
Cline · TypeScript

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.

Read itgithub.com
Code
Open Interpreter
Open Interpreter · Python

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.

Read itgithub.com
Code
learn-claude-code
shareAI-lab · TypeScript

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.

Read itgithub.com
6 resources

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.

6 resources

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.