ContextOverflowError
Raised when a provider rejects a request for exceeding its context window.
Description
ContextOverflowError is a RuntimeError subclass that distinguishes "the conversation no longer fits" from every other provider failure. The distinction matters because the two demand opposite responses:
- An overflow is worth reacting to: compact the history and retry, since resending the same request would fail identically.
- An ordinary failure is transient: fall back to the next preset, as usual.
libchat.call_completion checks each failure and re-raises an overflow as ContextOverflowError before the preset-fallback loop, so an oversized request never burns through the fallback chain.
is_context_overflow_error(error: BaseException) -> bool
Whether an error looks like a provider context-window rejection.
Providers report this in prose rather than a machine-readable code, so the check is a pattern match over the message. Deliberately conservative: mistaking a transient failure for an overflow would throw away history for nothing, so only well-known phrasings match:
maximum context lengthcontext_length_exceeded/context length exceededcontext window exceedexceed(s) the (maximum) (model's) contextprompt is too longinput is too longinput length and max_tokens exceedtoo many tokensreduce the length of the messagesmaximum number of tokens
Matching is case-insensitive.
Recovery
Recovery is opt-in via LLMConfig.enable_overflow_recovery (default True). When enabled, LLM_COMPLETION catches the error, folds the history through ContextCompactor, and retries once. If the retry also overflows, the error propagates.
Usage
from amrita_core.exceptions import ContextOverflowError, is_context_overflow_error
try:
response = await call_completion(messages, config=config)
except ContextOverflowError:
# history no longer fits even after compaction
...Related
- ContextCompactor — what performs the fold
- LLMConfig — the
enable_overflow_recoveryswitch
