Your agent loop sometimes runs forever on one task. Which control belongs in your code?
Why A is the answer
A maximum turn limit with a clear exit
Your code owns the loop, so it must enforce the limit. A prompt only asks. A turn cap also gives you a defined place to report that the task was not finished.
In detail
- Your code owns the loop, so it must enforce the limit.
- A prompt can only ask.
- A turn cap also gives you a defined place to report an unfinished task.
- Record how often the limit is reached, because it shows where agents struggle.
Use case
An illustrative scenario, not a real incident.
A coding agent loops on one failing test all night. A limit of 30 turns stops it, and the app reports that the task was not finished and shows what was tried.
See it in different ways
stateDiagram-v2 [*] --> Response Response --> Complete: end_turn Response --> RunTool: tool_use Response --> CutOff: max_tokens Response --> Blocked: refusal Response --> Continue: pause_turn Continue --> Response: send content back
stateDiagram-v2 state "Running" as s0 state "Finished" as s1 state "Stopped" as s2 state "Report to the user" as s3 [*] --> s0 s0 --> s1: task complete s0 --> s2: limit reached s2 --> s3 s1 --> [*] s3 --> [*]
Key point: Your code owns the loop, so it owns the limit.
Why the other options are wrong
- B. A larger max_tokens value
- max_tokens limits each reply, not the number of turns.
- C. A request in the prompt to finish quickly
- A request does not stop a loop that has gone wrong.
- D. A higher temperature
- Temperature has no effect on how long the loop runs.

