{"record":{"id":"69da8482af23ccbb","repo":"Mintplex-Labs/anything-llm","slug":"no-token-context-limit-was-set-69da84","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/koboldCPP/index.js","lineNumber":63,"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.KOBOLD_CPP_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.KOBOLD_CPP_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":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/koboldCPP/index.js#L45-L81","documentation":"Thrown by the static promptWindowLimit method on KoboldCPPLLM. It reads KOBOLD_CPP_MODEL_TOKEN_LIMIT, defaulting to 4096 via `||`. The error fires only when the env var is set to a truthy but non-numeric value (e.g. 'unlimited'), because the `|| 4096` fallback already absorbs unset/empty/falsy values. The static variant is used for pre-flight context-window calculations before a provider instance exists.","triggerScenarios":"Calling `KoboldCPPLLM.promptWindowLimit(modelName)` when process.env.KOBOLD_CPP_MODEL_TOKEN_LIMIT is set to a non-numeric string such as 'auto', 'unlimited', 'max', or 'n/a'. Numeric strings like '8192' or '0' do NOT trigger it (0 passes isNaN but yields zero-length limits).","commonSituations":"A user copies a token-limit value from a forum or blog that uses a word instead of a number. Setting the variable to 'unlimited' expecting KoboldCPP to auto-detect context size. Misunderstanding the comment 'if undefined - assume 4096' and setting a placeholder word.","solutions":["Set KOBOLD_CPP_MODEL_TOKEN_LIMIT to a numeric value (e.g. 4096, 8192, 16384) in .env.","Remove or comment out KOBOLD_CPP_MODEL_TOKEN_LIMIT entirely to fall back to the built-in 4096 default.","If using a pre-flight context-size check, catch the error and default to 4096 with a warning log."],"exampleFix":"// before\nKOBOLD_CPP_MODEL_TOKEN_LIMIT='unlimited'\n\n// after\nKOBOLD_CPP_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(\n      `${envVar}=\"${raw}\" is not a number. Set it to an integer or unset it for default ${defaultLimit}.`\n    );\n  }\n  return num;\n}\n\n// Pre-flight usage:\nconst limit = validateTokenLimit('KOBOLD_CPP_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 = KoboldCPPLLM.promptWindowLimit();\n} catch (e) {\n  if (e.message.includes('token context limit')) {\n    console.warn('KOBOLD_CPP_MODEL_TOKEN_LIMIT is invalid, defaulting to 4096');\n    limit = 4096;\n  } else {\n    throw e;\n  }\n}","preventionTips":["Always use plain integers for token-limit env vars — no units, no descriptive words.","Add a CI lint check or startup validator that verifies all *_TOKEN_LIMIT env vars are numeric.","Document the expected format clearly in .env.example comments."],"tags":["koboldcpp","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"}