BroadRouter
Get started

finish_reason

Updated

finish_reason is a field on each choice in a chat completion response that says why the model stopped generating. In the OpenAI format the values are stop, length, tool_calls, content_filter and the deprecated function_call. Anthropic's Messages API reports the same idea as stop_reason.

Also called: finish reason, stop reason, stop_reason

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 length reply is incomplete. Closing the brackets makes it parse but loses data. The invalid JSON post covers this.
  • Agent loops. tool_calls means “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.content without checking finish_reason first.
  • Treating an empty answer as a model failure when the reasoning used the whole budget.

Questions

What does finish_reason "length" mean?

The reply hit the token limit you set and was cut off. OpenAI describes it as "the maximum number of tokens specified in the request was reached." Raise max_tokens or ask for less; do not repair the cut-off text.

Why is finish_reason "stop" when I set a stop sequence?

In the OpenAI format, stop covers both a natural end and a matched stop sequence. Anthropic separates them into end_turn and stop_sequence.

Does BroadNet document finish_reason values?

No. BroadNet's API reference does not list finish_reason values. Log the field on your own requests and test before you branch on it.

Read as MarkdownAsk ChatGPTAsk ClaudeAsk Perplexity