{"record":{"id":"643350da5f3ce4ec","repo":"continuedev/continue","slug":"not-enough-context-available-to-include-the-system","errorCode":null,"errorMessage":"Not enough context available to include the system message, last user message, and tools.\n        There must be at least ${minOutputTokens} tokens remaining for output.\n        Request had the following token counts:\n        - contextLength: ${knownContextLength}\n        - counting safety buffer: ${countingSafetyBuffer}\n        - tools: ~${toolTokens}\n        - system message: ~${systemMsgTokens}\n        - max output tokens: ${maxTokens}","messagePattern":"Not enough context available to include the system message, last user message, and tools\\.\n        There must be at least (.+?) tokens remaining for output\\.\n        Request had the following token counts:\n        - contextLength: (.+?)\n        - counting safety buffer: (.+?)\n        - tools: ~(.+?)\n        - system message: ~(.+?)\n        - max output tokens: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"core/llm/countTokens.ts","lineNumber":498,"sourceCode":"\n  const contextLength = knownContextLength ?? DEFAULT_PRUNING_LENGTH;\n  const countingSafetyBuffer = getTokenCountingBufferSafety(contextLength);\n  const minOutputTokens = Math.min(MIN_RESPONSE_TOKENS, maxTokens);\n\n  let inputTokensAvailable = contextLength;\n\n  // Leave space for output/safety\n  inputTokensAvailable -= countingSafetyBuffer;\n  inputTokensAvailable -= minOutputTokens;\n\n  // Non-negotiable messages\n  inputTokensAvailable -= toolTokens;\n  inputTokensAvailable -= systemMsgTokens;\n  inputTokensAvailable -= lastMessagesTokens;\n\n  // Make sure there's enough context for the non-excludable items\n  if (knownContextLength !== undefined && inputTokensAvailable < 0) {\n    throw new Error(\n      `Not enough context available to include the system message, last user message, and tools.\n        There must be at least ${minOutputTokens} tokens remaining for output.\n        Request had the following token counts:\n        - contextLength: ${knownContextLength}\n        - counting safety buffer: ${countingSafetyBuffer}\n        - tools: ~${toolTokens}\n        - system message: ~${systemMsgTokens}\n        - max output tokens: ${maxTokens}`,\n    );\n  }\n\n  // Now remove messages till we're under the limit\n  let currentTotal = 0;\n  const historyWithTokens = msgsCopy.map((message) => {\n    const tokens = countChatMessageTokens(modelName, message);\n    currentTotal += tokens;\n    return {\n      ...message,","sourceCodeStart":480,"sourceCodeEnd":516,"githubUrl":"https://github.com/continuedev/continue/blob/5522c6f44ca0ac3528b37244818fbfa39b5af470/core/llm/countTokens.ts#L480-L516","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Switch to a model with a larger context window","Trim the tool list (disable unused MCP tools/extensions) to cut toolTokens","Shorten or remove the system message, and attach less file content","Verify contextLength in your config matches the actual model; correct it if it was mis-set"],"exampleFix":"// before\nmodels: [{ name: \"small-model\", contextLength: 8192, ... }]\n// after\nmodels: [{ name: \"long-context-model\", contextLength: 128000, ... }]","handlingStrategy":"fallback","validationCode":"const est = estimateTokens(toolsJson) + estimateTokens(system) + estimateTokens(lastMsg); if (est + minOutput > contextLength) throw new Error('prune tools/messages first');","typeGuard":null,"tryCatchPattern":"catch (e) { if (e.message.includes('Not enough context')) { dropToolsAndLongAttachmentsThenRetry(); } else throw e; }","preventionTips":["Right-size model contextLength in config","Disable unused MCP tools/extensions","Avoid attaching whole files when snippets suffice","Monitor token counts before large requests"],"tags":["context-window","tokens","llm","configuration"],"backgroundTag":"context-length-exceeded","analyzedSha":"5522c6f44ca0ac3528b37244818fbfa39b5af470","analyzedAt":"2026-08-27T11:28:54.683Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}