---
title: "Agent loop"
type: "glossary term"
url: "https://aifordevs.org/glossary#agent-loop"
markdown: "https://aifordevs.org/glossary/agent-loop.md"
publisher: "AI for Devs"
license: "https://aifordevs.org/reuse"
attribution: "Credit AI for Devs and link to https://aifordevs.org/glossary#agent-loop"
---

# Agent loop

An agent loop is the code that sends the conversation to a model, runs any tool calls in the response, appends the results, and calls the model again. The loop ends when a response contains no tool calls or a limit is reached.

A turn is one pass: model output, then tool execution if the output asked for it. Without a cap, the loop runs until the model returns text with no tool calls, so set a turn limit and a spend limit in production. In the Claude Agent SDK those limits are max_turns and max_budget_usd, and as of November 8, 2026, both default to no limit.

## Example

A bug fix takes four turns: run the tests, read two files, edit and rerun, then a text-only reply. With max_turns=2 the run stops before the edit and returns error_max_turns.

## Source

[Claude Agent SDK docs: How the agent loop works](https://code.claude.com/docs/en/agent-sdk/agent-loop)

## Related terms

- [Agent](https://aifordevs.org/glossary#agent)
- [Agentic AI](https://aifordevs.org/glossary#agentic-ai)
- [Tool calling](https://aifordevs.org/glossary#tool-calling)

## Read the article

[What is an AI agent? The loop in code, and where it fails](https://aifordevs.org/articles/what-is-an-ai-agent)

## Cite this term

AI for Devs. "Agent loop." https://aifordevs.org/glossary#agent-loop

- Page: https://aifordevs.org/glossary#agent-loop
- Markdown: https://aifordevs.org/glossary/agent-loop.md
- Reuse: Quote or summarize with a link to this page. Terms: https://aifordevs.org/reuse
- Publisher: AI for Devs
