fix(langchain): cached token usage #1121
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Important
Fixes token usage calculation in
langchain.pyand adds a test for cached token usage handling._parse_usage_model()inlangchain.pyby adjustinginputandoutputvalues wheninput_token_detailsandoutput_token_detailsare present.test_cached_token_usage()intest_langchain.pyto verify correct handling of cached token usage.This description was created by
for 331cc43. It will automatically update as commits are pushed.
Greptile Summary
Disclaimer: Experimental PR review
This PR fixes token usage calculations in the Langchain integration by correctly handling cached tokens and preventing double-counting when detailed token breakdowns are present.
langfuse/callback/langchain.pyto subtract input/output token details from totals in_parse_usage_modelfunction to prevent double-countingtests/test_langchain.pyto verify token usage accuracy with caching enabledinput_cache_readvalues and accurate cost calculations when using cached tokens