@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
106 lines (105 loc) • 5.63 kB
TypeScript
/**
* Context Window Registry
*
* Accurate per-provider, per-model context window sizes (INPUT token limits).
* These are distinct from OUTPUT token limits in tokens.ts.
*
* Sources:
* - Anthropic: https://docs.anthropic.com/en/docs/about-claude/models
* - OpenAI: https://platform.openai.com/docs/models
* - Google: https://ai.google.dev/gemini-api/docs/models
* - Others: Provider documentation as of Feb 2026
*/
/** Default context window when provider/model is unknown */
export declare const DEFAULT_CONTEXT_WINDOW = 128000;
/** Maximum output reserve when maxTokens not specified */
export declare const MAX_DEFAULT_OUTPUT_RESERVE = 64000;
/** Default output reserve ratio (35% of context) */
export declare const DEFAULT_OUTPUT_RESERVE_RATIO = 0.35;
/**
* Per-provider, per-model context window sizes.
* The "_default" key is the fallback for unknown models within a provider.
*/
export declare const MODEL_CONTEXT_WINDOWS: Record<string, Record<string, number>>;
/**
* Register a runtime-discovered context window for a provider/model pair.
* Later registrations overwrite earlier ones (rediscovery refreshes values).
* Non-positive/non-finite windows are ignored so a malformed discovery source
* can never shrink a budget to zero.
*/
export declare function registerRuntimeContextWindow(provider: string, model: string, contextWindow: number): void;
/** Test hook: clear runtime-discovered windows (state is module-global). */
export declare function clearRuntimeContextWindows(): void;
/**
* Runtime-discovered window for an exact provider/model pair, or undefined.
*
* Unlike {@link getContextWindowSize} this NEVER falls back to static table
* values. Callers use it to distinguish "the serving infrastructure told us
* the real window" (safe to hard-enforce: clamp max_tokens, fail fast) from
* "static guess" (advisory only — hard-enforcing a guessed window would
* falsely reject requests that the real deployment accepts).
*/
export declare function getRuntimeContextWindow(provider: string, model?: string): number | undefined;
/**
* Register a runtime-discovered output-token ceiling for a provider/model
* pair. Later registrations overwrite earlier ones (rediscovery refreshes
* values). Non-positive/non-finite ceilings are ignored so a malformed
* discovery source can never shrink an output budget to zero.
*/
export declare function registerRuntimeOutputCeiling(provider: string, model: string, maxOutputTokens: number): void;
/**
* Runtime-discovered output ceiling for an exact provider/model pair, or
* undefined when the serving infrastructure has not advertised one.
*/
export declare function getRuntimeOutputCeiling(provider: string, model?: string): number | undefined;
/** Remove a model ceiling that discovery no longer advertises as valid. */
export declare function clearRuntimeOutputCeiling(provider: string, model: string): void;
/** Test hook: clear runtime-discovered output ceilings (state is module-global). */
export declare function clearRuntimeOutputCeilings(): void;
/**
* Resolve context window size for a provider/model combination.
*
* Priority:
* 0. Dynamic model registry (DynamicModelProvider) — resolves cross-provider
* models (e.g. Claude on Vertex) that the static table cannot handle
* 0.5 Runtime-discovered windows (registerRuntimeContextWindow) — real
* per-model limits fetched from the serving infrastructure (LiteLLM
* `/model/info`)
* 1. Manifest exact/alias match ONLY (resolveManifestEntryStrict — no prefix, see
* src/lib/models/manifestRegistry.ts) — the emerging single source of
* truth for per-model metadata. Deliberately consulted for a REAL match
* only (never the manifest's synthesized/generic default): most provider
* manifests are still "minimal" stubs holding nothing but a provider-wide
* default (e.g. vertex.ts, together-ai.ts) and none of the per-model
* overrides — including Vertex's Claude "claude-" prefix catch-all below,
* which exists specifically to stop an unlisted Claude-on-Vertex model
* from inheriting Gemini's 1,048,576 default — that still live only in
* MODEL_CONTEXT_WINDOWS. Falling back to a manifest-wide default here
* would silently drop that coverage. When a manifest DOES carry a real
* entry for this model, it wins even if it disagrees with the legacy
* table (e.g. Mistral's manifest supersedes the coarser, older
* MODEL_CONTEXT_WINDOWS.mistral values).
* 2. Exact model match under provider in static registry
* 3. Prefix match under provider in static registry
* 4. Provider's _default in static registry
* 5. Global DEFAULT_CONTEXT_WINDOW
*/
export declare function getContextWindowSize(provider: string, model?: string): number;
/**
* Calculate output token reserve for a given context window.
*
* Returns the *real* token count that will be reserved for output so callers
* (`getAvailableInputTokens`, `BudgetChecker`, conversation-memory pruning, file
* summarisation) compute input budget against the actual outgoing maxTokens.
*
* @param contextWindow - Total context window size
* @param maxTokens - Explicit maxTokens from user config (if set)
* @returns Number of tokens reserved for output (matches what's sent upstream)
*/
export declare function getOutputReserve(contextWindow: number, maxTokens?: number): number;
/**
* Calculate available input tokens for a given provider/model.
*
* available = contextWindow - outputReserve
*/
export declare function getAvailableInputTokens(provider: string, model?: string, maxTokens?: number): number;