Best Practices for Building LLM Apps

Chapter 1 · Practice 4 of 4

Check why generation stopped before you trust the output

From Prompt design and structured 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 reasonWhat to do
FinishedUse the output, after validation
Hit the token limit (length)Raise the limit, ask the model to continue, or fail cleanly
Wants to call a toolRun the tool and send back the result
RefusedShow a helpful message rather than an empty box

Count each reason in your metrics.