Inference, Serving & Production·Question 14 of 76Basic

Your production code reads the response text but never checks finish_reason (or stop_reason). Why is that a bug, and what should you do with each stop reason?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Because a response can end for reasons other than "the model was done", and the text alone will not tell you. Every major API reports why generation stopped: finish_reason in OpenAI's Chat Completions, a status plus incomplete details in its Responses API, stop_reason on Anthropic, finishReason on Gemini. Ignoring it means shipping truncated or filtered output as if it were complete.

Full worked answer

Because a response can end for reasons other than "the model was done", and the text alone will not tell you. Every major API reports why generation stopped: finish_reason in OpenAI's Chat Completions, a status plus incomplete details in its Responses API, stop_reason on Anthropic, finishReason on Gemini. Ignoring it means shipping truncated or filtered output as if it were complete.

Each value maps to a distinct action:

  • Natural stop (stop, end_turn): the model finished. Validate and use it.
  • Length (length, max_tokens): truncated at the cap. Prose ends mid-sentence. JSON or a tool call is unparseable, and constrained decoding does not save you, because the grammar cannot close the object once the budget is gone. Raise the cap for that feature, ask for a shorter format, or continue the generation, but never parse it as complete. On reasoning models, thinking tokens usually count against the same budget, so a request can hit the cap before writing any visible answer.
  • Stop sequence (stop_sequence): one of your stop strings fired. Expected if you designed it, a bug if that string can legitimately appear in content.
  • Tool use (tool_calls, tool_use): the model wants a tool run. Agent loops should branch on this field, not on whether the text looks empty.
  • Safety or refusal (content_filter, refusal): the provider blocked the output or the model declined. Show a deliberate UX, do not blindly retry the same input, and log it for review.

Production habits that go with it:

  • Log the stop reason on every call and chart the rate of length stops per feature. A jump after a prompt or model change is an early sign of verbosity creep, and it shows up before users complain about cut-off answers.
  • Alert on a rising refusal rate per feature, since it often means a prompt change tripped a policy boundary.
  • In streams, the reason arrives in the final event, so code that aborts early or drops the last event silently loses it.
  • Treat an unknown value as an error. Providers add new ones, and a default branch that assumes success is how a new stop reason ships truncated data.

What a good answer covers

  • Because a response can end for reasons other than "the model was done", and the text alone will not tell you
  • Every major API reports why generation stopped
  • Ignoring it means shipping truncated or filtered output as if it were complete
  • Natural stop (stop, end_turn): the model finished
  • Length (length, max_tokens): truncated at the cap

Key terms: Natural stop · Length · Stop sequence · Tool use · Safety or refusal

Interviewer follow-ups to expect

  1. How would you safely continue a generation that was truncated in the middle of a JSON object?

  2. Your length-stop rate doubled after a model upgrade with no prompt change: what happened, and what do you change?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep