diff --git a/py/genai_client/text_generation/anthropic_client/anthropic_text_client.py b/py/genai_client/text_generation/anthropic_client/anthropic_text_client.py index 0c0fec8e66..b9d0428513 100644 --- a/py/genai_client/text_generation/anthropic_client/anthropic_text_client.py +++ b/py/genai_client/text_generation/anthropic_client/anthropic_text_client.py @@ -41,7 +41,6 @@ def smss_stream( note_output_stop, ) - class ToolCall(BaseModel): name: str description: Optional[str] = None @@ -458,6 +457,13 @@ def ask_call( _cache_creation = ( getattr(response.usage, "cache_creation_input_tokens", None) or 0 ) + _thinking_tokens = ( + getattr( + getattr(response.usage, "output_tokens_details", None), + "thinking_tokens", + None, + ) or None + ) return self._parse_tools_call_response( response, prompt_tokens=response.usage.input_tokens @@ -466,6 +472,7 @@ def ask_call( response_tokens=response.usage.output_tokens, cache_read_tokens=_cache_read, cache_creation_tokens=_cache_creation, + thinking_tokens=_thinking_tokens, ) thinking_text = "" @@ -476,9 +483,6 @@ def ask_call( elif hasattr(content, "type") and content.type == "text": response_text += content.text - if web_search_enabled and inline_citations_enabled: - response_text = self._add_inline_citations(response) or response_text - usage = Usage( input_tokens=response.usage.input_tokens, output_tokens=response.usage.output_tokens, @@ -489,6 +493,13 @@ def ask_call( cache_creation_tokens = ( getattr(response.usage, "cache_creation_input_tokens", None) or None ) + thinking_tokens = ( + getattr( + getattr(response.usage, "output_tokens_details", None), + "thinking_tokens", + None, + ) or None + ) # Normalize Anthropic input_tokens (new-only) to total billed, matching OpenAI/Gemini total_input_tokens = ( usage.input_tokens @@ -496,6 +507,9 @@ def ask_call( + (cache_creation_tokens or 0) ) + if web_search_enabled and inline_citations_enabled: + response_text = self._add_inline_citations(response) or response_text + if self.prompt_caching and (cache_read_tokens or cache_creation_tokens): print( f"[prompt_caching] ttl={self.cache_ttl or '5m'} " @@ -522,6 +536,7 @@ def ask_call( prompt_tokens=total_input_tokens, cache_read_tokens=cache_read_tokens, cache_creation_tokens=cache_creation_tokens, + thinking_tokens=thinking_tokens, schemaVersion=2, io="OUTPUT", parts=parts, @@ -550,6 +565,7 @@ def _apply_usage_ceiling(self, request_config: AnthropicRequestConfig) -> None: return request_config.max_tokens = fit.max_tokens + thinking = request_config.thinking if not thinking or thinking.get("type") != "enabled": return @@ -630,6 +646,7 @@ def _parse_tools_call_response( response_tokens: int = 0, cache_read_tokens: Optional[int] = None, cache_creation_tokens: Optional[int] = None, + thinking_tokens: Optional[int] = None, ) -> AskModelEngineResponse2: tools_result = [] # preamble text blocks the model emitted alongside the tool_use blocks @@ -656,6 +673,7 @@ def _parse_tools_call_response( prompt_tokens=prompt_tokens, cache_read_tokens=cache_read_tokens, cache_creation_tokens=cache_creation_tokens, + thinking_tokens=thinking_tokens, schemaVersion=2, io="OUTPUT", parts=parts, @@ -670,6 +688,7 @@ def _parse_tools_call_response( prompt_tokens=prompt_tokens, cache_read_tokens=cache_read_tokens, cache_creation_tokens=cache_creation_tokens, + thinking_tokens=thinking_tokens, schemaVersion=2, io="OUTPUT", parts=text_parts @@ -690,6 +709,7 @@ def _handle_streaming( output_tokens = 0 cache_read_tokens: Optional[int] = None cache_creation_tokens: Optional[int] = None + thinking_tokens: Optional[int] = None stop_reason: Optional[str] = None content_array = [] @@ -917,6 +937,13 @@ def _handle_streaming( elif event.type == "message_delta": output_tokens = event.usage.output_tokens + thinking_tokens = ( + getattr( + getattr(event.usage, "output_tokens_details", None), + "thinking_tokens", + None, + ) or None + ) if getattr(event, "delta", None) and getattr( event.delta, "stop_reason", None ): @@ -1064,6 +1091,7 @@ def _handle_streaming( prompt_tokens=total_input_tokens, cache_read_tokens=cache_read_tokens, cache_creation_tokens=cache_creation_tokens, + thinking_tokens=thinking_tokens, schemaVersion=2, io="OUTPUT", parts=parts, @@ -1077,6 +1105,7 @@ def _handle_streaming( prompt_tokens=total_input_tokens, cache_read_tokens=cache_read_tokens, cache_creation_tokens=cache_creation_tokens, + thinking_tokens=thinking_tokens, schemaVersion=2, io="OUTPUT", parts=parts, @@ -1090,6 +1119,7 @@ def _handle_streaming( response_tokens=output_tokens, cache_read_tokens=cache_read_tokens, cache_creation_tokens=cache_creation_tokens, + thinking_tokens=thinking_tokens, schemaVersion=2, io="OUTPUT", parts=parts,