/** * Context Budget System for Kuralle. * * Manages token allocation across the multiple content sources that compose * each LLM request. Ensures no single section can monopolize the context window. */ /** * ContextBudgetConfig defines the token allocation strategy for the LLM * context window. It governs how the finite context window is partitioned * across the multiple content sources that compose each LLM request. */ export interface ContextBudgetConfig { /** Total context window size of the target model, in tokens. Default: 128,000. */ modelContextWindow: number; /** Tokens reserved for the LLM's response generation. Default: 4,096. */ responseReserve: number; /** Maximum tokens allocated to auto-retrieve (RAG) context injection. Default: 4,000. */ maxAutoRetrieveTokens: number; /** Maximum tokens allocated to the working memory section. Default: 2,000. */ maxWorkingMemoryTokens: number; /** Maximum tokens allocated to the extraction snapshot section. Default: 2,000. */ maxExtractionTokens: number; /** Maximum tokens allocated to cross-session long-term memory. Default: 2,000. */ maxLongTermMemoryTokens: number; /** * Maximum tokens allocated to the base system prompt. * 0 means no limit — the base prompt is measured, not capped. * Default: 0. */ maxBasePromptTokens: number; } export declare const DEFAULT_CONTEXT_BUDGET: ContextBudgetConfig; /** Preset for realtime / voice agents — smaller window and allocations for low-latency turns. */ export declare const VOICE_CONTEXT_BUDGET: ContextBudgetConfig; /** * Token estimation function. Uses a rough 4:1 character-to-token ratio. * Consistent with ContextManager.ts. */ export declare function estimateTokenCount(text: string): number; /** * Computes the remaining token budget available for message history after * all system prompt sections have been allocated. * * Floors at 1000 tokens to ensure the LLM always sees at least minimal * conversation history. */ export declare function computeMessageHistoryBudget(config: ContextBudgetConfig, measuredBasePromptTokens: number, measuredPolicyInjectionTokens: number): number; /** * Truncates a text string to fit within a token budget. * Truncation is at the nearest sentence boundary when possible. */ export declare function truncateToTokenBudget(text: string, maxTokens: number): string; /** * Budget-aware working memory formatter. * Iterates entries in insertion order, dropping entries that exceed the budget. * Entries are atomic — they are either fully included or fully dropped. */ export declare function formatMemoryWithBudget(memory: Record, maxTokens: number, allowlist?: string[]): string; /** * Tracks the last pre-flight input token estimate and compares it to provider-reported actuals. */ export declare class ContextBudget { private readonly config; private lastEstimate; constructor(config: ContextBudgetConfig); /** Call after assembling the request (e.g. from `onBeforeModelCall` estimated tokens). */ recordPreFlightEstimate(estimatedInputTokens: number): void; /** * Compares the last pre-flight estimate to actual input tokens from the provider. * Logs a warning when absolute drift percentage exceeds 20%. */ validateActual(actualInputTokens: number): { estimated: number; actual: number; drift: number; driftPct: number; }; get modelContextWindow(): number; }