{"record":{"id":"db5400d7e637bf70","repo":"Mintplex-Labs/anything-llm","slug":"no-localai-token-context-limit-was-set","errorCode":null,"errorMessage":"No LocalAi token context limit was set.","messagePattern":"No LocalAi token context limit was set\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/localAi/index.js","lineNumber":51,"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.LOCAL_AI_MODEL_TOKEN_LIMIT || 4096;\n    if (!limit || isNaN(Number(limit)))\n      throw new Error(\"No LocalAi 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.LOCAL_AI_MODEL_TOKEN_LIMIT || 4096;\n    if (!limit || isNaN(Number(limit)))\n      throw new Error(\"No LocalAi token context limit was set.\");\n    return Number(limit);\n  }\n\n  async isValidChatCompletionModel(_ = \"\") {\n    return true;\n  }\n\n  /**\n   * Generates appropriate content array for a message + attachments.","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/3aec848f2885144aa8f1e53b9731a04310d5d558/server/utils/AiProviders/localAi/index.js#L33-L69","documentation":"LocalAiLLM.promptWindowLimit(): `const limit = process.env.LOCAL_AI_MODEL_TOKEN_LIMIT || 4096; if (!limit || isNaN(Number(limit))) throw 'No LocalAi token context limit was set.'`. As with the KoboldCPP/LiteLLM twins, the || 4096 fallback kills the falsy branch — the throw means the env var is set to a string Number() cannot parse. The constructor calls it to compute this.limits, so it fails at provider construction time.","triggerScenarios":"Instantiating LocalAiLLM (i.e., choosing LocalAI as provider) or calling promptWindowLimit() while LOCAL_AI_MODEL_TOKEN_LIMIT holds a non-numeric value — '16384 ', '16k', '16,384', quoted numbers, or CRLF-polluted lines from Windows-edited .env files.","commonSituations":"Pasting context-window specs with units; dotenv files with BOM/CRLF artifacts; compose interpolation injecting empty-but-present values (${VAR} with VAR unsets can yield ''); secret managers storing numbers as quoted strings.","solutions":["Set LOCAL_AI_MODEL_TOKEN_LIMIT to a bare integer (e.g., 16384) or remove it to use the 4096 default.","Sanitize the .env: no commas, units, quotes, spaces, inline comments; verify with cat -A.","In Docker, ensure the variable is declared with a clean value under environment: and recreate the container.","Add a boot-time validation step that Number()-parses all *_TOKEN_LIMIT vars and fails fast with a clear message."],"exampleFix":"# before (.env)\nLOCAL_AI_MODEL_TOKEN_LIMIT=16,384\n\n# after (.env)\nLOCAL_AI_MODEL_TOKEN_LIMIT=16384","handlingStrategy":"validation","validationCode":"const raw = process.env.LOCAL_AI_MODEL_TOKEN_LIMIT;\nif (raw && !/^\\d+$/.test(raw.trim())) throw new Error(`LOCAL_AI_MODEL_TOKEN_LIMIT must be an integer, got ${JSON.stringify(raw)}`);\nconst llm = new LocalAiLLM(embedder, model);","typeGuard":"const isParsableLimitEnv = (v) => v == null || v === \"\" || /^\\d+$/.test(String(v).trim());","tryCatchPattern":"try {\n  const llm = new LocalAiLLM(embedder, model);\n} catch (e) {\n  if (/LocalAi token context limit/i.test(e.message)) throw new Error(\"Fix LOCAL_AI_MODEL_TOKEN_LIMIT to a bare integer or unset for the 4096 default\");\n  throw e;\n}","preventionTips":["Run one boot-time validator over all *_MODEL_TOKEN_LIMIT vars using /^\\d+$/.","Write limits as plain integers — no commas, units, quotes, or inline comments.","If unsure of the real context window, unset the var; the 4096 default keeps the provider constructing successfully."],"tags":["localai","anythingllm","env-var","nan","token-limit","configuration"],"backgroundTag":"invalid-env-var-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"}