{"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":"Error","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/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/localAi/index.js#L33-L69","documentation":"Thrown by the static promptWindowLimit on LocalAiLLM when LOCAL_AI_MODEL_TOKEN_LIMIT is set to a truthy but non-numeric value. The `|| 4096` default handles unset/empty values, so this only fires on explicit misconfiguration. The static method is used for pre-flight context-window calculations before a provider instance exists.","triggerScenarios":"Calling `LocalAiLLM.promptWindowLimit(modelName)` when process.env.LOCAL_AI_MODEL_TOKEN_LIMIT is a non-numeric truthy string (e.g. 'auto', 'max', 'default'). Numeric strings pass the isNaN check; unset/empty get the 4096 default.","commonSituations":"An admin sets a descriptive word instead of a number for the token limit. Copy-pasting a config snippet with a placeholder value. Misunderstanding the variable's expected format.","solutions":["Set LOCAL_AI_MODEL_TOKEN_LIMIT to a plain integer in .env.","Remove the variable to inherit the 4096 default.","If using a pre-flight check, catch the error and default to 4096 with a log warning."],"exampleFix":"// before\nLOCAL_AI_MODEL_TOKEN_LIMIT='auto'\n\n// after\nLOCAL_AI_MODEL_TOKEN_LIMIT=4096","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('LOCAL_AI_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 = LocalAiLLM.promptWindowLimit();\n} catch (e) {\n  if (e.message.includes('token context limit')) {\n    console.warn('LOCAL_AI_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.","Omit the variable rather than setting a placeholder if you want the default."],"tags":["localai","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"}