Skip to main content
dcyfr.codes
intermediatetypescript

Context Window Budget Guard

Estimate token usage and truncate context before it exceeds a model's context window limit.

Code

const CHARS_PER_TOKEN = 4; // rough approximation

export function guardContextBudget(
  items: string[],
  maxTokens: number,
  reserveTokens = 2048,
): string[] {
  const limit = (maxTokens - reserveTokens) * CHARS_PER_TOKEN;
  let total = 0;
  const result: string[] = [];
  for (const item of items) {
    if (total + item.length > limit) break;
    result.push(item);
    total += item.length;
  }
  return result;
}

How it works

Reserve tokens for the model's response before filling context. `reserveTokens` defaults to 2048 — adjust based on expected output length. Items are included in order until the budget is exhausted.