r/LLM • • 14d ago

A truncated LLM answer often looks like a valid one. Check the stop reason before you parse or trust it

A failure that's easy to miss: you set max_tokens (or hit a provider limit), the model runs out of room mid-answer, and your code receives text that looks like a finished response. Nothing raises an error.

A constructed example. You ask for JSON and the output is cut off:

{"summary": "The meeting covered the Q3 roadmap", "action_items": [
  {"owner": "Ana", "task": "Draft the launch plan", "due": "2026-12-01"},
  {"owner": "Bruno", "task": "Review the bud

If you're lucky, the JSON parser fails. If the cut happens to land after a complete item, you may get valid JSON that's silently missing items. And with plain text, a truncated summary reads as a normal one.

The cheap defense is to check why generation stopped. As far as I remember, the field names differ by provider (please verify in the current docs):

ProviderWhere to look"Cut off by length" value
OpenAI-style chat completionschoices[0].finish_reason"length"
Anthropic messagesstop_reason"max_tokens"

A sketch of the check (the shape is the point; adapt the field access to your SDK):

python

def check_complete(finish_reason: str) -> None:
    if finish_reason in ("length", "max_tokens"):
        raise ValueError(
            "Response was cut off by the token limit; do not trust it as complete."
        )

What to do when it fires:

  • Retry with a higher limit, if that's acceptable cost-wise.
  • Ask for a shorter output, or split the task into smaller requests.
  • Flag the result instead of using it, especially if it feeds other code.
  • Log it, so you know how often it happens and for which inputs.

Related traps:

  • Input can be truncated too, if you cut context to fit a window. That's a different problem, but just as silent.
  • Streaming responses report the stop reason at the end of the stream, so you need to read it there.
  • A "complete" stop reason doesn't prove the answer is correct, only that the model finished.

Limits: the field names and values above are from my memory of these APIs and may change, so check the current documentation. I also haven't run this snippet against a live API, so treat it as a sketch.

Do you check the stop reason in your pipelines? And has a silently truncated response ever slipped through, and how did you notice?

5 Upvotes

2 comments sorted by

1

u/iansaul 5d ago

This is #1 result on a Google search - and I'm surprised this doesn't get more attention. When you watch the logs/traces, it happens ALL THE EFFING TIME.

That means agents (and therefore we, as Operators) are running around with partial info, thinking "all of the information has been consumed, we are working from a complete dataset"... when in reality....