{"record":{"id":"e0f6e6afcf75405c","repo":"Mintplex-Labs/anything-llm","slug":"no-token-context-limit-was-set","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/genericOpenAi/index.js","lineNumber":102,"sourceCode":"    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    if (process.env.GENERIC_OPENAI_STREAMING_DISABLED === \"true\") return false;\n    return \"streamGetChatCompletion\" in this;\n  }\n\n  static promptWindowLimit(_modelName) {\n    const limit = process.env.GENERIC_OPEN_AI_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.GENERIC_OPEN_AI_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":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/3aec848f2885144aa8f1e53b9731a04310d5d558/server/utils/AiProviders/genericOpenAi/index.js#L84-L120","documentation":"The static promptWindowLimit reads GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT (defaulting the value to 4096 when the env var is empty/unset) and throws when Number(limit) is NaN — i.e. the env var is set to a non-numeric string. Because the || fallback only substitutes for falsy values, a garbage non-empty value like '4k' or '8192 ' passes into the NaN check and throws. This static variant runs during provider-selection/class-level queries such as context-window sizing before an instance exists.","triggerScenarios":"GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT set to something non-numeric: '4k', '16k tokens', '8,192', or a value with stray characters. Note: whitespace-only or numeric-with-comma strings all produce NaN; plain '0' or '' do NOT throw (they fall back or return 0).","commonSituations":"User copies a token limit styled like marketing copy ('128k context') into the env var; values pasted with commas or units; shell quoting artifacts around the number.","solutions":["Set GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT to a bare integer, e.g. 8192 (no 'k', commas, or units), and restart","Or remove the variable entirely — the code then defaults to 4096","Check for invisible characters/quotes: print it with node -e \"console.log(JSON.stringify(process.env.GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT))\""],"exampleFix":"# before (.env)\nGENERIC_OPEN_AI_MODEL_TOKEN_LIMIT=128k\n\n# after (.env)\nGENERIC_OPEN_AI_MODEL_TOKEN_LIMIT=131072","handlingStrategy":"validation","validationCode":"const raw = process.env.GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT;\nif (raw != null && raw !== \"\" && Number.isNaN(Number(raw))) {\n  throw new Error(`GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT must be numeric, got '${raw}'`);\n}\n// safe to call GenericOpenAiLLM.promptWindowLimit(modelName)","typeGuard":null,"tryCatchPattern":"try {\n  const limit = GenericOpenAiLLM.promptWindowLimit(modelName);\n} catch (err) {\n  if (err.message === \"No token context limit was set.\") {\n    return respond(\"GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT must be a plain integer like 8192.\");\n  }\n  throw err;\n}","preventionTips":["Validate numeric env vars once at boot instead of at call time","Store bare integers in env files — no 'k' suffixes, commas, or units","Print JSON.stringify(envValue) when debugging suspected invisible characters"],"tags":["generic-openai","invalid-env-value","token-limit","configuration"],"backgroundTag":"invalid-env-value","analyzedSha":"3aec848f2885144aa8f1e53b9731a04310d5d558","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}