How to set token limits without cutting answers short
LLM output limits can silently cut responses short even when API requests succeed. Anthropic flags truncation with stop_reason set to max_tokens, OpenAI uses finish_reason set to length, and Google’s Gemini API reports MAX_TOKENS. Applications should check these fields after every call rather than treating returned text as complete. Streaming interfaces can display incomplete answers before the final stop reason arrives.
Limits should reflect actual output lengths for each route and model, not a global default. A practical starting point is the 99th percentile of observed output length plus 20 to 50 percent headroom, with more room for structured data and long-form responses. Teams should recalculate distributions after model changes. Reasoning models also consume output budgets on hidden thinking, potentially leaving insufficient capacity for the visible answer.
A shared API wrapper can detect truncation, log it by route and model, and trigger recovery. Prose can sometimes be continued, with safeguards against repetition and runaway costs. Truncated JSON should instead be retried with a higher limit or failed visibly, never patched and passed downstream. Schema-constrained output does not prevent hard token cutoffs. Raising a cap does not itself increase charges for responses that finish early: billing depends on generated tokens. Concise prompts, route-specific limits and account-level spending alerts provide complementary cost controls.