File tree Expand file tree Collapse file tree 1 file changed +6
-5
lines changed
libs/partners/anthropic/langchain_anthropic Expand file tree Collapse file tree 1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change @@ -2131,11 +2131,12 @@ def _create_usage_metadata(anthropic_usage: BaseModel) -> UsageMetadata:
2131
2131
}
2132
2132
# Add (beta) cache TTL information if available
2133
2133
cache_creation = getattr (anthropic_usage , "cache_creation" , None )
2134
+ cache_creation_keys = ("ephemeral_1h_input_tokens" , "ephemeral_5m_input_tokens" )
2134
2135
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 )
2139
2140
2140
2141
# Anthropic input_tokens exclude cached token counts.
2141
2142
input_tokens = (
@@ -2149,6 +2150,6 @@ def _create_usage_metadata(anthropic_usage: BaseModel) -> UsageMetadata:
2149
2150
output_tokens = output_tokens ,
2150
2151
total_tokens = input_tokens + output_tokens ,
2151
2152
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 },
2153
2154
),
2154
2155
)
You can’t perform that action at this time.
0 commit comments