Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Aug 27, 2025

Important

Adds cost tracking for OpenRouter in Langfuse OpenAI integration by updating _create_langfuse_update() and introducing _parse_cost() in langfuse/openai.py.

  • Behavior:
    • Adds cost tracking for OpenRouter in langfuse/openai.py by updating _create_langfuse_update() to include cost_details.
    • Introduces _parse_cost() to extract cost from usage if available.
  • Functions:
    • _create_langfuse_update() now calls _parse_cost() to add cost_details to the update.
    • _parse_cost() checks for cost attribute in usage and returns it if it's a float.

This description was created by Ellipsis for 7ca7ff0. You can customize this summary. It will automatically update as commits are pushed.

Disclaimer: Experimental PR review

Greptile Summary

This PR adds cost tracking support for OpenRouter API usage in Langfuse's OpenAI integration. The change introduces a new _parse_cost() function that extracts cost information from OpenRouter's usage response and integrates it into the existing generation update workflow.

The implementation is straightforward: it checks if the usage object has a cost attribute (which OpenRouter provides as the total USD cost of the API call) and maps it to the cost_details.total field in the generation update. This follows OpenRouter's documented API format where cost is provided directly as a float value, unlike standard OpenAI responses that require model pricing lookups.

The change integrates cleanly with the existing usage parsing infrastructure in langfuse/openai.py. The cost parsing is called alongside the existing _parse_usage() function at line 497, maintaining consistency with the current architecture. This allows users to see actual costs in Langfuse when using OpenRouter models without needing to manually configure custom model definitions for cost tracking.

Confidence score: 5/5

  • This PR is safe to merge with minimal risk
  • Score reflects simple, focused implementation with proper error handling and clear integration point
  • No files require special attention

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.

1 file reviewed, no comments

Edit Code Review Bot Settings | Greptile


return usage_dict


Copy link

Choose a reason for hiding this comment

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

When setting cost_details, consider supporting cases where usage is a dict (as _parse_usage does) rather than relying solely on attribute access.

@hassiebp hassiebp merged commit bd5f72f into main Aug 27, 2025
8 of 10 checks passed
@hassiebp hassiebp deleted the feat-openrouter-cost-tracking branch August 27, 2025 14:52
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