diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py index ff931efb7c447..2fa330754e677 100644 --- a/libs/partners/anthropic/langchain_anthropic/chat_models.py +++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py @@ -757,9 +757,18 @@ def _format_output(self, data: Any, **kwargs: Any) -> ChatResult: "output_tokens": data.usage.output_tokens, "total_tokens": data.usage.input_tokens + data.usage.output_tokens, } + if hasattr(data.usage, "cache_creation_input_tokens"): + msg.usage_metadata["cache_creation_input_tokens"] = ( + data.usage.cache_creation_input_tokens + ) + msg.usage_metadata["total_tokens"] += data.usage.cache_creation_input_tokens + if hasattr(data.usage, "cache_read_input_tokens"): + msg.usage_metadata["cache_read_input_tokens"] = ( + data.usage.cache_read_input_tokens + ) + msg.usage_metadata["total_tokens"] += data.usage.cache_read_input_tokens return ChatResult( - generations=[ChatGeneration(message=msg)], - llm_output=llm_output, + generations=[ChatGeneration(message=msg)], llm_output=llm_output ) def _generate(