Skip to content

agent loop

An agent loop is the repeating cycle in which an AI agent gathers context, lets the model choose the next step, carries that step out, and feeds the result back until the task is finished. Each pass starts with the harness assembling what the model needs, including the request, any instructions, and the results of earlier steps.

The LLM then either answers or asks for a tool call, such as reading a file or running a test. The harness performs the call and adds the output to the conversation for the next pass.

Step through a short agent run below to see the model request one tool call per pass, each result carry forward into the next pass, and the loop stop once the goal is met:

Interactive diagram — enable JavaScript to view.

The loop ends when the model decides the goal is met, a step limit or budget runs out, or a person stops it. Every pass carries the growing history forward, so long runs consume more tokens and can fill the context window. The 2022 ReAct paper described this interleaving of reasoning and action. The loop is what separates an AI agent from a single prompt and response.

LangGraph: Build Stateful AI Agents in Python

Course

Building an AI Agent With LangGraph and Python

Learn LangGraph in Python with this hands-on video course and build a stateful AI agent using state graphs, nodes, and conditional edges.

intermediate ai data-science

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Sarah Jane Ross • Updated Dec. 1, 2026