Skip to content

Comments

Langchain model token counting bug fix#268

Closed
adriantqw wants to merge 2 commits intoPostHog:masterfrom
adriantqw:patch-1
Closed

Langchain model token counting bug fix#268
adriantqw wants to merge 2 commits intoPostHog:masterfrom
adriantqw:patch-1

Conversation

@adriantqw
Copy link

For models with reasoning tokens, the _parse_usage function in the Langchain CallbackHandler does not successfully parse token usage metadata to PostHog.

For models with reasoning tokens the `_parse_usage` function does not successfully parse Langchain token usage metadata to PostHog.
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.

PR Summary

Fixed token counting for language models with reasoning tokens by enhancing the _parse_usage function in posthog/ai/langchain/callbacks.py to properly handle usage metadata parsing.

  • Added fallback paths to check usage information in both generation_info and message properties of model responses
  • Implemented default empty dictionary fallback if neither usage source exists
  • Critical fix ensures accurate token counting metrics in PostHog analytics, particularly for models using reasoning tokens

1 file reviewed, no comments
Edit PR Review Bot Settings | Greptile

@rafaeelaudibert rafaeelaudibert requested a review from a team February 19, 2026 03:22
@rafaeelaudibert
Copy link
Member

We've updated our release process. We require sampo now. Please rebase on master and check README to understand what should be done.

@Radu-Raicea
Copy link
Member

I don't think this is still relevant, please re-open if it is.

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.

3 participants