{"record":{"id":"bd6807448fb594b0","repo":"Mintplex-Labs/anything-llm","slug":"no-token-context-limit-was-set-bd6807","errorCode":null,"errorMessage":"No token context limit was set.","messagePattern":"No token context limit was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/liteLLM/index.js","lineNumber":61,"sourceCode":"    if (!contextTexts || !contextTexts.length) return \"\";\n    return (\n      \"\\nContext:\\n\" +\n      contextTexts\n        .map((text, i) => {\n          return `[CONTEXT ${i}]:\\n${text}\\n[END CONTEXT ${i}]\\n\\n`;\n        })\n        .join(\"\")\n    );\n  }\n\n  streamingEnabled() {\n    return \"streamGetChatCompletion\" in this;\n  }\n\n  static promptWindowLimit(_modelName) {\n    const limit = process.env.LITE_LLM_MODEL_TOKEN_LIMIT || 4096;\n    if (!limit || isNaN(Number(limit)))\n      throw new Error(\"No token context limit was set.\");\n    return Number(limit);\n  }\n\n  // Ensure the user set a value for the token limit\n  // and if undefined - assume 4096 window.\n  promptWindowLimit() {\n    const limit = process.env.LITE_LLM_MODEL_TOKEN_LIMIT || 4096;\n    if (!limit || isNaN(Number(limit)))\n      throw new Error(\"No token context limit was set.\");\n    return Number(limit);\n  }\n\n  // Short circuit since we have no idea if the model is valid or not\n  // in pre-flight for generic endpoints\n  isValidChatCompletionModel(_modelName = \"\") {\n    return true;\n  }\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/liteLLM/index.js#L43-L79","documentation":"Thrown by the static promptWindowLimit on LiteLLM when LITE_LLM_MODEL_TOKEN_LIMIT is set to a truthy but non-numeric value. The `|| 4096` default absorbs unset/empty values, so this only fires on explicit misconfiguration like setting it to 'auto' or 'none'. The static method is used for pre-flight context calculations.","triggerScenarios":"Calling `LiteLLM.promptWindowLimit(modelName)` when process.env.LITE_LLM_MODEL_TOKEN_LIMIT is a non-numeric string. Numeric strings, unset, and empty are handled by the default or pass the isNaN check.","commonSituations":"An admin sets the token limit to a descriptive word thinking it's a label. Copy-pasting a configuration snippet from documentation that uses a placeholder. Setting the value to a float-formatted string like '4.096e3' which actually works (Number('4.096e3') = 4096) but is confusing.","solutions":["Set LITE_LLM_MODEL_TOKEN_LIMIT to a plain integer in .env.","Remove the variable to use the default 4096.","If you need different limits per model, set the variable dynamically or handle it in the calling code."],"exampleFix":"// before\nLITE_LLM_MODEL_TOKEN_LIMIT='max'\n\n// after\nLITE_LLM_MODEL_TOKEN_LIMIT=8192","handlingStrategy":"validation","validationCode":"function validateTokenLimit(envVar, defaultLimit = 4096) {\n  const raw = process.env[envVar];\n  if (!raw) return defaultLimit;\n  const num = Number(raw);\n  if (isNaN(num)) {\n    throw new Error(`${envVar}=\"${raw}\" must be a number or unset for default ${defaultLimit}.`);\n  }\n  return num;\n}\n\nconst limit = validateTokenLimit('LITE_LLM_MODEL_TOKEN_LIMIT');","typeGuard":"/** @returns {value is number} */\nfunction isValidTokenLimit(value) {\n  return typeof value === 'number' && !isNaN(value) && value > 0;\n}","tryCatchPattern":"let limit;\ntry {\n  limit = LiteLLM.promptWindowLimit();\n} catch (e) {\n  if (e.message.includes('token context limit')) {\n    console.warn('LITE_LLM_MODEL_TOKEN_LIMIT invalid, defaulting to 4096');\n    limit = 4096;\n  } else {\n    throw e;\n  }\n}","preventionTips":["Use bare integers for token-limit env vars.","Run a startup validator that checks all *_TOKEN_LIMIT vars are numeric.","If unsure of the limit, omit the variable to use the 4096 default."],"tags":["litellm","configuration","env-var","token-limit","pre-flight"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}