{"record":{"id":"70462b762bc6ed88","repo":"BerriAI/litellm","slug":"azureexception-contextwindowexceedederror-messa","errorCode":null,"errorMessage":"AzureException ContextWindowExceededError - {message}","messagePattern":"AzureException ContextWindowExceededError - (.+?)","errorType":"exception","errorClass":"ContextWindowExceededError","httpStatus":400,"severity":"error","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":1905,"sourceCode":"                if azure_error_code != \"content_policy_violation\":\n                    _inner: Final = body_dict[\"error\"].get(\"inner_error\") or body_dict[\"error\"].get(\"innererror\")\n                    if isinstance(_inner, dict) and _inner.get(\"code\") == \"ResponsibleAIPolicyViolation\":\n                        azure_error_code = \"content_policy_violation\"\n            else:\n                azure_error_code = body_dict.get(\"code\")\n    except Exception:\n        azure_error_code = None\n\n    if \"Internal server error\" in error_str:\n        raise litellm.InternalServerError(\n            message=f\"AzureException Internal server error - {message}\",\n            llm_provider=\"azure\",\n            model=model,\n            litellm_debug_info=extra_information,\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"This model's maximum context length is\" in error_str:\n        raise ContextWindowExceededError(\n            message=f\"AzureException ContextWindowExceededError - {message}\",\n            llm_provider=\"azure\",\n            model=model,\n            litellm_debug_info=extra_information,\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"DeploymentNotFound\" in error_str:\n        raise NotFoundError(\n            message=f\"AzureException NotFoundError - {message}\",\n            llm_provider=\"azure\",\n            model=model,\n            litellm_debug_info=extra_information,\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif azure_error_code == \"content_policy_violation\" or ExceptionCheckers.is_azure_content_policy_violation_error(\n        error_str\n    ):\n        from litellm.llms.azure.exception_mapping import (","sourceCodeStart":1887,"sourceCodeEnd":1923,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/exception_mapping_utils.py#L1887-L1923","documentation":"litellm maps Azure OpenAI errors containing \"This model's maximum context length is\" to litellm.ContextWindowExceededError. Prompt tokens plus requested completion exceed the deployed Azure model's context window.","triggerScenarios":"An Azure deployment call (model='azure/...') whose prompt plus max_tokens exceeds the underlying model's limit (e.g. 4k/8k/32k/128k depending on version); Azure returns the context-length error and litellm converts it, attaching litellm_debug_info.","commonSituations":"Long RAG contexts stuffed into gpt-35-turbo (4k) deployments; conversation histories that grow past the window mid-session; assuming 16k/32k limits on a 4k deployment SKU.","solutions":["Trim messages/summaries so prompt + max_tokens fit the deployed model's window","Point the call at a deployment of a larger-context model version (e.g. gpt-35-turbo-16k, gpt-4o)","Pre-count tokens with litellm.token_counter(model=<azure model>) and enforce a budget","Cap conversation history length with litellm Router's trim messages helpers"],"exampleFix":"# before\nlitellm.completion(model='azure/my-gpt35-deployment', messages=long_rag_msgs, max_tokens=2000)\n# after\nlimit = 16384 if '16k' in deployment_version else 4097\nmsgs = trim_messages_to(long_rag_msgs, limit - 2000 - 64)\nlitellm.completion(model='azure/my-gpt35-16k-deployment', messages=msgs, max_tokens=2000)","handlingStrategy":"validation","validationCode":"used = litellm.token_counter(model='gpt-3.5-turbo', messages=msgs) + max_tokens\nlimit = 4097  # per deployed version\nif used > limit:\n    msgs = trim_to_budget(msgs, limit - max_tokens)","typeGuard":"import litellm\n\ndef is_azure_context_overflow(e: Exception) -> bool:\n    return isinstance(e, litellm.ContextWindowExceededError) and 'azure' in str(getattr(e, 'llm_provider', ''))","tryCatchPattern":"try:\n    litellm.completion(...)\nexcept litellm.ContextWindowExceededError:\n    msgs = trim_to_budget(msgs)\n    return litellm.completion(..., messages=msgs)","preventionTips":["Enforce per-deployment token budgets in the app layer","Pick deployment SKUs sized to real context needs","Log token counts per request to catch growth trends"],"tags":["azure","context-window","validation","litellm"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}