Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Sep 2, 2025

Important

Adds cost tracking for non-streamed OpenRouter responses in synchronous and asynchronous wrappers in langfuse/openai.py.

  • Behavior:
    • Adds cost tracking for non-streamed OpenRouter responses in _wrap() and _wrap_async() functions in langfuse/openai.py.
    • Uses _parse_cost() to extract cost details from openai_response.usage if available.
  • Functions:
    • Updates generation.update() in _wrap() and _wrap_async() to include cost_details.
    • _parse_cost() checks for cost attribute in usage and returns a dictionary with total cost if present.

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

Disclaimer: Experimental PR review

Greptile Summary

Updated On: 2025-09-02 14:15:53 UTC

This PR fixes cost tracking for non-streamed OpenRouter API responses in the Langfuse OpenAI integration. The changes add explicit cost extraction logic to both synchronous and asynchronous wrapper functions that handle non-streamed OpenAI API responses.

The core issue was that OpenRouter (an OpenAI-compatible API provider) returns cost information in a different format than standard OpenAI responses - specifically in a cost field within the usage object. While Langfuse already had cost parsing logic for streamed responses via the _create_langfuse_update function, non-streamed responses were missing this cost tracking capability for OpenRouter.

The implementation leverages the existing _parse_cost() function, which already contains OpenRouter-specific logic to extract cost data from the usage object's cost attribute and format it as {"total": cost_value}. The fix adds conditional cost parsing in two locations:

  1. Line 794-796 in the sync wrapper (_wrap function)
  2. Line 861-863 in the async wrapper (_wrap_async function)

Both additions use the same pattern: cost_details=_parse_cost(openai_response.usage) if hasattr(openai_response, "usage") else None. This ensures cost tracking works for OpenRouter while maintaining backward compatibility with other OpenAI-compatible providers that may not have usage data available.

The change integrates seamlessly with Langfuse's existing cost tracking infrastructure and follows the established pattern of conditional attribute checking before attempting to extract cost information.

Confidence score: 4/5

  • This PR is safe to merge with low risk of breaking existing functionality
  • Score reflects targeted fix with proper conditional checks and reuse of existing parsing logic
  • Pay close attention to langfuse/openai.py for integration testing with different API providers

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

@hassiebp hassiebp merged commit 0854815 into main Sep 2, 2025
10 checks passed
@hassiebp hassiebp deleted the fix-openrouter-non-streamed-cost branch September 2, 2025 15:02
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.

2 participants