Skip to content

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

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.

1 participant