continuedev/continue · critical

Not enough context available to include the system message,

Error message

Not enough context available to include the system message, last user message, and tools.
        There must be at least ${minOutputTokens} tokens remaining for output.
        Request had the following token counts:
        - contextLength: ${knownContextLength}
        - counting safety buffer: ${countingSafetyBuffer}
        - tools: ~${toolTokens}
        - system message: ~${systemMsgTokens}
        - max output tokens: ${maxTokens}

What it means

Thrown by compileChatMessages when, after subtracting tool definitions, the system message, the last user message, and a safety buffer from the model's known context length, fewer than zero tokens remain (fewer than minOutputTokens for output). It is a preflight guard that fails the request instead of letting the provider truncate or 400 it later.

Source

Thrown at core/llm/countTokens.ts:498

  const contextLength = knownContextLength ?? DEFAULT_PRUNING_LENGTH;
  const countingSafetyBuffer = getTokenCountingBufferSafety(contextLength);
  const minOutputTokens = Math.min(MIN_RESPONSE_TOKENS, maxTokens);

  let inputTokensAvailable = contextLength;

  // Leave space for output/safety
  inputTokensAvailable -= countingSafetyBuffer;
  inputTokensAvailable -= minOutputTokens;

  // Non-negotiable messages
  inputTokensAvailable -= toolTokens;
  inputTokensAvailable -= systemMsgTokens;
  inputTokensAvailable -= lastMessagesTokens;

  // Make sure there's enough context for the non-excludable items
  if (knownContextLength !== undefined && inputTokensAvailable < 0) {
    throw new Error(
      `Not enough context available to include the system message, last user message, and tools.
        There must be at least ${minOutputTokens} tokens remaining for output.
        Request had the following token counts:
        - contextLength: ${knownContextLength}
        - counting safety buffer: ${countingSafetyBuffer}
        - tools: ~${toolTokens}
        - system message: ~${systemMsgTokens}
        - max output tokens: ${maxTokens}`,
    );
  }

  // Now remove messages till we're under the limit
  let currentTotal = 0;
  const historyWithTokens = msgsCopy.map((message) => {
    const tokens = countChatMessageTokens(modelName, message);
    currentTotal += tokens;
    return {
      ...message,

View on GitHub (pinned to 5522c6f44c)

Solutions

  1. Switch to a model with a larger context window
  2. Trim the tool list (disable unused MCP tools/extensions) to cut toolTokens
  3. Shorten or remove the system message, and attach less file content
  4. Verify contextLength in your config matches the actual model; correct it if it was mis-set

Example fix

// before
models: [{ name: "small-model", contextLength: 8192, ... }]
// after
models: [{ name: "long-context-model", contextLength: 128000, ... }]
Defensive patterns

Strategy: fallback

Validate before calling

const est = estimateTokens(toolsJson) + estimateTokens(system) + estimateTokens(lastMsg); if (est + minOutput > contextLength) throw new Error('prune tools/messages first');

Try / catch

catch (e) { if (e.message.includes('Not enough context')) { dropToolsAndLongAttachmentsThenRetry(); } else throw e; }

Prevention

When it happens

Trigger: Huge tool definitions plus a long system prompt plus large attached files/messages against a small contextLength model; e.g. 30k tokens of tools + 100k attachment on an 8k/32k-context model.

Common situations: Adding many MCP tools to an agent with a small-context model; pasting entire files or long chat histories; misconfigured contextLength in config (set too high or wrong model selected); recent growth of the system prompt.

Related errors


AI-assisted analysis of continuedev/continue@5522c6f44c (2026-08-27). Data as JSON: /api/errors/643350da5f3ce4ec. Report an issue: GitHub.