BerriAI/litellm · error · ContextWindowExceededError
{custom_llm_provider.capitalize()}Exception: Context Window
Error message
{custom_llm_provider.capitalize()}Exception: Context Window Error - {error_str} What it means
LiteLLM re-throws a provider's failure as ContextWindowExceededError when the raw error text matches its context-window phrase list (ExceptionCheckers.is_error_str_context_window_exceeded). This means the input plus requested output exceeded the model's maximum context length. The message is prefixed with the capitalized provider name so you can tell which backend rejected the request.
Source
Thrown at litellm/litellm_core_utils/exception_mapping_utils.py:725
extra_information: str,
) -> None:
if "authorization denied for" in error_str:
# Predibase returns the raw API Key in the response - this block ensures it's not returned in the exception
if error_str is not None and isinstance(error_str, str) and "bearer" in error_str.lower():
# only keep the first 10 chars after the occurnence of "bearer"
_bearer_token_start_index: Final = error_str.lower().find("bearer")
error_str = error_str[: _bearer_token_start_index + 14]
error_str += "XXXXXXX" + '"'
raise AuthenticationError(
message=f"{custom_llm_provider.capitalize()}Exception: Authentication Error - {error_str}",
llm_provider=custom_llm_provider,
model=model,
response=getattr(original_exception, "response", None),
litellm_debug_info=extra_information,
)
elif ExceptionCheckers.is_error_str_context_window_exceeded(error_str):
raise ContextWindowExceededError(
message=f"{custom_llm_provider.capitalize()}Exception: Context Window Error - {error_str}",
model=model,
llm_provider=custom_llm_provider,
response=getattr(original_exception, "response", None),
litellm_debug_info=extra_information,
)
elif "token_quota_reached" in error_str:
raise RateLimitError(
message=f"{custom_llm_provider.capitalize()}Exception: Rate Limit Errror - {error_str}",
llm_provider=custom_llm_provider,
model=model,
response=getattr(original_exception, "response", None),
)
elif "The server received an invalid response from an upstream server." in error_str:
raise litellm.InternalServerError(
message=f"{custom_llm_provider.capitalize()}Exception - {original_exception.message}",
llm_provider=custom_llm_provider,
model=model,View on GitHub (pinned to 6c2dcb801b)
Solutions
- Reduce input size: truncate or summarize the message history, or drop older turns before calling completion().
- Compute the token count first with litellm.token_counter(model=..., messages=...) and compare against litellm.get_max_tokens(model).
- Switch to a larger-context model (e.g. gpt-4o, claude with 200k) or route via litellm.Router with context_window_fallbacks.
- Lower max_tokens so prompt + completion fits within the limit.
Example fix
# before
resp = litellm.completion(model="gpt-3.5-turbo", messages=messages)
# after
n = litellm.token_counter(model="gpt-3.5-turbo", messages=messages)
limit = litellm.get_max_tokens(model="gpt-3.5-turbo")
while n > limit - 500:
messages.pop(1) # drop oldest turns, keep system prompt
n = litellm.token_counter(model="gpt-3.5-turbo", messages=messages)
resp = litellm.completion(model="gpt-3.5-turbo", messages=messages) Defensive patterns
Strategy: fallback
Validate before calling
import litellm
def fits_context(model: str, messages: list) -> bool:
used = litellm.token_counter(model=model, messages=messages)
limit = litellm.get_max_tokens(model=model) or 4096
return used <= limit - 256 # headroom for the completion Type guard
import litellm
def is_context_window_error(e: BaseException) -> bool:
return isinstance(e, litellm.ContextWindowExceededError) Try / catch
try:
resp = litellm.completion(model=model, messages=messages)
except litellm.ContextWindowExceededError:
messages = trim_history(messages, keep=8)
resp = litellm.completion(model=fallback_model, messages=messages) Prevention
- Count tokens with litellm.token_counter and compare against litellm.get_max_tokens before every large call.
- Configure litellm.Router context_window_fallbacks to auto-switch to a larger-context model.
- Cap retrieved RAG chunks (max_chunks * avg_chunk_tokens) well below the window.
- Reserve headroom: budget prompt tokens <= limit - max_tokens.
When it happens
Trigger: A completion() call where the provider returns an error containing phrases like 'maximum context length', 'too many tokens', or 'input is too long' (OpenAI, Azure, Anthropic, etc.). Any provider whose error string matches the checker gets mapped here, and the original provider exception text is appended after 'Context Window Error - '.
Common situations: Long chat histories, RAG pipelines stuffing many retrieved chunks, large files pasted into messages, or sending a big max_tokens alongside a near-limit prompt. Also happens after switching to a model with a smaller context window (e.g. moving from gpt-4-32k to an 8k model) without trimming history.
Related errors
- BedrockException: Context Window Error - {error_str}
- ContextWindowExceededError: {custom_llm_provider.capitalize(
- ContextWindowExceededError: {exception_provider} - {message}
- SagemakerException - {error_str}
- {custom_llm_provider.capitalize()}Exception - {error_str}
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/8276ca3a4648acbd.
Report an issue: GitHub.