BerriAI/litellm · error · ContextWindowExceededError

AlephAlphaException - {original_exception.message}

Error message

AlephAlphaException - {original_exception.message}

What it means

litellm raises litellm.ContextWindowExceededError when an Aleph Alpha error contains 'This is longer than the model's maximum context length'. The combined prompt (and requested completion) overflow the model's context window.

Source

Thrown at litellm/litellm_core_utils/exception_mapping_utils.py:1748

            message=f"TogetherAIException - {original_exception.message}",
            llm_provider="together_ai",
            model=model,
            request=getattr(original_exception, "request", None),
        )


def _map_aleph_alpha_exception(
    *,
    model: str,
    original_exception: _ProviderHTTPException,
    custom_llm_provider: str,
    error_str: str,
    exception_type: str,
    exception_provider: str,
    extra_information: str,
) -> None:
    if "This is longer than the model's maximum context length" in error_str:
        raise ContextWindowExceededError(
            message=f"AlephAlphaException - {original_exception.message}",
            llm_provider="aleph_alpha",
            model=model,
            response=getattr(original_exception, "response", None),
        )
    elif "InvalidToken" in error_str or "No token provided" in error_str:
        raise BadRequestError(
            message=f"AlephAlphaException - {original_exception.message}",
            llm_provider="aleph_alpha",
            model=model,
            response=getattr(original_exception, "response", None),
        )
    elif hasattr(original_exception, "status_code"):
        verbose_logger.debug("status code: %s", original_exception.status_code)
        if original_exception.status_code == 401:
            raise AuthenticationError(
                message=f"AlephAlphaException - {original_exception.message}",
                llm_provider="aleph_alpha",

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Trim or summarize the input to fit the model's context window
  2. Switch to a model/deployment with a larger context window
  3. Count tokens with litellm.token_counter before the call and drop oldest turns
  4. Chunk long documents and aggregate answers instead of one giant prompt

Example fix

# before
litellm.completion(model='aleph_alpha/luminous-supreme', messages=entire_history)
# after
budget = litellm.get_max_tokens('aleph_alpha/luminous-supreme') or 2048
msgs = trim_to_budget(entire_history, budget - 512)
litellm.completion(model='aleph_alpha/luminous-supreme', messages=msgs, max_tokens=512)
Defensive patterns

Strategy: validation

Validate before calling

used = litellm.token_counter(model='aleph_alpha/luminous-supreme', messages=msgs) + max_tokens
limit = litellm.get_max_tokens('aleph_alpha/luminous-supreme') or 2048
if used > limit:
    msgs = trim_messages(msgs, limit - max_tokens - 64)

Type guard

import litellm

def is_context_overflow(e: Exception) -> bool:
    return isinstance(e, litellm.ContextWindowExceededError)

Try / catch

try:
    litellm.completion(...)
except litellm.ContextWindowExceededError:
    msgs = trim_to_budget(msgs)
    return litellm.completion(..., messages=msgs)  # one retry, smaller

Prevention

When it happens

Trigger: Calling model='aleph_alpha/...' with a prompt whose token count plus max_tokens exceeds the model's context limit (e.g. Luminous models); Aleph Alpha returns the context-length error and this mapper converts it.

Common situations: Stuffing long documents into a prompt without token counting; switching from a large-context model to Luminous without resizing inputs; inheriting conversation histories that grow unboundedly.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/82db2312393f3d7d. Report an issue: GitHub.