Chapter 1 · Practice 4 of 4
Check why generation stopped before you trust the output
Why it matters
Every API response tells you why the model stopped. Code that only reads the text will happily save half a JSON object or a truncated answer, so read the stop or finish reason on every call.
| Stop reason | What to do |
|---|---|
| Finished | Use the output, after validation |
| Hit the token limit (length) | Raise the limit, ask the model to continue, or fail cleanly |
| Wants to call a tool | Run the tool and send back the result |
| Refused | Show a helpful message rather than an empty box |
Count each reason in your metrics.