feat(core): zero-out token costs for cache hits (#32437)

This commit is contained in:
ccurme
2025-08-07 09:49:34 -03:00
committed by GitHub
parent bc4251b9e0
commit 6e108c1cb4
2 changed files with 30 additions and 0 deletions

View File

@@ -666,6 +666,16 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
converted_generations.append(chat_gen)
else:
# Already a ChatGeneration or other expected type
if hasattr(gen, "message") and isinstance(gen.message, AIMessage):
# We zero out cost on cache hits
gen.message = gen.message.model_copy(
update={
"usage_metadata": {
**(gen.message.usage_metadata or {}),
"total_cost": 0,
}
}
)
converted_generations.append(gen)
return converted_generations