Skip to content

fix: clamp maxAllowedPromptTokens to zero to prevent silent autocomplete failures - #13045

Open
rekha0suthar wants to merge 1 commit into
continuedev:mainfrom
rekha0suthar:fix/autocomplete-negative-prune-length
Open

fix: clamp maxAllowedPromptTokens to zero to prevent silent autocomplete failures#13045
rekha0suthar wants to merge 1 commit into
continuedev:mainfrom
rekha0suthar:fix/autocomplete-negative-prune-length

Conversation

@rekha0suthar

Copy link
Copy Markdown

Problem

When using Ollama with a num_ctx value equal to (or smaller than) Continue's default maxTokens, autocomplete silently produces no output. No error is shown, no completion appears — it just does nothing.

Root Cause

In renderPromptWithTokenLimit, the pruneLength helper calculates how many tokens to remove from the prompt:

function pruneLength(llm: ILLM, prompt: string): number {
  const contextLength = llm.contextLength;
  const reservedTokens = llm.completionOptions.maxTokens ?? DEFAULT_MAX_TOKENS;
  const safetyBuffer = getTokenCountingBufferSafety(contextLength);
  const maxAllowedPromptTokens = contextLength - reservedTokens - safetyBuffer;
  const promptTokenCount = countTokens(prompt, llm.model);
  return promptTokenCount - maxAllowedPromptTokens;
}

When contextLength ≤ reservedTokens + safetyBuffer (which happens when Ollama's num_ctx equals Continue's default maxTokens), maxAllowedPromptTokens becomes negative. This causes pruneLength to return promptTokenCount - (negative), i.e. a very large positive number. The pruner then tries to drop more tokens than the entire prompt contains, reducing it to nothing.

Fix

Clamp maxAllowedPromptTokens to a minimum of 0:

const maxAllowedPromptTokens = Math.max(
  0,
  contextLength - reservedTokens - safetyBuffer,
);

This ensures the pruner never requests removal of more tokens than the prompt has, preventing the silent empty-output failure.

Reproducing

  1. Set Ollama num_ctx to the same value as Continue's default maxTokens (600)
  2. Open a file and wait for autocomplete to trigger
  3. Before: no suggestion appears, no error in logs
  4. After: autocomplete works correctly, prompt is used as-is (no pruning needed)

Fixes #13038


🤖 Generated with Claude Code

@rekha0suthar
rekha0suthar requested a review from a team as a code owner July 28, 2026 06:44
@rekha0suthar
rekha0suthar requested review from sestinj and removed request for a team July 28, 2026 06:44
@github-actions

github-actions Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@rekha0suthar

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@rekha0suthar

Copy link
Copy Markdown
Author

recheck

@rekha0suthar
rekha0suthar force-pushed the fix/autocomplete-negative-prune-length branch from fb543fb to 8fe6f59 Compare July 28, 2026 07:13
@rekha0suthar

Copy link
Copy Markdown
Author

recheck

When the LLM contextLength is less than or equal to the sum of
maxTokens and the safety buffer (e.g. when Ollama num_ctx equals
Continue default maxTokens), the calculation

  contextLength - reservedTokens - safetyBuffer

produced a negative value. This caused pruneLength() to return a very
large positive number, making the prompt pruner strip all context and
silently produce an empty completion.

Fixed by clamping maxAllowedPromptTokens to 0 with Math.max(), so the
pruner never attempts to remove more tokens than the prompt contains.

Fixes continuedev#13038
@rekha0suthar
rekha0suthar force-pushed the fix/autocomplete-negative-prune-length branch from 8fe6f59 to a32931c Compare July 28, 2026 07:27
@rekha0suthar

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant