fix(langfuse): restore usage/cost when post_api_request sends a sanitized response#40560
Merged
kshitijk4poor merged 1 commit intoJun 8, 2026
Conversation
…ized response on_post_llm_call extracted usage via `if response is not None:`, taking the response-object path. But post_api_request delivers `response` as a sanitized dict (no `.usage` attribute) alongside a separate `usage` summary dict, so `getattr(response, "usage")` was always None and token/cost data was dropped for every gateway turn (traces showed usage 0 / cost 0). Gate on a real `.usage` attribute so the existing usage-dict fallback is reached. Real response objects (post_llm_call / legacy) still take the response-object path. Adds regression tests for both paths.
13 tasks
1 task
alt-glitch
pushed a commit
that referenced
this pull request
Jun 14, 2026
…zed-response fix(langfuse): restore usage/cost when post_api_request sends a sanitized response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
The bundled Langfuse plugin stopped recording token usage and cost for gateway
turns: traces show usage 0 / cost 0 even though the provider returned usage.
Root cause:
on_post_llm_callextracts usage withif response is not None:,taking the response-object branch.
post_api_requestpassesresponseas asanitized dict (no
.usageattribute) plus a separateusagesummary dict, sogetattr(response, "usage")is always None and the usage-dict fallback below isnever reached — token/cost data is silently dropped.
Fix: gate the response-object branch on a real
.usageattribute(
getattr(response, "usage", None) is not None). Sanitized dict responses fallthrough to the
usagedict; genuine response objects (post_llm_call / legacy)still take the response-object path.
Related Issue
N/A — no existing issue; happy to file one if preferred.
Type of Change
Changes Made
plugins/observability/langfuse/__init__.py: gate usage extraction on a real.usageattribute so theusage-dict fallback is reached for sanitizedpost_api_requestresponses.tests/plugins/test_langfuse_plugin.py: regression tests for both thesanitized-dict path (usage read from the
usagedict) and the real-objectpath (response object still takes precedence).
How to Test
pytest tests/plugins/test_langfuse_plugin.py -q→ 39 pass (37 existing + 2 new).test_sanitized_dict_response_uses_usage_dictfails onmain(usage dropped) and passes with this change.
Checklist
pytest tests/plugins/test_langfuse_plugin.py -q— 39 pass (37 existing + 2 new); did not run the full suite locallymain, pass here)