Repository navigation
fix(langchain): keep input and output counts for OpenAI flex tier usage - #2174
Open
pcbeingused333 wants to merge 1 commit into
Open
pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
AI review gate — AI review passedClean review from: Cursor Bugbot. Configuration: the repository's History (latest 5 heads, newest first)
|
This branch has not been deployed
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?
With
ChatOpenAI(service_tier="flex"), the LangChain callback handler recordsinput=0andoutput=0.langchain-openaireports the tier asflex,flex_cache_readandflex_reasoningentries in the token details, which together cover the whole prompt and completion._parse_usage_modelsubtracts them as if they were exclusive sub-categories, so the totals are zeroed out.The
prioritytier, which has the same shape, is already exempt from that subtraction. This applies the same exemption toflexandflex_*. The detail keys are still recorded asinput_flex,output_flex, etc.Related to langfuse/langfuse#11896 (the same problem for the priority tier).
Type of change
Verification
All 10 parser unit tests passed locally. With the same test file on
main, the new flex regression test failed (inputwas 0 instead of 100) and the other 9 tests passed. Ruff and mypy passed. The GitHub Actions unit suite has no result on this PR yet.Checklist
code_review.md..env.templateif needed. (None needed: no documented behavior or configuration changes.)AI assistance (Claude Code and Codex) was used for preparation and verification.
Note
Low Risk
Narrow change to LangChain usage normalization with a matching unit test; no auth, security, or data-path changes beyond token accounting.
Overview
Fixes incorrect zero token counts when tracing LangChain runs with OpenAI
service_tier="flex".langchain-openaireports flex usage viaflexandflex_*keys ininput_token_details/output_token_details;_parse_usage_modelwas subtracting those from aggregateinput/outputas if they were exclusive breakdowns (same bug as the priority tier).The handler now skips subtraction for
flexandflex_*alongside the existingpriority/priority_*exemption, while still recording prefixed fields likeinput_flexandoutput_flex_reasoning. Comments refer to these as service-tier keys rather than priority-only.A unit test
test_flex_tier_not_subtractedlocks in the behavior for flex-shaped usage payloads.Reviewed by Cursor Bugbot for commit ec45b5b. Bugbot is set up for automated code reviews on this repo. Configure here.


Confidence Score: 5/5
The PR appears safe to merge.Summary
Keeps input and output counts for OpenAI flex-tier usage in the LangChain callback.
Reviews (4) · Last reviewed commit: "fix(langchain): keep input and output co..." · Reviewed by Greptile