The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Merge branch 'litellm_internal_staging' into litellm_fix_bedrock_adaptive_thinking_token_accounting
Devin AIaadfa89ff697
diff --cc litellm/llms/anthropic/chat/transformation.pyindex 5c27535014a,7bbdb9a43fd..e0e11be356c--- a/litellm/llms/anthropic/chat/transformation.py+++ b/litellm/llms/anthropic/chat/transformation.py@@@ -2107,66 -2102,21 +2105,81 @@@ class AnthropicConfig(AnthropicModelInfcompaction_blocks,)+ @staticmethod+ def _thinking_tokens_from_usage(usage_object: Mapping[str, object]) -> int | None:+ details: Final = usage_object.get("output_tokens_details")+ if not isinstance(details, Mapping):+ return None+ try:+ return AnthropicOutputTokensDetails.model_validate(details).thinking_tokens+ except ValidationError:+ return None++ @staticmethod+ def _response_has_thinking_block(completion_response: Mapping[str, object] | None) -> bool:+ if completion_response is None:+ return False+ content: Final = completion_response.get("content")+ if not isinstance(content, list):+ return False+ return any(+ isinstance(block, Mapping) and block.get("type") in ("thinking", "redacted_thinking") for block in content+ )++ def _build_completion_token_details(+ self,+ usage_object: Mapping[str, object],+ iterations: Sequence[object] | None,+ completion_tokens: int,+ reasoning_content: str | None,+ completion_response: Mapping[str, object] | None,+ ) -> CompletionTokensDetailsWrapper:+ reported_thinking_tokens: Final = (+ self._sum_iteration_thinking_tokens(iterations)+ if iterations+ else self._thinking_tokens_from_usage(usage_object)+ )+ if reported_thinking_tokens is not None:+ capped_reported: Final = min(max(0, reported_thinking_tokens), completion_tokens)+ return CompletionTokensDetailsWrapper(+ reasoning_tokens=capped_reported,+ text_tokens=completion_tokens - capped_reported,+ )+ if reasoning_content:+ estimated: Final = min(+ token_counter(text=reasoning_content, count_response_tokens=True),+ completion_tokens,+ )+ return CompletionTokensDetailsWrapper(+ reasoning_tokens=max(0, estimated),+ text_tokens=completion_tokens - max(0, estimated),+ )+ if self._response_has_thinking_block(completion_response):+ return CompletionTokensDetailsWrapper(reasoning_tokens=None, text_tokens=None)+ return CompletionTokensDetailsWrapper(reasoning_tokens=0, text_tokens=completion_tokens)++ def _sum_iteration_thinking_tokens(self, iterations: Sequence[object]) -> int | None:+ per_iteration: Final = tuple(+ self._thinking_tokens_from_usage(iteration) for iteration in iterations if isinstance(iteration, Mapping)+ )+ reported: Final = tuple(tokens for tokens in per_iteration if tokens is not None)+ return sum(reported) if reported else None++ @staticmethod+ def is_anthropic_usage_object(usage_object: dict) -> bool:+ """Anthropic reports prompt cache tokens as top-level ``cache_read_input_tokens`` /+ ``cache_creation_input_tokens``; no other API surface uses those keys, and the+ Responses API mapping would silently drop them.++ Requiring a cache key is deliberate: Responses API usage also carries top-level+ ``input_tokens``, so the cache keys are the only shape discriminator between the+ two. A cache-free Anthropic payload falls through to the Responses API mapping,+ which is safe because both mappings agree whenever no cache tokens are present.+ """+ if "prompt_tokens" in usage_object or "input_tokens" not in usage_object:+ return False+ return any(key in usage_object for key in ("cache_read_input_tokens", "cache_creation_input_tokens"))+def calculate_usage(self,usage_object: dict,