mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-04 12:39:32 +00:00
anthropic[patch]: cache tokens nit (#31484)
if you pass in beta headers directly cache_creation is a dict
This commit is contained in:
@@ -2131,11 +2131,12 @@ def _create_usage_metadata(anthropic_usage: BaseModel) -> UsageMetadata:
|
|||||||
}
|
}
|
||||||
# Add (beta) cache TTL information if available
|
# Add (beta) cache TTL information if available
|
||||||
cache_creation = getattr(anthropic_usage, "cache_creation", None)
|
cache_creation = getattr(anthropic_usage, "cache_creation", None)
|
||||||
|
cache_creation_keys = ("ephemeral_1h_input_tokens", "ephemeral_5m_input_tokens")
|
||||||
if cache_creation:
|
if cache_creation:
|
||||||
for k in ["ephemeral_1h_input_tokens", "ephemeral_5m_input_tokens"]:
|
if isinstance(cache_creation, BaseModel):
|
||||||
v = getattr(cache_creation, k, None)
|
cache_creation = cache_creation.model_dump()
|
||||||
if v:
|
for k in cache_creation_keys:
|
||||||
input_token_details[k] = v
|
input_token_details[k] = cache_creation.get(k)
|
||||||
|
|
||||||
# Anthropic input_tokens exclude cached token counts.
|
# Anthropic input_tokens exclude cached token counts.
|
||||||
input_tokens = (
|
input_tokens = (
|
||||||
@@ -2149,6 +2150,6 @@ def _create_usage_metadata(anthropic_usage: BaseModel) -> UsageMetadata:
|
|||||||
output_tokens=output_tokens,
|
output_tokens=output_tokens,
|
||||||
total_tokens=input_tokens + output_tokens,
|
total_tokens=input_tokens + output_tokens,
|
||||||
input_token_details=InputTokenDetails(
|
input_token_details=InputTokenDetails(
|
||||||
**{k: v for k, v in input_token_details.items() if v is not None}
|
**{k: v for k, v in input_token_details.items() if v is not None},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user