{"record":{"id":"395e524d24fb1ff3","repo":"BerriAI/litellm","slug":"bedrockexception-context-window-error-error-st","errorCode":null,"errorMessage":"BedrockException: Context Window Error - {error_str}","messagePattern":"BedrockException: Context Window Error - (.+?)","errorType":"exception","errorClass":"ContextWindowExceededError","httpStatus":400,"severity":"error","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":832,"sourceCode":"def _map_bedrock_exception(\n    *,\n    model: str,\n    original_exception: _ProviderHTTPException,\n    custom_llm_provider: str,\n    error_str: str,\n    exception_type: str,\n    exception_provider: str,\n    extra_information: str,\n) -> None:\n    if (\n        \"too many tokens\" in error_str\n        or \"expected maxLength:\" in error_str\n        or \"Input is too long\" in error_str\n        or \"prompt is too long\" in error_str\n        or \"prompt: length: 1..\" in error_str\n        or \"Too many input tokens\" in error_str\n    ):\n        raise ContextWindowExceededError(\n            message=f\"BedrockException: Context Window Error - {error_str}\",\n            model=model,\n            llm_provider=\"bedrock\",\n        )\n    elif \"Conversation blocks and tool result blocks cannot be provided in the same turn.\" in error_str:\n        raise BadRequestError(\n            message=f\"BedrockException - {error_str}\\n. Enable 'litellm.modify_params=True' (for PROXY do: `litellm_settings::modify_params: True`) to insert a dummy assistant message and fix this error.\",\n            model=model,\n            llm_provider=\"bedrock\",\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"Malformed input request\" in error_str:\n        raise BadRequestError(\n            message=f\"BedrockException - {error_str}\",\n            model=model,\n            llm_provider=\"bedrock\",\n            response=getattr(original_exception, \"response\", None),\n        )","sourceCodeStart":814,"sourceCodeEnd":850,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/exception_mapping_utils.py#L814-L850","documentation":"LiteLLM throws ContextWindowExceededError (a BadRequestError subclass, HTTP 400) when AWS Bedrock rejects a request whose input exceeds the model's context window. The mapper at litellm/litellm_core_utils/exception_mapping_utils.py:832 matches the provider strings ('too many tokens', 'expected maxLength:', 'Input is too long', 'prompt is too long', 'prompt: length: 1..', 'Too many input tokens') from Titan, Anthropic-on-Bedrock, and Cohere, and re-raises them as one normalized type so cross-provider code needs a single except clause. Retrying the identical payload always fails again: the input must shrink or the window must grow.","triggerScenarios":"litellm.completion(model='bedrock/...', messages=[...]) where prompt tokens (or prompt + max_tokens) exceed the deployed model's limit: Amazon Titan returns 'Input is too long', Anthropic on Bedrock returns 'prompt is too long' / 'expected maxLength:', Cohere returns 'Too many input tokens'. Long chat histories accumulated across agent turns are the most common producer.","commonSituations":"RAG pipelines stuffing many retrieved chunks into one prompt; sending whole files to a small-window model (Titan Express is 8k); unbounded chat history growth in chat apps; budgeting only prompt tokens and forgetting that max_tokens counts against the same window.","solutions":["Shrink the input: drop oldest messages, cut retrieved chunk count, or summarize the context until under the limit","Pre-flight the budget with litellm.get_max_tokens(model) and litellm.token_counter(model=..., messages=...) and cap max_tokens to (window - input - margin)","Switch to a Bedrock model variant with a larger context window (Claude / Titan Premier instead of Titan Express)","In the proxy, trim requests via litellm_settings (e.g. history caps / drop_params) so oversized payloads never reach Bedrock"],"exampleFix":"# before\nresp = litellm.completion(model=\"bedrock/amazon.titan-text-express-v1\", messages=all_messages, max_tokens=1024)\n\n# after\nlimit = litellm.get_max_tokens(\"bedrock/amazon.titan-text-express-v1\")\nwhile litellm.token_counter(model=\"bedrock/amazon.titan-text-express-v1\", messages=all_messages) + 1024 > limit - 64:\n    all_messages = [all_messages[0]] + all_messages[2:]  # drop oldest turn, keep system\nresp = litellm.completion(model=\"bedrock/amazon.titan-text-express-v1\", messages=all_messages, max_tokens=1024)","handlingStrategy":"validation","validationCode":"import litellm\n\ndef fits_context(model: str, messages: list, max_tokens: int, margin: int = 64) -> bool:\n    limit = litellm.get_max_tokens(model) or 0\n    used = litellm.token_counter(model=model, messages=messages) + max_tokens\n    return used <= limit - margin","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 = [messages[0]] + messages[2:]  # drop oldest turn, keep system\n    resp = litellm.completion(model=model, messages=messages)  # retry once, trimmed","preventionTips":["Budget prompt + max_tokens with litellm.token_counter and litellm.get_max_tokens before every call","Cap conversation history length (sliding window) in chat applications","Pick a Bedrock model whose context window covers your worst-case document","Treat ContextWindowExceededError as permanent for that payload - never blind-retry unchanged"],"tags":["bedrock","context-window","prompt-too-long","tokens","llm"],"backgroundTag":"context-length-exceeded","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}