fix(openai): openrouter non-streamed cost tracking #1319
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Important
Adds cost tracking for non-streamed OpenRouter responses in synchronous and asynchronous wrappers in
langfuse/openai.py._wrap()and_wrap_async()functions inlangfuse/openai.py._parse_cost()to extract cost details fromopenai_response.usageif available.generation.update()in_wrap()and_wrap_async()to includecost_details._parse_cost()checks forcostattribute inusageand returns a dictionary with total cost if present.This description was created by
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
costfield within the usage object. While Langfuse already had cost parsing logic for streamed responses via the_create_langfuse_updatefunction, 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'scostattribute and format it as{"total": cost_value}. The fix adds conditional cost parsing in two locations:_wrapfunction)_wrap_asyncfunction)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
langfuse/openai.pyfor integration testing with different API providers