{"record":{"id":"48ecfeb39a953ec7","repo":"BerriAI/litellm","slug":"ai21exception-original-exception-message","errorCode":null,"errorMessage":"AI21Exception - {original_exception.message}","messagePattern":"AI21Exception - (.+?)","errorType":"exception","errorClass":"ContextWindowExceededError","httpStatus":400,"severity":"error","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":1505,"sourceCode":"                llm_provider=\"huggingface\",\n                model=model,\n                request=getattr(original_exception, \"request\", None),\n            )\n\n\ndef _map_ai21_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 hasattr(original_exception, \"message\"):\n        if \"Prompt has too many tokens\" in original_exception.message:\n            raise ContextWindowExceededError(\n                message=f\"AI21Exception - {original_exception.message}\",\n                model=model,\n                llm_provider=\"ai21\",\n                response=getattr(original_exception, \"response\", None),\n            )\n        if \"Bad or missing API token.\" in original_exception.message:\n            raise BadRequestError(\n                message=f\"AI21Exception - {original_exception.message}\",\n                model=model,\n                llm_provider=\"ai21\",\n                response=getattr(original_exception, \"response\", None),\n            )\n    if hasattr(original_exception, \"status_code\"):\n        if original_exception.status_code == 401:\n            raise AuthenticationError(\n                message=f\"AI21Exception - {original_exception.message}\",\n                llm_provider=\"ai21\",\n                model=model,","sourceCodeStart":1487,"sourceCodeEnd":1523,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/exception_mapping_utils.py#L1487-L1523","documentation":"LiteLLM's _map_ai21_exception raises ContextWindowExceededError when the AI21 error message contains 'Prompt has too many tokens'. It is LiteLLM's normalized signal that the input exceeded the model's maximum context length (e.g. Jurrassic-2 / Jamba limits).","triggerScenarios":"completion('ai21/<model>', messages=long_history) where total tokens exceed the model's context window - AI21 rejects with 'Prompt has too many tokens' and this branch re-raises it as ContextWindowExceededError.","commonSituations":"Long chat histories without truncation, RAG pipelines stuffing too many retrieved chunks, or switching from a large-context model (Jamba 256k) to a smaller-context one without re-tuning prompt budgets.","solutions":["Count and truncate inputs before the call: use litellm.token_counter(model, messages=messages) and trim history/chunks to fit.","Switch to a larger-context AI21 model (e.g. jamba-1.5-large) or another provider via Router fallback.","Implement sliding-window or summarize-older-turns trimming for chat history.","Catch ContextWindowExceededError specifically and degrade gracefully (truncate + retry once)."],"exampleFix":"# before\nlitellm.completion(model='ai21/jamba-1.5-mini', messages=messages)  # too many tokens\n\n# after\nfrom litellm import token_counter\nMAX = 100_000\nwhile messages and token_counter(model='ai21/jamba-1.5-mini', messages=messages) > MAX:\n    messages.pop(0)\nlitellm.completion(model='ai21/jamba-1.5-mini', messages=messages)","handlingStrategy":"validation","validationCode":"from litellm import token_counter\n\ndef fits_context(model: str, messages: list, budget: int = 250_000) -> bool:\n    try:\n        return token_counter(model=model, messages=messages) < budget\n    except Exception:\n        return len(str(messages)) // 4 < budget  # rough fallback estimate","typeGuard":"from litellm import ContextWindowExceededError\n\ndef is_context_exceeded(e: BaseException) -> bool:\n    return isinstance(e, ContextWindowExceededError)","tryCatchPattern":"from litellm import ContextWindowExceededError\n\ntry:\n    resp = litellm.completion(model='ai21/jamba-1.5-mini', messages=messages)\nexcept ContextWindowExceededError:\n    messages = messages[: len(messages) // 2]  # trim oldest turns\n    resp = litellm.completion(model='ai21/jamba-1.5-mini', messages=messages)","preventionTips":["Count tokens with litellm.token_counter before every large call.","Trim chat history to a rolling window sized under the model limit.","Store each model's context limit in config and validate inputs against it."],"tags":["ai21","context-window","token-limit","litellm","prompt-too-long"],"backgroundTag":"context-length-exceeded","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}