On this page
Learning objectives
- Implement a bounded agent loop
- Separate orchestration state from model conversation
- Handle retries, repeated actions, and non-progress
Prerequisites
Tool use · Context and state · Structured outputs
Answer first
Canonical definition
An agent loop repeatedly assembles state and observations, asks a model for a next action, executes an approved action, records the result, and stops when a goal, limit, or escalation condition is reached.
Operating context
Why it matters
The loop is where a useful one-step model call becomes an adaptive system—and where cost, repeated actions, and compounding errors appear.
Reliable orchestration makes progress and failure visible outside the model transcript.
Method
Step by step
- 01
Model the state
Store goal, completed work, outstanding constraints, tool results, approvals, and terminal status in application state.
- 02
Bound every run
Set maximum steps, wall time, tokens or spend, retries, and side-effect counts.
- 03
Detect non-progress
Stop repeated tool calls, unchanged plans, circular delegation, and outputs that fail validation more than a defined number of times.
- 04
Escalate with context
When the loop stops, give a human the goal, evidence, attempted actions, current state, and recommended next decision.
Hands-on lab
Implement a bounded research loop
Let an agent search an approved corpus until it has enough evidence or reaches a step and confidence limit.
Deliverable
A loop controller, state schema, budget guard, repetition detector, trace, and escalation packet.
What breaks
Common failure modes
- F1Letting the model decide whether cost and policy limits apply
- F2Retrying the same failing action without changing state
- F3Encoding orchestration entirely inside a prompt
Beyond the demo
Production notes
- Persist state before and after side effects so a retry can determine what already happened.
- Use deterministic orchestration for policy, budgets, and state transitions; reserve model reasoning for uncertain decisions.
Further reading
Sources
Related Tenten resources