Agent Loop
Agent Loop is a core operational architectural pattern in autonomous agent systems, referring to the working mechanism by which an agent completes complex tasks through continuous cyclic execution of steps such as perception, reasoning, action, and observation. In each iteration, the agent invokes a large language model for reasoning, generates action instructions, calls tools or interacts with the environment, and feeds the results as new input into the next reasoning round until the goal is achieved or a termination condition is met. This pattern is a foundational design principle for agent frameworks such as ReAct and AutoGPT.
Core Facts
The underlying operational logic of an Agent Loop is an iterative cycle of 'perception–planning–action–observation.'
The decision loop of an AI Agent consists of four steps: Sense, Plan, Action, and Observe, forming a closed-loop iteration.
The agent flow is: send a message to the model to get a response; if the stop reason is not tool use, return the text; otherwise, iterate through the tool-call blocks, validate parameters, run the function, push the result back as a tool result, and loop.
Timeline (last 90 days)
Common engineering means of terminating an agent include maximum step/maximum round limits, timeout budgets, consecutive-failure counters, and failure-signal detection based on rules or model judgment.
When a limit is triggered, execution should stop and return the partial results already completed along with a concise explanation; a justified partial answer is far better than an Agent that loops infinitely until killed externally.
When asked to 'execute at most ten steps', the model sometimes executes thirty steps; this is an inherent limitation of current models, and the reliable approach is to enforce the maximum number of steps, timeouts, and token or cost budgets at the code level of the run loop.
Defining the state machine before writing any code allows you to consider boundary cases in a structured way, including which events can be triggered in which state of the loop, and which terminal states can cover these boundary cases.
Cursor and Claude Code are both based on the same core Agent work loop: read code, make a plan, edit files, run verification, and check results.
In a cloud Agent architecture, the Agent Loop does not run inside the sandbox; the Agent and the sandbox are separate.
Both the Claude Agent SDK and the OpenAI Agents SDK take over the Agent Loop, greatly simplifying the model invocation, tool dispatching, history maintenance, and loop control logic that previously had to be written by hand.
Large language models are responsible for thinking and decision-making but never directly execute actions themselves; the code outside the model is what actually executes the actions.
A turn limit and a budget should be set for the Agent loop to prevent it from getting stuck in an infinite loop and to avoid runaway costs.
Every round of calls sends the complete conversation history again, so the longer the task, the larger the context, and the slower and more expensive the calls become.
32 more timeline events
All Facts (20)
The underlying operational logic of an Agent Loop is an iterative cycle of 'perception–planning–action–observation.'
80%VerifiedThe decision loop of an AI Agent consists of four steps: Sense, Plan, Action, and Observe, forming a closed-loop iteration.
80%VerifiedThe agent flow is: send a message to the model to get a response; if the stop reason is not tool use, return the text; otherwise, iterate through the tool-call blocks, validate parameters, run the function, push the result back as a tool result, and loop.
75%VerifiedModern AI coding agents typically adopt a plan-execute-reflect loop framework, decomposing complex tasks into sub-steps and self-correcting based on execution results.
70%VerifiedCursor and Claude Code are both based on the same core Agent work loop: read code, make a plan, edit files, run verification, and check results.
65%VerifiedEach iteration cycle of an Agent consists of three phases—model inference, tool invocation, and result return—and latency accumulates at every step
65%VerifiedIn workflow-type agents, the execution path is pre-designed by humans, whereas an Agent Loop lets the model autonomously plan what to do next.
65%Unverified在Agent Loop中如果只将工具结果塞回messages而漏掉assistant消息,模型会陷入无限循环,不断重复调用工具
90%UnverifiedAgent Prompt模板通常包含四个部分:System Message、Chat History、User Input和Agent Scratchpad
85%UnverifiedOpenAI Codex CLI本质上是一个在本地终端运行的Agent循环(ReAct Loop),每一轮迭代包含思考、行动、观察三个步骤
85%Partially VerifiedClaude Code's Agent Loop is responsible for sending user messages to the Claude model, parsing the tool-call instructions returned by the model, executing tool operations, and feeding the results back to the model for iterative execution.
65%UnverifiedThe "Loop" in Loop Engineering and the "Loop" in Agent Loop are not the same concept: the former is a loop of multiple Skill calls and multi-Agent collaboration, while the latter is a loop of tool calls and reasoning.
60%UnverifiedIn multi-step tasks, small errors at individual steps accumulate progressively into a snowball effect, where errors in early steps contaminate the input context of subsequent steps
60%UnverifiedA typical Agent execution loop includes perception, reasoning, action, and observation, forming the ReAct (Reasoning + Acting) pattern
60%UnverifiedThe project uses an Agent loop (ReAct architecture) to allow the LLM to autonomously determine the next action based on the current page state, rather than executing pre-set scripts.
60%UnverifiedCommon engineering means of terminating an agent include maximum step/maximum round limits, timeout budgets, consecutive-failure counters, and failure-signal detection based on rules or model judgment.
50%UnverifiedWhen asked to 'execute at most ten steps', the model sometimes executes thirty steps; this is an inherent limitation of current models, and the reliable approach is to enforce the maximum number of steps, timeouts, and token or cost budgets at the code level of the run loop.
50%UnverifiedWhen a limit is triggered, execution should stop and return the partial results already completed along with a concise explanation; a justified partial answer is far better than an Agent that loops infinitely until killed externally.
50%UnverifiedDefining the state machine before writing any code allows you to consider boundary cases in a structured way, including which events can be triggered in which state of the loop, and which terminal states can cover these boundary cases.
50%UnverifiedIn a cloud Agent architecture, the Agent Loop does not run inside the sandbox; the Agent and the sandbox are separate.
50%