# Agent loop

> An agent loop is the cycle an AI agent repeats: a model step, a tool result, and another model step, until a stop.

A cycle of model step, tool result, and another model step, until a stop.

Track: Agents
Access: free
Card: https://buildsparkai.com/agent-loop
Plain text: https://buildsparkai.com/agent-loop.md
Last checked: 2026-10-18

## What it is

An AI agent repeats a model step until a stop. OpenAI's running-agents guide says one run calls the model, executes any tool calls and continues, and returns a result when the model produced a final answer with no more tool work. Anthropic's tool-use guide says your application drives that loop for client tools: the model asks, you execute, you send the result back, and you repeat while the stop reason is tool use. Gemini's function calling guide says the same handoff can repeat over multiple turns, and that the model does not execute the function itself.

## Why it matters

**Cost.** Each continuation is another model request. Anthropic's tool-use guide says every client tool call is at least one extra round trip, and that this overhead can exceed the work on a lightweight task. OpenAI's running-agents guide lists a max-turn limit among the failures that stop a run. This card does not add a price.

**Quality.** The next model step can use a tool result only after you send that result back. OpenAI's guide says to return a result when the model produced a final answer with no more tool work. Anthropic lists max tokens as a stop reason. A stop for that reason is not the same as a finished answer.

**Risk.** A loop with no stop keeps sending requests. Anthropic's server-side tool loop has an iteration limit and can pause instead of finishing. A tool result is text the next model step will read, including text you did not write.

## Try today

Pick one task that needs one lookup. Write the stop you will accept: a final answer, or a turn limit. Run it once and read the tool result before you read the final sentence.

## Common mistake

Treating the first model reply as the finished task when that reply is a tool request. Anthropic's guide says a tool-use stop means you still have to execute the tool and send the result back.

## Self-check

The model reply asks for a tool and does not answer the user. What is that reply?

A model step that is waiting for a tool result

## Practice and deep

Coming soon. One task, one tool, and the stop you chose, written down before you run it.

Coming soon. How OpenAI's runner and Anthropic's client-tool loop each decide the run is finished.

## Sources

- [OpenAI, Running agents](https://developers.openai.com/api/docs/guides/agents/running-agents)
- [Anthropic, How tool use works](https://platform.claude.com/docs/en/agents-and-tools/tool-use/how-tool-use-works)
- [Gemini API, Function calling](https://ai.google.dev/gemini-api/docs/function-calling)

Opened 2026-10-18. Each page is vendor documentation. The card does not add a rate or a usage count.
