During an incident where a Claude-powered agent is 'looping' (calling the same tool repeatedly with the same arguments), which diagnostic metric from the Messages API response is most useful for identifying the failure to progress?
The stop_reason tells you why the model stopped generating. In a loop, it will repeatedly stop with 'tool_use'. By tracking the tool name and arguments associated with this reason over several turns, developers can programmatically detect and break infinite loops that waste tokens and time.
Why this answer
When an agent is stuck in a loop, it continues to consume tokens without reaching an 'end_turn' stop_reason. The most critical field to monitor is the 'stop_reason'. If the stop_reason is consistently 'tool_use' for the same tool across multiple turns, it indicates the model is not satisfied with the results or is failing to reason about the next step.
Comparing the 'usage' (input_tokens) across these turns will also show a growing context as the conversation history (including the repetitive tool calls) is sent back to the model.
Exam trap
Looking for a 'loop_detected' flag or error code, which the API does not provide.