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:
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
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.
For additional information on related topics, take a look at the following resources:
- LangChain Tutorial: From Prompts to RAG and AI Agents (Tutorial)
- Build an AI Agent From Scratch in Python: The Agentic Loop (Tutorial)
- Pydantic AI: Build Type-Safe LLM Agents in Python (Tutorial)
- MCP vs API Calls: Which Should You Use for Python LLM Apps? (Tutorial)
- How to Debug Python Code With an AI Agent (Tutorial)
- LangGraph Tutorial: Build Stateful AI Agents in Python (Tutorial)
- AI Coding Agents Guide: A Map of the Four Workflow Types (Tutorial)
- LangChain Tutorial: From Prompts to RAG and AI Agents (Quiz)
- Build an AI Agent From Scratch in Python (Course)
- Build an AI Agent From Scratch in Python: The Agentic Loop (Quiz)
- Building Type-Safe LLM Agents With Pydantic AI (Course)
- Pydantic AI: Build Type-Safe LLM Agents in Python (Quiz)
- MCP vs API Calls: Which Should You Use for Python LLM Apps? (Quiz)
- How to Debug Python Code With an AI Agent (Quiz)
- LangGraph: Build Stateful AI Agents in Python (Quiz)
- Exploring AI Coding Agent Workflows (Course)
- 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 Dec. 1, 2026