MCPcopy Index your code
hub / github.com/continuedev/continue / validateContextLength

Function validateContextLength

extensions/cli/src/util/tokenizer.ts:354–393  ·  view source on GitHub ↗
(params: ValidateContextLengthParams)

Source from the content-addressed store, hash-verified

352 * @returns Validation result with error details if invalid
353 */
354export function validateContextLength(params: ValidateContextLengthParams): {
355 isValid: boolean;
356 error?: string;
357 inputTokens?: number;
358 contextLimit?: number;
359 maxTokens?: number;
360} {
361 const { chatHistory, model, safetyBuffer = 0, systemMessage, tools } = params;
362
363 const inputTokens = countTotalInputTokens({
364 chatHistory,
365 systemMessage,
366 tools,
367 model,
368 });
369 const contextLimit = getModelContextLimit(model);
370 const maxTokens = model.defaultCompletionOptions?.maxTokens || 0;
371
372 // If maxTokens is not set, use 35% default reservation for output
373 const reservedForOutput =
374 maxTokens > 0 ? maxTokens : Math.ceil(contextLimit * 0.35);
375 const totalRequired = inputTokens + reservedForOutput + safetyBuffer;
376
377 if (totalRequired > contextLimit) {
378 return {
379 isValid: false,
380 error: `Context length exceeded: input (${inputTokens.toLocaleString()}) + max_tokens (${reservedForOutput.toLocaleString()})${safetyBuffer > 0 ? ` + buffer (${safetyBuffer})` : ""} = ${totalRequired.toLocaleString()} > context_limit (${contextLimit.toLocaleString()})`,
381 inputTokens,
382 contextLimit,
383 maxTokens: reservedForOutput,
384 };
385 }
386
387 return {
388 isValid: true,
389 inputTokens,
390 contextLimit,
391 maxTokens: reservedForOutput,
392 };
393}

Calls 2

countTotalInputTokensFunction · 0.85
getModelContextLimitFunction · 0.85

Tested by

no test coverage detected