Mintplex-Labs/anything-llm · error · Error

No token context limit was set.

Error message

No token context limit was set.

What it means

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.

Source

Thrown at server/utils/AiProviders/textGenWebUI/index.js:59

    if (!contextTexts || !contextTexts.length) return "";
    return (
      "\nContext:\n" +
      contextTexts
        .map((text, i) => {
          return `[CONTEXT ${i}]:\n${text}\n[END CONTEXT ${i}]\n\n`;
        })
        .join("")
    );
  }

  streamingEnabled() {
    return "streamGetChatCompletion" in this;
  }

  static promptWindowLimit(_modelName) {
    const limit = process.env.TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT || 4096;
    if (!limit || isNaN(Number(limit)))
      throw new Error("No token context limit was set.");
    return Number(limit);
  }

  // Ensure the user set a value for the token limit
  // and if undefined - assume 4096 window.
  promptWindowLimit() {
    const limit = process.env.TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT || 4096;
    if (!limit || isNaN(Number(limit)))
      throw new Error("No token context limit was set.");
    return Number(limit);
  }

  // Short circuit since we have no idea if the model is valid or not
  // in pre-flight for generic endpoints
  isValidChatCompletionModel(_modelName = "") {
    return true;
  }

View on GitHub (pinned to 526360e320)

Solutions

  1. Set TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT to a plain integer (e.g. 4096, 8192) matching the loaded model's context.
  2. If you want the default, unset the variable entirely so the `|| 4096` fallback applies.
  3. Remove any unit suffix ('k', 'K', 'tokens').
  4. Restart AnythingLLM after correcting .env.

Example fix

// before
// TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT=4k -> Number('4k') is NaN -> throws
const ctx = TextGenWebUILLM.promptWindowLimit("my-model");

// after
// server/.env
// TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT=4096
const ctx = TextGenWebUILLM.promptWindowLimit("my-model");
Defensive patterns

Strategy: validation

Validate before calling

function parseTokenLimit() {
  const raw = process.env.TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT;
  // unset/empty falls back to 4096 inside the provider; only non-numeric throws
  if (raw === undefined || raw === "") return 4096;
  const n = Number(raw);
  if (!Number.isFinite(n) || n <= 0) {
    throw new Error(`TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT must be a positive integer, got: ${raw}`);
  }
  return n;
}
const ctx = parseTokenLimit();

Type guard

function isValidTokenLimit(raw) {
  if (raw === undefined || raw === "") return true; // default applies
  return typeof raw === "string" && /^[1-9]\d*$/.test(raw.trim());
}

Try / catch

try {
  return TextGenWebUILLM.promptWindowLimit(modelName);
} catch (e) {
  if (/No token context limit/i.test(e.message)) {
    delete process.env.TEXT_GEN_WEB_UI_MODEL_TOKEN_LIMIT; // accept default 4096
    return TextGenWebUILLM.promptWindowLimit(modelName);
  }
  throw e;
}

Prevention

When it happens

Trigger: 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'.

Common situations: 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'.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/d78453b115f7b24a. Report an issue: GitHub.