Agent loop
The core while-loop behind an AI agent: send context and tool definitions to the model, execute any tool call it requests, feed the result back and repeat until a stop condition fires.

Also called: Agentic loop, Tool-use loop.
For: Beginner · Builders · Must know
Why it matters
Understanding the loop demystifies agents: there is no magic autonomy, just repeated model calls bounded by tools, budgets and exit checks.
In practice
A bug-fix agent runs a loop capped at 12 turns and $1.50 in token spend. Turn 1 reads the failing test, turns 2–3 edit the handler, turn 4 runs npm test and sees a green exit code 0, and turn 5 emits the final summary with no tool calls, exiting the loop cleanly.
Watch for
Without a hard step limit, spend cap and duplicate-call detector, a stuck model will retry the same failing command until it burns your budget or hits rate limits.
How it compares
- Agent harness: The agent loop is the core model-tool cycle; the agent harness is the complete surrounding system — state persistence, permissions, compaction, sandboxing and verification.
- Workflow: A workflow follows fixed code-defined branches between steps; an agent loop lets the model decide which tool to call next until its goal or stop cap is met.
Related terms
Agent · Agent harness · Tool calling · ReAct
Try it here
AI agents, minus the hype · Build your first agent
Sources
Use in an AI agent: connect MCP at https://www.theglitch.ai/mcp and call get_glossary_term with agent-loop, or read the Markdown edition.
Definition updated 2026-11-28.