Agents

Agent loop

An agent loop repeatedly observes state, chooses an action, and evaluates the result.

Send context to modelModel returns tool call or answerRun tool, add the resultuntil answer or limit
An agent loop repeatedly observes state, chooses an action, and evaluates the result. Values shown in the worked visual are illustrative unless the labels state otherwise.

How it works

The loop converts a one-shot model into a stateful process. Budgets, idempotency, and termination checks prevent uncontrolled repetition.

A concrete example

Illustrative: cap execution at 12 turns or $0.50, whichever occurs first.

Do not confuse it with Retry loop

a retry repeats an operation; an agent loop can choose a different action from new evidence.

Study the full lesson