LLM Output Token Budget Enforcer (chatCore/outputTokenBudget.ts)

LLM-Router · open-sse/handlers/chatCore/outputTokenBudget.ts · typescript · MIT
ai, lang:typescript, tag:tokens, tag:router

Enforces and adjusts output token limits for LLM requests based on context windows. Use when routing requests across multiple AI language models.

// Source: https://github.com/cmahmud/LLM-Router | License: MIT | Copyright 2026 diegosouzapw
// Provided via justclaw.it.com; keep this notice with the code.
export const OUTPUT_TOKEN_FIELDS = [
  "max_tokens",
  "max_completion_tokens",
  "max_output_tokens",
] as const;

export type OutputTokenBudgetResult =
  | {
      ok: true;
      body: Record<string, unknown>;
      availableOutputTokens: number;
      adjustedFields: string[];
    }
  | {
      ok: false;
      estimatedInputTokens: number;
      contextLimit: number;
      maxInputTokens?: number | null;
    };

type OutputTokenAdjustment = { field: string; value?: number; remove?: boolean };

function getOutputTokenAdjustment(
  field: string,
  value: unknown,
  effectiveCap: number
): OutputTokenAdjustment | null {
  if (type

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key