BerriAI/litellm · error · BedrockError

{error_text}

Error message

{error_text}

What it means

A BedrockError whose message is the raw response body returned by the AWS CountTokens endpoint when it answers with a non-200 status. LiteLLM's Anthropic-compatible count_tokens handler signs and posts the request, sees response.status_code != 200, logs the body via verbose_logger, and rethrows it verbatim — so the actual cause is whatever JSON/text AWS sent (validation error, model not found, access denied, throttling).

Solutions

  1. Read message of the BedrockError — it is the verbatim AWS error JSON which names the exact problem (e.g. ValidationException, AccessDeniedException, ThrottlingException)
  2. Verify the model ID exists and is enabled in the region (aws bedrock list-foundation-models --region <region>)
  3. Check IAM: the caller needs bedrock permissions for the model ARN; for throttling, slow the request rate or request a quota increase
  4. If credentials drift is suspected (expired session token), refresh credentials and retry

Example fix

# before
resp = await client.count_tokens(model='bedrock/anthropic.claude-3-5-sonnet', messages=msgs)

# after
try:
    resp = await client.count_tokens(model='bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0', messages=msgs)
except BedrockError as e:
    logging.error('AWS said: %s', e.message)  # verbatim AWS error body
    raise
Defensive patterns

Strategy: try-catch

Validate before calling

import re

BEDROCK_MODEL_RE = re.compile(r'^[a-zA-Z0-9.-]+([:/][a-zA-Z0-9._-]+)*$')

def assert_model_id_plausible(model: str, region: str) -> None:
    assert model and BEDROCK_MODEL_RE.match(model), f'implausible model id: {model!r}'
    assert re.match(r'^[a-z]{2}-[a-z]+-\d+$', region), f'implausible region: {region!r}'

Try / catch

from litellm.llms.bedrock.common_utils import BedrockError

try:
    counts = await count_tokens_handler.count_tokens(request)
except BedrockError as e:
    body = e.message or ''
    if 'ValidationException' in body:
        fix_request_shape(body)          # AWS tells you the bad field
    elif 'AccessDenied' in body:
        raise PermissionsError(body) from e
    elif 'Throttling' in body:
        await backoff_retry()            # retry with jitter
    else:
        raise

Prevention

When it happens

Trigger: POSTing a count-tokens request with a modelId that does not exist in the configured region; missing bedrock:InvokeModel/CountTokens IAM permissions; an unsupported model family for the count-tokens API; AWS throttling (429) or a malformed body that fails AWS-side validation — any non-200 from bedrock-runtime.{region}.amazonaws.com/model/{id}/invoke .../count-tokens style endpoints.

Common situations: Using anthropic.count_tokens style calls through bedrock with a typo'd model ID; model invoked in the wrong region (model not enabled there); expired AWS credentials or an assumed-role without bedrock permissions; hitting account-level TPS limits during load tests.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/4770b287fda064b2. Report an issue: GitHub.

Appendix: source

Thrown at litellm/llms/bedrock/count_tokens/handler.py:102

                model=resolved_model,
                api_key=api_key,
            )

            async_client: Final = get_async_httpx_client(llm_provider=litellm.LlmProviders.BEDROCK)

            response: Final = await async_client.post(
                endpoint_url,
                headers=signed_headers,
                data=signed_body,
                timeout=30.0,
            )

            verbose_logger.debug("Response status: %s", response.status_code)

            if response.status_code != 200:
                error_text: Final = response.text
                verbose_logger.error("AWS Bedrock error: %s", error_text)
                raise BedrockError(
                    status_code=response.status_code,
                    message=error_text,
                )

            bedrock_response: Final = response.json()

            verbose_logger.debug("Bedrock response: %s", bedrock_response)

            # Transform response back to expected format
            final_response: Final = self.transform_bedrock_response_to_anthropic(bedrock_response)

            verbose_logger.debug("Final response: %s", final_response)

            return final_response

        except BedrockError:
            # Re-raise Bedrock exceptions as-is
            raise

View on GitHub (pinned to 77b7c6c40c)