{"record":{"id":"8276ca3a4648acbd","repo":"BerriAI/litellm","slug":"custom-llm-provider-capitalize-exception-conte","errorCode":null,"errorMessage":"{custom_llm_provider.capitalize()}Exception: Context Window Error - {error_str}","messagePattern":"(.+?)Exception: Context Window Error - (.+?)","errorType":"exception","errorClass":"ContextWindowExceededError","httpStatus":400,"severity":"error","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":725,"sourceCode":"    extra_information: str,\n) -> None:\n    if \"authorization denied for\" in error_str:\n        # Predibase returns the raw API Key in the response - this block ensures it's not returned in the exception\n        if error_str is not None and isinstance(error_str, str) and \"bearer\" in error_str.lower():\n            # only keep the first 10 chars after the occurnence of \"bearer\"\n            _bearer_token_start_index: Final = error_str.lower().find(\"bearer\")\n            error_str = error_str[: _bearer_token_start_index + 14]\n            error_str += \"XXXXXXX\" + '\"'\n\n        raise AuthenticationError(\n            message=f\"{custom_llm_provider.capitalize()}Exception: Authentication Error - {error_str}\",\n            llm_provider=custom_llm_provider,\n            model=model,\n            response=getattr(original_exception, \"response\", None),\n            litellm_debug_info=extra_information,\n        )\n    elif ExceptionCheckers.is_error_str_context_window_exceeded(error_str):\n        raise ContextWindowExceededError(\n            message=f\"{custom_llm_provider.capitalize()}Exception: Context Window Error - {error_str}\",\n            model=model,\n            llm_provider=custom_llm_provider,\n            response=getattr(original_exception, \"response\", None),\n            litellm_debug_info=extra_information,\n        )\n    elif \"token_quota_reached\" in error_str:\n        raise RateLimitError(\n            message=f\"{custom_llm_provider.capitalize()}Exception: Rate Limit Errror - {error_str}\",\n            llm_provider=custom_llm_provider,\n            model=model,\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"The server received an invalid response from an upstream server.\" in error_str:\n        raise litellm.InternalServerError(\n            message=f\"{custom_llm_provider.capitalize()}Exception - {original_exception.message}\",\n            llm_provider=custom_llm_provider,\n            model=model,","sourceCodeStart":707,"sourceCodeEnd":743,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/exception_mapping_utils.py#L707-L743","documentation":"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.","triggerScenarios":"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 - '.","commonSituations":"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.","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."],"exampleFix":"# before\nresp = litellm.completion(model=\"gpt-3.5-turbo\", messages=messages)\n\n# after\nn = litellm.token_counter(model=\"gpt-3.5-turbo\", messages=messages)\nlimit = litellm.get_max_tokens(model=\"gpt-3.5-turbo\")\nwhile n > limit - 500:\n    messages.pop(1)  # drop oldest turns, keep system prompt\n    n = litellm.token_counter(model=\"gpt-3.5-turbo\", messages=messages)\nresp = litellm.completion(model=\"gpt-3.5-turbo\", messages=messages)","handlingStrategy":"fallback","validationCode":"import litellm\n\ndef fits_context(model: str, messages: list) -> bool:\n    used = litellm.token_counter(model=model, messages=messages)\n    limit = litellm.get_max_tokens(model=model) or 4096\n    return used <= limit - 256  # headroom for the completion","typeGuard":"import litellm\n\ndef is_context_window_error(e: BaseException) -> bool:\n    return isinstance(e, litellm.ContextWindowExceededError)","tryCatchPattern":"try:\n    resp = litellm.completion(model=model, messages=messages)\nexcept litellm.ContextWindowExceededError:\n    messages = trim_history(messages, keep=8)\n    resp = litellm.completion(model=fallback_model, messages=messages)","preventionTips":["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."],"tags":["context-window","tokens","litellm","input-validation"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}