import type { AssistantMessage } from "../types.js"; export interface ContextOverflowOptions { /** Maximum output tokens requested for the exhausted response. */ maxOutputTokens?: number; /** Estimated request-input tokens when the provider omitted usage. */ estimatedInputTokens?: number; } /** Whether the agent exhausted its bounded retries at the configured output limit. */ export declare function isOutputLimitExhaustion(message: AssistantMessage): boolean; /** * Check if an assistant message represents a context overflow error. * * This handles three cases: * 1. Error-based overflow: Most providers return stopReason "error" with a * specific error message pattern. * 2. Context-constrained truncation: dreb exhausted its length retries and the * provider signal or input-plus-configured-output budget indicates that the * response may not have fit in the remaining context window. * 3. Silent overflow: Some providers accept requests beyond the configured window * and return successfully (the server's hard limit may be higher). For these, * we compare input + cacheRead + cacheWrite against the configured window; * output tokens are not part of the input size. * * ## Reliability by Provider * * **Reliable detection (returns error with detectable message):** * - Anthropic: "prompt is too long: X tokens > Y maximum" * - OpenAI (Completions & Responses): "exceeds the context window" * - Google Gemini: "input token count exceeds the maximum" * - xAI (Grok): "maximum prompt length is X but request contains Y" * - Groq: "reduce the length of the messages" * - Cerebras: 400/413 status code (no body) * - Mistral: "Prompt contains X tokens ... too large for model with Y maximum context length" * - OpenRouter (all backends): "maximum context length is X tokens" * - llama.cpp: "exceeds the available context size" * - LM Studio: "greater than the context length" * - Kimi For Coding: "exceeded model token limit: X (requested: Y)" * * **Unreliable detection:** * - z.ai: Sometimes accepts overflow silently, sometimes returns rate limit errors. * - GitHub Copilot: May accept input beyond a conservative configured window. * Pass contextWindow to detect these cases using input + cacheRead + cacheWrite. * - Ollama: Silently truncates input without error. Cannot be detected via this function. * The response will have usage.input < expected, but we don't know the expected value. * * ## Custom Providers * * If you've added custom models via settings.json, this function may not detect * overflow errors from those providers. To add support: * * 1. Send a request that exceeds the model's context window * 2. Check the errorMessage in the response * 3. Create a regex pattern that matches the error * 4. The pattern should be added to OVERFLOW_PATTERNS in this file, or * check the errorMessage yourself before calling this function * * @param message - The assistant message to check * @param contextWindow - Optional configured window for usage-based overflow detection * @param options - Output limit and input estimate used only after bounded length retries are exhausted * @returns true if the message indicates a context overflow */ export declare function isContextOverflow(message: AssistantMessage, contextWindow?: number, options?: ContextOverflowOptions): boolean; /** * Get the overflow patterns for testing purposes. */ export declare function getOverflowPatterns(): RegExp[]; //# sourceMappingURL=overflow.d.ts.map