Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Feb 24, 2025

Important

Fixes token usage calculation in langchain.py and adds a test for cached token usage handling.

  • Behavior:
    • Fixes token usage calculation in _parse_usage_model() in langchain.py by adjusting input and output values when input_token_details and output_token_details are present.
    • Adds a test test_cached_token_usage() in test_langchain.py to verify correct handling of cached token usage.
  • Misc:
    • No changes to constants or logging.
    • No renames or typo fixes.

This description was created by Ellipsis 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.

  • Modified langfuse/callback/langchain.py to subtract input/output token details from totals in _parse_usage_model function to prevent double-counting
  • Added comprehensive test case in tests/test_langchain.py to verify token usage accuracy with caching enabled
  • Ensures proper tracking of input_cache_read values and accurate cost calculations when using cached tokens

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile

hassiebp and others added 3 commits February 24, 2025 14:40
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
@hassiebp hassiebp enabled auto-merge (squash) February 24, 2025 13:45
@hassiebp hassiebp disabled auto-merge February 25, 2025 11:17
@hassiebp hassiebp merged commit 0ef2378 into main Feb 25, 2025
8 of 10 checks passed
@hassiebp hassiebp deleted the hassieb/lfe-4029-fixtoken-calc-langchain branch February 25, 2025 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Incorrect Token Calculation When Using Cached Tokens

2 participants