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.
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.
Related Resources
Tutorial
How to Debug Python Code With an AI Agent
Learn AI debugging by pairing with an AI coding agent: reproduce the bug with a failing test, give your agent context, then verify the fix.
For additional information on related topics, take a look at the following resources:
- LangGraph Tutorial: Build Stateful AI Agents in Python (Tutorial)
- Pydantic AI: Build Type-Safe LLM Agents in Python (Tutorial)
- AI Coding Agents Guide: A Map of the Four Workflow Types (Tutorial)
- How to Debug Python Code With an AI Agent (Quiz)
- Building an AI Agent With LangGraph and Python (Course)
- LangGraph: Build Stateful AI Agents in Python (Quiz)
- Building Type-Safe LLM Agents With Pydantic AI (Course)
- Pydantic AI: Build Type-Safe LLM Agents in Python (Quiz)
- AI Coding Agents Guide: A Map of the Four Workflow Types (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Sarah Jane Ross • Updated Oct. 11, 2026