{"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/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/exception_mapping_utils.py#L707-L743","documentation":"When ExceptionCheckers.is_error_str_context_window_exceeded matches the provider error text (an 'input is too long' style message from watsonx, databricks, predibase, or lemonade), litellm raises ContextWindowExceededError. It means the assembled prompt plus reserved completion tokens exceed the target model's context window. The litellm_debug_info field carries the provider's extra detail to help identify which part overflowed.","triggerScenarios":"Long chat histories sent to a small-context watsonx model; RAG stuffing too many retrieved chunks; max_tokens large enough that prompt plus completion no longer fits the window.","commonSituations":"Conversation apps that accumulate messages forever; switching to a model with a smaller window without changing trimming logic; document QA that injects whole files.","solutions":["Trim or summarize older messages before sending (sliding window or map-reduce)","Preflight with litellm.token_counter(model, messages=...) against the model's limit","Reduce max_tokens to leave room for the prompt","Switch to a variant with a larger context window"],"exampleFix":"# before - full history blows the window\nresp = litellm.completion(model=\"watsonx/ibm/granite-3-8b-instruct\", messages=all_messages)\n\n# after - preflight token count, trim oldest turns\nmodel = \"watsonx/ibm/granite-3-8b-instruct\"\nlimit = 4096\nused = litellm.token_counter(model=model, messages=all_messages)\nwhile used > limit - 512:  # reserve space for the answer\n    all_messages = [all_messages[0]] + all_messages[2:]  # keep the system prompt\n    used = litellm.token_counter(model=model, messages=all_messages)\nresp = litellm.completion(model=model, messages=all_messages)","handlingStrategy":"validation","validationCode":"import litellm\n\ndef fits_context(model: str, messages: list[dict], max_tokens: int = 512) -> bool:\n    used = litellm.token_counter(model=model, messages=messages)\n    limit = litellm.get_max_tokens(model) or 4096\n    return used + max_tokens <= limit\n\nif not fits_context(model, messages):\n    messages = condense(messages)","typeGuard":"import litellm\n\ndef is_context_window_error(exc: BaseException) -> bool:\n    return isinstance(exc, litellm.ContextWindowExceededError)","tryCatchPattern":"try:\n    resp = litellm.completion(model=model, messages=messages)\nexcept litellm.ContextWindowExceededError:\n    messages = condense(messages)  # summarize older turns, keep system + latest\n    resp = litellm.completion(model=model, messages=messages)","preventionTips":["Track token counts per conversation and trim before the call, not after the error","Reserve headroom: prompt + max_tokens must stay under the window","Bound RAG chunk counts per model"],"tags":["litellm","watsonx","context-window","token-limit","prompt-too-long"],"backgroundTag":"context-window-exceeded","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}