BerriAI/litellm · error · ContextWindowExceededError
BedrockException: Context Window Error - {error_str}
Error message
BedrockException: Context Window Error - {error_str} What it means
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.
Source
Thrown at litellm/litellm_core_utils/exception_mapping_utils.py:832
def _map_bedrock_exception(
*,
model: str,
original_exception: _ProviderHTTPException,
custom_llm_provider: str,
error_str: str,
exception_type: str,
exception_provider: str,
extra_information: str,
) -> None:
if (
"too many tokens" in error_str
or "expected maxLength:" in error_str
or "Input is too long" in error_str
or "prompt is too long" in error_str
or "prompt: length: 1.." in error_str
or "Too many input tokens" in error_str
):
raise ContextWindowExceededError(
message=f"BedrockException: Context Window Error - {error_str}",
model=model,
llm_provider="bedrock",
)
elif "Conversation blocks and tool result blocks cannot be provided in the same turn." in error_str:
raise BadRequestError(
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.",
model=model,
llm_provider="bedrock",
response=getattr(original_exception, "response", None),
)
elif "Malformed input request" in error_str:
raise BadRequestError(
message=f"BedrockException - {error_str}",
model=model,
llm_provider="bedrock",
response=getattr(original_exception, "response", None),
)View on GitHub (pinned to 6c2dcb801b)
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.
Example fix
# before
resp = litellm.completion(model="bedrock/amazon.titan-text-express-v1", messages=msgs)
# after
model = "bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0"
max_tok = litellm.get_max_tokens(model)
while litellm.token_counter(model=model, messages=msgs) > max_tok - 512:
msgs.pop(1)
resp = litellm.completion(model=model, messages=msgs) Defensive patterns
Strategy: fallback
Validate before calling
import litellm
def fits_bedrock_context(bedrock_model: str, messages: list, reserve: int = 256) -> bool:
limit = litellm.get_max_tokens(model=bedrock_model) or 4096
return litellm.token_counter(model=bedrock_model, messages=messages) <= limit - reserve Type guard
import litellm
def is_bedrock_context_error(e: BaseException) -> bool:
return isinstance(e, litellm.ContextWindowExceededError) and getattr(e, "llm_provider", "") == "bedrock" Try / catch
try:
resp = litellm.completion(model="bedrock/...", messages=messages)
except litellm.ContextWindowExceededError:
messages = summarize_history(messages)
resp = litellm.completion(model="bedrock/<larger-context-model>", messages=messages) Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- {custom_llm_provider.capitalize()}Exception: Context Window
- BedrockException Invalid Authentication - {error_str}
- BedrockException PermissionDeniedError - {error_str}
- BedrockException: Rate Limit Error - {error_str}
- BedrockException: Timeout Error - {error_str}
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/395e524d24fb1ff3.
Report an issue: GitHub.