BerriAI/litellm · error · AnthropicError
{completion_response["error"]}
Error message
{completion_response["error"]} What it means
After a non-streaming call, LiteLLM checks the parsed response body for an 'error' key; if present it raises AnthropicError with that error payload as the message and the HTTP status code from the raw response. This is the Anthropic API's own error (overloaded, invalid_request, authentication, rate limit) surfaced as a Python exception — check status_code to classify it.
Source
Thrown at litellm/llms/anthropic/chat/transformation.py:2352
provider_specific_fields["compaction_blocks"] = compaction_blocks
return provider_specific_fields
def transform_parsed_response(
self,
completion_response: dict,
raw_response: httpx.Response,
model_response: ModelResponse,
json_mode: bool | None = None,
prefix_prompt: str | None = None,
speed: str | None = None,
tool_name_reverse_map: dict[str, str] | None = None,
):
_hidden_params: Final[dict] = {}
_hidden_params["additional_headers"] = process_anthropic_headers(dict(raw_response.headers))
if "error" in completion_response:
response_headers: Final = getattr(raw_response, "headers", None)
raise AnthropicError(
message=str(completion_response["error"]),
status_code=raw_response.status_code,
headers=response_headers,
)
(
text_content,
citations,
thinking_blocks,
reasoning_content,
tool_calls,
web_search_results,
tool_results,
compaction_blocks,
) = self.extract_response_content(completion_response=completion_response)
# Reverse-map rewritten tool names back to caller's originals so a
# downstream OpenAI-style dispatcher can match on the registered name.View on GitHub (pinned to 6c2dcb801b)
Solutions
- Inspect exception.status_code and the message body: 401 -> fix ANTHROPIC_API_KEY; 429 -> back off / raise limits; 529 -> retry with jitter; 400 -> fix request params.
- For 429/529 use litellm's built-in retries (num_retries) or exponential backoff around the call.
- Verify key and base URL env vars are set and current.
- Reduce max_tokens or trim input if the error mentions context length.
Example fix
# before
resp = litellm.completion(model="anthropic/claude-sonnet-4-5", messages=msgs)
# after
import litellm, time
for attempt in range(5):
try:
resp = litellm.completion(
model="anthropic/claude-sonnet-4-5", messages=msgs,
num_retries=3, # handles 429/529 automatically
)
break
except litellm.exceptions.AnthropicError as e:
if e.status_code in (429, 529) and attempt < 4:
time.sleep(2 ** attempt)
continue
raise Defensive patterns
Strategy: retry
Try / catch
from litellm.exceptions import AnthropicError
try:
resp = litellm.completion(model=MODEL, messages=messages, num_retries=3)
except AnthropicError as e:
if e.status_code == 401:
rotate_key_and_alert()
elif e.status_code in (429, 529):
schedule_retry_with_backoff()
elif e.status_code == 400:
fix_request_from_message(e.message)
raise Prevention
- Always set num_retries for 429/529 instead of hand-rolling loops.
- Monitor status_code distribution to catch key expiry and rate limits early.
- Trim context proactively to avoid 400 context-length rejections.
When it happens
Trigger: Any non-streaming anthropic/ completion where the API responds with an error JSON: 401 bad API key, 429 rate limit, 529 overloaded, 400 invalid_request_error (bad params, context length), or a proxy returning Anthropic-shaped error bodies.
Common situations: Hitting rate limits under load; expired/rotated API keys; requests exceeding max tokens/context; Anthropic capacity events (529); misconfigured LiteLLM proxy keys.
Related errors
- Missing Anthropic API Key
- Anthropic API key is required. Set ANTHROPIC_API_KEY or ANTH
- ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN is required for Sk
- BFL initial request failed: {initial_response.text}
- Failed to transform Braintrust response: {str(e)}
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/1db1ea02a747e960.
Report an issue: GitHub.