{"record":{"id":"4770b287fda064b2","repo":"BerriAI/litellm","slug":"error-text","errorCode":null,"errorMessage":"{error_text}","messagePattern":"\\{error_text\\}","errorType":"http","errorClass":"BedrockError","httpStatus":null,"severity":"error","filePath":"litellm/llms/bedrock/count_tokens/handler.py","lineNumber":102,"sourceCode":"                model=resolved_model,\n                api_key=api_key,\n            )\n\n            async_client: Final = get_async_httpx_client(llm_provider=litellm.LlmProviders.BEDROCK)\n\n            response: Final = await async_client.post(\n                endpoint_url,\n                headers=signed_headers,\n                data=signed_body,\n                timeout=30.0,\n            )\n\n            verbose_logger.debug(\"Response status: %s\", response.status_code)\n\n            if response.status_code != 200:\n                error_text: Final = response.text\n                verbose_logger.error(\"AWS Bedrock error: %s\", error_text)\n                raise BedrockError(\n                    status_code=response.status_code,\n                    message=error_text,\n                )\n\n            bedrock_response: Final = response.json()\n\n            verbose_logger.debug(\"Bedrock response: %s\", bedrock_response)\n\n            # Transform response back to expected format\n            final_response: Final = self.transform_bedrock_response_to_anthropic(bedrock_response)\n\n            verbose_logger.debug(\"Final response: %s\", final_response)\n\n            return final_response\n\n        except BedrockError:\n            # Re-raise Bedrock exceptions as-is\n            raise","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/bedrock/count_tokens/handler.py#L84-L120","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read message of the BedrockError — it is the verbatim AWS error JSON which names the exact problem (e.g. ValidationException, AccessDeniedException, ThrottlingException)","Verify the model ID exists and is enabled in the region (aws bedrock list-foundation-models --region <region>)","Check IAM: the caller needs bedrock permissions for the model ARN; for throttling, slow the request rate or request a quota increase","If credentials drift is suspected (expired session token), refresh credentials and retry"],"exampleFix":"# before\nresp = await client.count_tokens(model='bedrock/anthropic.claude-3-5-sonnet', messages=msgs)\n\n# after\ntry:\n    resp = await client.count_tokens(model='bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0', messages=msgs)\nexcept BedrockError as e:\n    logging.error('AWS said: %s', e.message)  # verbatim AWS error body\n    raise","handlingStrategy":"try-catch","validationCode":"import re\n\nBEDROCK_MODEL_RE = re.compile(r'^[a-zA-Z0-9.-]+([:/][a-zA-Z0-9._-]+)*$')\n\ndef assert_model_id_plausible(model: str, region: str) -> None:\n    assert model and BEDROCK_MODEL_RE.match(model), f'implausible model id: {model!r}'\n    assert re.match(r'^[a-z]{2}-[a-z]+-\\d+$', region), f'implausible region: {region!r}'","typeGuard":null,"tryCatchPattern":"from litellm.llms.bedrock.common_utils import BedrockError\n\ntry:\n    counts = await count_tokens_handler.count_tokens(request)\nexcept BedrockError as e:\n    body = e.message or ''\n    if 'ValidationException' in body:\n        fix_request_shape(body)          # AWS tells you the bad field\n    elif 'AccessDenied' in body:\n        raise PermissionsError(body) from e\n    elif 'Throttling' in body:\n        await backoff_retry()            # retry with jitter\n    else:\n        raise","preventionTips":["Keep a registry of model IDs validated against aws bedrock list-foundation-models per region","Verify IAM policies grant bedrock:InvokeModel on the model ARN before shipping","Enable LITELLM_LOG=DEBUG in non-prod to see the logged AWS error body early"],"tags":["bedrock","aws","count-tokens","api-error","anthropic"],"backgroundTag":"aws-api-error","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}