# stop_reason पढ़ना — Agent Loops, Stop Conditions और Token Budgets

Source: https://www.geekswithgeeks.com/hi/agent-loops/loop-stop-reasons

> आम stop reasons सही ढंग से सँभालें: बारी का अंत, tool उपयोग, token सीमा और stop sequence।

## Model क्यों रुका

हर response बताता है कि generation क्यों रुका। Claude Messages API में आम मान हैं `end_turn` (उसने उत्तर पूरा किया), `tool_use` (उसे tool चलवाना है), `max_tokens` (उत्तर के बीच आपकी output सीमा पूरी हो गई) और `stop_sequence` (उसने वह sequence बनाई जहाँ आपने रुकने को कहा था)। आपका loop हर एक को जानबूझकर सँभाले, संयोग से नहीं।

## हर कारण को सँभालना

`max_tokens` वाला मामला अहम है: उत्तर या tool call कट सकता है, इसलिए reply को पूरा मान लेना सुरक्षित नहीं।

```python
match reply.stop_reason:
    case "end_turn":
        return final_text(reply)
    case "tool_use":
        messages.append(run_tools(reply))
    case "max_tokens":
        raise OutputTruncated("reply cut off; raise max_tokens or ask for less")
    case _:
        raise UnexpectedStop(reply.stop_reason)
```

## end_turn मानकर न चलें

जो loop हर non-tool reply को पूरा उत्तर मानता है, वह `max_tokens` आने पर आधा वाक्य ख़ुशी से लौटा देगा। कारण स्पष्ट रूप से जाँचें।

**Quiz:** stop_reason "max_tokens" आपको क्या बताता है?

- [ ] Reply सामान्य रूप से पूरी हुई
- [x] Reply आपकी output सीमा पर कट सकती है
- [ ] Model ने काम से मना किया
- [ ] API key expire हो गई

*Answer:* Reply आपकी output सीमा पर कट सकती है. इसका मतलब है कि generation output सीमा पर पहुँचा, इसलिए सामग्री अधूरी हो सकती है।
