Skip to content

Commit f7f52ca

Browse files
authored
anthropic[patch]: cache tokens nit (#31484)
if you pass in beta headers directly cache_creation is a dict
1 parent 14c561e commit f7f52ca

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

libs/partners/anthropic/langchain_anthropic/chat_models.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,11 +2131,12 @@ def _create_usage_metadata(anthropic_usage: BaseModel) -> UsageMetadata:
21312131
}
21322132
# Add (beta) cache TTL information if available
21332133
cache_creation = getattr(anthropic_usage, "cache_creation", None)
2134+
cache_creation_keys = ("ephemeral_1h_input_tokens", "ephemeral_5m_input_tokens")
21342135
if cache_creation:
2135-
for k in ["ephemeral_1h_input_tokens", "ephemeral_5m_input_tokens"]:
2136-
v = getattr(cache_creation, k, None)
2137-
if v:
2138-
input_token_details[k] = v
2136+
if isinstance(cache_creation, BaseModel):
2137+
cache_creation = cache_creation.model_dump()
2138+
for k in cache_creation_keys:
2139+
input_token_details[k] = cache_creation.get(k)
21392140

21402141
# Anthropic input_tokens exclude cached token counts.
21412142
input_tokens = (
@@ -2149,6 +2150,6 @@ def _create_usage_metadata(anthropic_usage: BaseModel) -> UsageMetadata:
21492150
output_tokens=output_tokens,
21502151
total_tokens=input_tokens + output_tokens,
21512152
input_token_details=InputTokenDetails(
2152-
**{k: v for k, v in input_token_details.items() if v is not None}
2153+
**{k: v for k, v in input_token_details.items() if v is not None},
21532154
),
21542155
)

0 commit comments

Comments
 (0)