{"record":{"id":"d78453b115f7b24a","repo":"Mintplex-Labs/anything-llm","slug":"no-token-context-limit-was-set-d78453","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/textGenWebUI/index.js","lineNumber":59,"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.TEXT_GEN_WEB_UI_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.TEXT_GEN_WEB_UI_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":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/textGenWebUI/index.js#L41-L77","documentation":"Thrown by the STATIC TextGenWebUILLM.promptWindowLimit(modelName) when TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT is set to a non-numeric value. Note the logic: `const limit = process.env.TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT || 4096;` means an unset/empty value falls back to 4096 and does NOT throw — the only way to reach the throw is to set the variable to something Number() cannot parse (e.g. 'auto', '4k', 'undefined'), making isNaN(Number(limit)) true. The static variant is called without an instance.","triggerScenarios":"Calling TextGenWebUILLM.promptWindowLimit(modelName) (statically) while TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT is set to a non-numeric string such as 'auto', '4k', or 'none'.","commonSituations":"A user copies a forum snippet setting TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT=auto expecting the server to auto-detect; setting it to '4k' thinking the 'k' suffix is honoured; accidentally exporting the literal string 'undefined'.","solutions":["Set TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT to a plain integer (e.g. 4096, 8192) matching the loaded model's context.","If you want the default, unset the variable entirely so the `|| 4096` fallback applies.","Remove any unit suffix ('k', 'K', 'tokens').","Restart AnythingLLM after correcting .env."],"exampleFix":"// before\n// TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT=4k -> Number('4k') is NaN -> throws\nconst ctx = TextGenWebUILLM.promptWindowLimit(\"my-model\");\n\n// after\n// server/.env\n// TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT=4096\nconst ctx = TextGenWebUILLM.promptWindowLimit(\"my-model\");","handlingStrategy":"validation","validationCode":"function parseTokenLimit() {\n  const raw = process.env.TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT;\n  // unset/empty falls back to 4096 inside the provider; only non-numeric throws\n  if (raw === undefined || raw === \"\") return 4096;\n  const n = Number(raw);\n  if (!Number.isFinite(n) || n <= 0) {\n    throw new Error(`TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT must be a positive integer, got: ${raw}`);\n  }\n  return n;\n}\nconst ctx = parseTokenLimit();","typeGuard":"function isValidTokenLimit(raw) {\n  if (raw === undefined || raw === \"\") return true; // default applies\n  return typeof raw === \"string\" && /^[1-9]\\d*$/.test(raw.trim());\n}","tryCatchPattern":"try {\n  return TextGenWebUILLM.promptWindowLimit(modelName);\n} catch (e) {\n  if (/No token context limit/i.test(e.message)) {\n    delete process.env.TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT; // accept default 4096\n    return TextGenWebUILLM.promptWindowLimit(modelName);\n  }\n  throw e;\n}","preventionTips":["Remember the provider only throws for NON-NUMERIC values — unset is safe (defaults to 4096).","Reject unit suffixes ('k', 'K', 'tokens') at config time.","Add a startup regex check /^[1-9]\\d*$/ on TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT.","Keep .env.example showing an integer example to set the convention."],"tags":["configuration","env","validation","llm-provider","textgenwebui","token-limit"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}