Core Concepts
Reasoning
Memory & Retrieval
Agent Types
Design Patterns
Training & Alignment
Frameworks
Tools
Safety & Security
Evaluation
Meta
Core Concepts
Reasoning
Memory & Retrieval
Agent Types
Design Patterns
Training & Alignment
Frameworks
Tools
Safety & Security
Evaluation
Meta
This is an old revision of the document!
The agent loop, also known as the perception-thought-action cycle, is the fundamental execution pattern underlying most AI agent architectures. In each iteration, the agent perceives its environment through observations, reasons about the current state and its goals, and then selects and executes an action that modifies the environment. This continuous cycle enables autonomous agents to operate over multiple steps, adapting their behavior based on feedback from each action.
Every agent loop implementation shares the same basic structure:
This cycle runs in a “while” loop structure, forming the backbone of every major agentic system. The ReAct pattern is a specific instantiation of this loop where the Think step produces an explicit verbal reasoning trace.
The following example implements a minimal agent loop in pure Python that reasons, calls tools, and iterates until the task is done:
# Minimal agent loop with tool use in pure Python from openai import OpenAI import json client = OpenAI() def calculator(expression: str) -> str: return str(eval(expression, {"__builtins__": {}}, {})) TOOLS = [{"type": "function", "function": { "name": "calculator", "description": "Evaluate a math expression", "parameters": { "type": "object", "properties": {"expression": {"type": "string"}}, "required": ["expression"], }, }}] TOOL_FNS = {"calculator": calculator} def agent_loop(user_query, max_iterations=5): messages = [{"role": "user", "content": user_query}] for _ in range(max_iterations): response = client.chat.completions.create( model="gpt-4o", messages=messages, tools=TOOLS ) msg = response.choices[0].message messages.append(msg) if not msg.tool_calls: return msg.content # No more tool calls -- task complete for tc in msg.tool_calls: result = TOOL_FNS[tc.function.name](**json.loads(tc.function.arguments)) messages.append({"role": "tool", "tool_call_id": tc.id, "content": result}) return messages[-1].content print(agent_loop("What is (17 * 31) + (45 * 22)?"))
The agent loop closely mirrors the OODA loop (Observe-Orient-Decide-Act) from military strategy, developed by Colonel John Boyd:
Both frameworks emphasize rapid iteration and adaptation. The OODA loop's insight that faster iteration cycles create strategic advantage applies directly to agent design: agents that quickly process observations and act on them outperform those with slower loops, particularly in dynamic environments.
Modern frameworks implement the agent loop with varying levels of sophistication:
LangGraph uses a StateGraph where nodes represent agents or tools and edges manage state transitions. The loop is expressed as graph traversal, with supervisor nodes routing between worker agents and conditional edges controlling iteration. Supports hierarchical loops, parallel execution, and shared knowledge bases.
CrewAI implements role-based crews with sequential or parallel task execution. The agent loop includes built-in delegation – agents can hand off subtasks to teammates – and refinement cycles where outputs are reviewed and improved.
AutoGen uses conversational multi-agent loops where agents communicate by passing messages. Inner loops handle individual step execution while outer loops manage overall strategy. Supports peer handoff patterns and dynamic orchestration.
OpenAI Agents SDK implements a manager pattern where a central agent delegates via tool calls in a single loop. Sub-agents are invoked as tools, creating nested loops for multi-agent workflows. Designed for cost-optimized, observable iterations.
Agent loops can be implemented using two fundamental execution models:
Hybrid approaches dominate in 2025 production systems, combining polling for the main loop with event-driven handling for tool responses and inter-agent communication.
Modern agent loops stream partial outputs in real-time rather than waiting for complete iterations:
Streaming is critical for managing the high token costs of agentic loops (up to 15x standard chat) by providing early visibility into agent behavior and enabling early intervention.
Robust agent loops handle failures gracefully through several mechanisms:
Production agent loops incorporate human oversight at strategic points: