Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def smss_stream(
note_output_stop,
)


class ToolCall(BaseModel):
name: str
description: Optional[str] = None
Expand Down Expand Up @@ -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
Expand All @@ -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 = ""
Expand All @@ -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,
Expand All @@ -489,13 +493,23 @@ 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
+ (cache_read_tokens or 0)
+ (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'} "
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 = []
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Loading