{"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/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/exception_mapping_utils.py#L814-L850","documentation":"Bedrock-specific mapping: when the AWS error text matches 'too many tokens', 'expected maxLength:', 'Input is too long', 'prompt is too long', 'prompt: length: 1..', or 'Too many input tokens', LiteLLM raises ContextWindowExceededError with llm_provider='bedrock'. The prompt exceeds the model's context window as enforced by Bedrock.","triggerScenarios":"bedrock completion/converse calls where input tokens exceed the model limit — e.g. Titan (4k/8k), older Claude on Bedrock, Llama models with small windows, or huge tool definitions plus long history.","commonSituations":"Assuming a large context because the API accepts it, but the Bedrock model id has a tighter limit; RAG over-stuffing; or summing system prompt + tools + history beyond e.g. 4096 tokens on small-window models.","solutions":["Trim/summarize history and retrieved chunks before the call.","Check the limit with litellm.get_max_tokens(model='bedrock/<model-id>') and count with litellm.token_counter.","Switch to a Bedrock model with a larger context (e.g. Claude 3.5+ with 200k).","Reduce tool definitions or compress the system prompt."],"exampleFix":"# before\nresp = litellm.completion(model=\"bedrock/amazon.titan-text-express-v1\", messages=msgs)\n\n# after\nmodel = \"bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0\"\nmax_tok = litellm.get_max_tokens(model)\nwhile litellm.token_counter(model=model, messages=msgs) > max_tok - 512:\n    msgs.pop(1)\nresp = litellm.completion(model=model, messages=msgs)","handlingStrategy":"fallback","validationCode":"import litellm\n\ndef fits_bedrock_context(bedrock_model: str, messages: list, reserve: int = 256) -> bool:\n    limit = litellm.get_max_tokens(model=bedrock_model) or 4096\n    return litellm.token_counter(model=bedrock_model, messages=messages) <= limit - reserve","typeGuard":"import litellm\n\ndef is_bedrock_context_error(e: BaseException) -> bool:\n    return isinstance(e, litellm.ContextWindowExceededError) and getattr(e, \"llm_provider\", \"\") == \"bedrock\"","tryCatchPattern":"try:\n    resp = litellm.completion(model=\"bedrock/...\", messages=messages)\nexcept litellm.ContextWindowExceededError:\n    messages = summarize_history(messages)\n    resp = litellm.completion(model=\"bedrock/<larger-context-model>\", messages=messages)","preventionTips":["Check litellm.get_max_tokens for the exact bedrock model id — windows vary sharply across Titan/Llama/Claude.","Token-count prompts before sending; Bedrock enforces hard limits.","Trim tool definitions and system prompt — they count against the window."],"tags":["bedrock","context-window","tokens","aws","litellm"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}