---
title: "finish_reason length Meaning: 4 Causes of Cut-Off Replies + Fix"
description: "A reply that stops mid-sentence usually means finish_reason is length (max_tokens on Anthropic). See the values, 4 causes, and Python continuation code."
url: https://cdnping.org/en/blog/finish-reason-length-truncated
language: en
published: 2026-11-29
category: "Troubleshooting"
tags: ["finish_reason","max_tokens","truncated response","LLM API","continuation"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# finish_reason length Meaning: 4 Causes of Cut-Off Replies + Fix

If a reply stops halfway through, check the stop-reason field in the response first. On OpenAI-compatible APIs it is `finish_reason`, with the value `length`. On the Anthropic API it is `stop_reason`, with the value `max_tokens`. Both mean the same thing: the output hit the token limit, so the model didn't finish. It was cut off. The fixes are to raise the limit, shorten what you ask for, or have the model continue from where it stopped.

If your `finish_reason` is `length`, you can work through the sections below as written.

## What values can finish_reason and stop_reason take?

Field names and values differ between APIs. The table lists the common ones. For the full list and any newly added values, check each vendor's official docs.

| API | Field | Value | Meaning |
| --- | --- | --- | --- |
| OpenAI Chat Completions (and compatible APIs) | `finish_reason` | `stop` | Finished naturally, or hit a configured stop sequence |
| | | `length` | Hit the token limit and was truncated |
| | | `tool_calls` | The model wants to call a tool |
| | | `content_filter` | Blocked by a content filtering policy |
| Anthropic Messages | `stop_reason` | `end_turn` | Finished naturally |
| | | `max_tokens` | Hit the `max_tokens` limit and was truncated |
| | | `stop_sequence` | Hit a custom stop sequence |
| | | `tool_use` | The model wants to call a tool |

OpenAI's Responses API doesn't use `finish_reason`. When the response `status` is `incomplete`, it explains why in `incomplete_details.reason`, and truncation shows up as `max_output_tokens`. Gemini uses `finishReason`, and truncation is `MAX_TOKENS`. Confirm exact field names in the official docs.

Note that some compatible APIs pass the upstream value through unchanged, while others convert it to the OpenAI convention. The same model can return different values through different gateways, so it's safer to check for both forms in your code.

## Why do responses get truncated?

### 1. max_tokens is set too low

This is the most common cause. Some clients and SDKs ship with a default, and if you didn't set one, you may be getting a small number. On Anthropic, `max_tokens` is a required parameter, so if you typed in a small value casually, any long answer will be cut off. See our other article, [how to set max_tokens](/en/blog/max-tokens-how-to-set).

### 2. A reasoning model spent the budget on thinking

For models with a "thinking" mode, the tokens produced while thinking usually count toward the output limit too. If the limit isn't large enough, you can end up with a very short or even empty reply and `finish_reason: length`, because the whole budget went to thinking. The parameter name may also differ for these models. For example, OpenAI's newer API uses `max_completion_tokens`. Check the official docs for details.

### 3. The input filled the context window

Input plus output must fit inside the context window. If the conversation history is long, there is little room left for output, and even a very large `max_tokens` doesn't help. Some APIs return an error outright. Others stop partway with `length`.

### 4. A gateway or client imposes its own limit

If your request goes through a relay gateway, an IDE plugin, or a chat client, it may add its own cap. You didn't set one in your code, but the request actually carries a number. Capture the request body that was really sent to confirm.

## How do you tell truncation from a dropped stream?

They look alike but have different causes:

- Truncation: the response ends normally, the HTTP status is 200, and the last chunk carries `finish_reason: length`.
- Dropped stream: the connection broke midway. There is usually no stop reason, and the client may report a connection reset, a timeout, or something like `stream disconnected`.

If the response has no stop reason at all, or the stream always cuts off at a fixed point in time, look at timeouts and buffering in your proxy, Nginx, or CDN. See [how to debug SSE streaming that drops](/en/blog/llm-sse-streaming-disconnect-proxy). If 4xx or 5xx status codes show up too, compare against the [LLM API error code cheatsheet](/en/blog/ai-api-error-codes-400-to-529-cheatsheet).

You can use cdnping's [API status page](/en/status) to see whether the official endpoints had recent problems, but truncation has nothing to do with a service outage. It is caused by parameters.

## The fix: adjust parameters and continue

### Option 1: Raise the limit, after checking how much was used

The `usage` field in the response reports actual consumption. First check whether the output tokens exactly equal the limit you set. If so, you did hit the cap. Then raise the limit to a reasonable number, but don't exceed the model's maximum single-response output as documented officially.

### Option 2: Split the task

Have the model write one chapter, one file, or one function at a time instead of the whole thing. When you need structured output, ask for an outline first and then expand each item.

### Option 3: Detect length and continue automatically

Here is a Python example for OpenAI-compatible APIs. The idea is to put the generated text back as an assistant message and append a "continue" prompt. Replace the model name and base URL with your own.

```python
from openai import OpenAI

client = OpenAI(base_url="your API base URL", api_key="your API key")

def chat_full(messages, model, max_tokens=1024, max_rounds=5):
    parts = []
    for _ in range(max_rounds):
        resp = client.chat.completions.create(
            model=model,
            messages=messages,
            max_tokens=max_tokens,
        )
        choice = resp.choices[0]
        text = choice.message.content or ""
        parts.append(text)
        if choice.finish_reason != "length":
            break
        messages = messages + [
            {"role": "assistant", "content": text},
            {"role": "user", "content": "Continue from where the last sentence stopped. Do not repeat what you already wrote."},
        ]
    return "".join(parts)
```

On the Anthropic API, change the check to `stop_reason == "max_tokens"`. The parameter is `max_tokens`, and the approach is the same.

Continuation has a few limitations:

- Every round counts the input tokens again, so the cost adds up.
- The model may repeat or skip a sentence at the seam. Strict formats such as code and JSON are especially prone to this, so re-validate the format after stitching.
- Always cap `max_rounds`, so a loop can't burn through your quota.

For JSON, a more reliable approach is to make each output smaller rather than continue and stitch.

## Checklist

1. Print the stop reason and `usage` from the response and confirm it is `length` or `max_tokens`.
2. Check the limit the request actually carries, including whether the client or gateway rewrote it.
3. With a reasoning model, leave enough budget for thinking.
4. Estimate the input length and make sure history hasn't filled the window.
5. Split the task if you can. If you must continue, add a round cap and validate the format.
6. If there is no stop reason and the output cuts off at a fixed time, move on to checking the proxy and timeouts.

## For beginners: what if Claude Code or Codex gets truncated?

For these command-line tools, the tool itself controls the limit, and you usually don't need to pass `max_tokens` by hand. If truncation happens often, look in the tool's docs for settings on output length or thinking budget. The names vary by tool, so check each one's official documentation.

If you don't have an international credit card, or don't have the matching official account, you could also look at AGI Plan. It is a multi-provider LLM aggregation gateway sold by monthly subscription, billed in RMB (Chinese yuan), with no international credit card required. To keep using tools like Claude Code and Codex, you only change one endpoint URL. The service states plainly that it is not an officially authorized channel, and some models are marked "overseas only"; see the [models page](https://agiplan.dev/models) for specifics. AGI Plan is a commercial partner of cdnping. For setup steps, see the [AGI Plan getting-started page](https://agiplan.dev/startup).

## Next step

Start by printing `finish_reason` or `stop_reason` in your own code, which takes a few minutes. Once you confirm it is `length`, raise the limit and split the task in the order above. If there is no stop reason at all, go investigate the network path. While you're debugging, it also helps to look at [how to read output speed in tokens/s](/en/blog/llm-output-speed-tokens-per-second), so you can estimate how long a long output takes and set sensible timeouts.
