subtract_usage#

langchain_core.messages.ai.subtract_usage(left: UsageMetadata | None, right: UsageMetadata | None) UsageMetadata[来源]#

递归地减去两个UsageMetadata对象。

令牌计数不能为负数,因此实际操作为 max(left - right, 0)。

示例

from langchain_core.messages.ai import subtract_usage

left = UsageMetadata(
    input_tokens=5,
    output_tokens=10,
    total_tokens=15,
    input_token_details=InputTokenDetails(cache_read=4)
)
right = UsageMetadata(
    input_tokens=3,
    output_tokens=8,
    total_tokens=11,
    output_token_details=OutputTokenDetails(reasoning=4)
)

subtract_usage(left, right)

结果在

UsageMetadata(
    input_tokens=2,
    output_tokens=2,
    total_tokens=4,
    input_token_details=InputTokenDetails(cache_read=4),
    output_token_details=OutputTokenDetails(reasoning=0)
)
Parameters:
Return type:

UsageMetadata