From a32931c48f5e4109d43c6340e62c3a82212dae2a Mon Sep 17 00:00:00 2001 From: REKHA SUTHAR <71004640+rekha0suthar@users.noreply.github.com> Date: Tue, 28 Jul 2026 12:56:58 +0530 Subject: [PATCH] fix: clamp maxAllowedPromptTokens to zero in autocomplete pruner 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 #13038 --- core/autocomplete/templating/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/autocomplete/templating/index.ts b/core/autocomplete/templating/index.ts index 3ebf29b71be..cb51ef55abb 100644 --- a/core/autocomplete/templating/index.ts +++ b/core/autocomplete/templating/index.ts @@ -204,7 +204,10 @@ 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 maxAllowedPromptTokens = Math.max( + 0, + contextLength - reservedTokens - safetyBuffer, + ); const promptTokenCount = countTokens(prompt, llm.model); return promptTokenCount - maxAllowedPromptTokens; }