Agent that never terminates
“Your agent sometimes loops indefinitely or runs far longer than expected. What causes this and how do you prevent it?”
What this tests
- Knowledge of common non-termination causes
- Hard limits: iterations, tokens, time, cost
- Detecting repeated states and tool calls
- Designing clear completion criteria
Answers by level
Read the beginner answer first and notice what is missing.
Causes I see most: no clear completion criterion (the goal is vague so the model keeps "checking"); tool errors misread as progress (the same failing call retried by the model); oscillation between two states (search → wrong result → refine → same result); unbounded sub-tasks (each step spawns more work); and context growth that makes the model lose track of what it already did. See Budgets, Limits and Termination and The Agent Loop.
Prevention has two layers. Hard limits in code: max iterations sized from the loop-length distribution of successful runs (if p99 is 12 steps, cap at 20, not 100), a token and cost budget, a wall-clock timeout, and a cap on retries per tool. Structural fixes: explicit completion signals (a finish tool with a required result schema), state tracking that detects repeated identical tool calls and injects "you already tried this", and returning structured errors so the model does not treat failures as partial success.
When a limit fires, the run ends with a clear status and a partial result, and the event is counted; a rising cap-hit rate is an alert, not noise.
Green flags · Red flags
- Names concrete causes: vague goal, misread errors, oscillation, context growth
- Hard limits on iterations, tokens, time, cost, set from data
- Repeated-state or duplicate-call detection
- Explicit completion tool with a result schema
- Cap-hit rate monitored as a health metric
- Only a generous iteration cap
- Relies on prompt instructions to finish
- No metrics on loop length
- No partial-result handling when a limit fires