{"record":{"id":"e53a30bb64c417c9","repo":"unslothai/unsloth","slug":"the-model-reached-the-max-tokens-limit-before-prod","errorCode":null,"errorMessage":"The model reached the Max Tokens limit before producing a final answer. Increase Max Tokens or disable thinking, then retry.","messagePattern":"The model reached the Max Tokens limit before producing a final answer\\. Increase Max Tokens or disable thinking, then retry\\.","errorType":"exception","errorClass":"GenerationLengthError","httpStatus":null,"severity":"error","filePath":"studio/frontend/src/features/chat/api/chat-api.ts","lineNumber":1336,"sourceCode":"  }\n\n  const reader = response.body.getReader();\n  const decoder = new TextDecoder();\n  let buffer = \"\";\n  let completed = false;\n  // EOF without `[DONE]` or a finish_reason chunk means the stream was cut mid-generation.\n  let sawTerminalSignal = false;\n  let terminalFinishReason: string | null = null;\n  let sawAssistantContent = false;\n  let sawReasoningContent = false;\n\n  const throwIfReasoningOnlyLength = () => {\n    if (\n      terminalFinishReason === \"length\" &&\n      sawReasoningContent &&\n      !sawAssistantContent\n    ) {\n      throw new GenerationLengthError();\n    }\n  };\n\n  try {\n    while (true) {\n      const { done, value } = await reader.read();\n      if (done) {\n        completed = true;\n        if (!sawTerminalSignal) {\n          throw new StreamInterruptedError();\n        }\n        throwIfReasoningOnlyLength();\n        break;\n      }\n\n      buffer += decoder.decode(value, { stream: true });\n\n      let separatorIndex = buffer.search(/\\r?\\n\\r?\\n/);","sourceCodeStart":1318,"sourceCodeEnd":1354,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/frontend/src/features/chat/api/chat-api.ts#L1318-L1354","documentation":"GenerationLengthError is thrown when the stream terminated with finish_reason === \"length\" (Max Tokens hit) and the model emitted reasoning content but never produced any assistant-facing text — i.e. the entire token budget was consumed by thinking. The message tells the user to raise Max Tokens or disable thinking. It is a typed error class (chat-api.ts:82) so the UI can offer a targeted retry action.","triggerScenarios":"A reasoning model with a low max_tokens setting: the reasoning channel fills the budget, finish_reason becomes 'length', sawAssistantContent stays false, and throwIfReasoningOnlyLength() fires at stream end (either [DONE] or clean EOF).","commonSituations":"Reasoning models (o-series / thinking modes) with max_tokens copied from a non-reasoning config; long chains of thought eating a 1-2k budget; users enabling thinking on a plan/model with a tight output cap.","solutions":["Increase the Max Tokens setting for the request/model.","Disable thinking/reasoning mode for this generation.","If both are fixed by policy, surface the typed error and let the user retry with adjusted settings rather than retrying blindly."],"exampleFix":"// before\npayload.max_tokens = 1024; // reasoning eats it all\n\n// after\npayload.max_tokens = 8192;\n// or: payload.reasoning_effort = 'none';","handlingStrategy":"validation","validationCode":"// Before sending: budget for reasoning overhead\nconst isReasoning = modelSupportsThinking(payload.model);\nif (isReasoning && (payload.max_tokens ?? 0) < 4096) {\n  payload = { ...payload, max_tokens: Math.max(payload.max_tokens ?? 0, 4096) };\n}","typeGuard":"export function isGenerationLengthError(e: unknown): e is GenerationLengthError {\n  return e instanceof GenerationLengthError;\n}","tryCatchPattern":"try { for await (const c of stream) render(c); }\ncatch (e) {\n  if (isGenerationLengthError(e)) offerRetryWithHigherLimit();\n  else throw e;\n}","preventionTips":["Scale max_tokens with reasoning effort.","Offer a UI toggle to disable thinking when limits are tight.","Never auto-retry a length-limited generation with the same settings."],"tags":["streaming","max-tokens","reasoning","generation"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}