A reply that looks complete may not be. finish_reason is the one field that tells you whether the model ended on its own terms.
How it works
Each item in choices carries a finish_reason. OpenAI’s reference lists these values:
| Value | Meaning (OpenAI) |
|---|---|
stop |
Natural stop point, or a provided stop sequence was hit |
length |
“The maximum number of tokens specified in the request was reached” |
tool_calls |
The model called a tool |
content_filter |
“Content was omitted due to a flag from our content filters” |
function_call |
Older, deprecated form of a function call |
Anthropic’s Messages API uses stop_reason with its own values, including end_turn, max_tokens, stop_sequence, tool_use, pause_turn, refusal and model_context_window_exceeded. When a stop sequence fires, Anthropic also returns which one in stop_sequence.
choice = r.choices[0]
if choice.finish_reason == "length":
raise RuntimeError("cut off: raise max_tokens and resend")
if choice.finish_reason == "tool_calls":
run_tools(choice.message.tool_calls)
Why it matters
- Truncated JSON. A
lengthreply is incomplete. Closing the brackets makes it parse but loses data. The invalid JSON post covers this. - Agent loops.
tool_callsmeans “run the tools and call me again”, not “done”. - Caching. Only cache replies that ended with
stop. A cut-off answer cached for a week looks like a model bug.
On BroadNet Inference
BroadNet’s API reference does not document finish_reason values, so log what you get before you rely on them. It does document the main cause of length surprises: DeepSeek V4.1 Flash, GLM 5.3 and Qwen3.8 27B reason before they answer, and the reasoning tokens count toward max_tokens. BroadNet says that with a small limit “the reasoning can use the whole budget and the answer comes back empty.” Leave 1,000 tokens or more, or set reasoning_effort to low. The empty response guide walks through the fix.
Common mistakes
- Reading
message.contentwithout checking finish_reason first. - Treating an empty answer as a model failure when the reasoning used the whole budget.