Skip to content

fix(langchain): keep input and output counts for OpenAI flex tier usage - #2174

Open
pcbeingused333 wants to merge 1 commit into
langfuse:mainfrom
pcbeingused333:fix/langchain-flex-tier-usage
Open

pcbeingused333 wants to merge 1 commit into
langfuse:mainfrom
pcbeingused333:fix/langchain-flex-tier-usage

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 28, 2026 •

Copy link
Copy Markdown

What does this PR do?

With ChatOpenAI(service_tier="flex"), the LangChain callback handler records input=0 and output=0. langchain-openai reports the tier as flex, flex_cache_read and flex_reasoning entries in the token details, which together cover the whole prompt and completion. _parse_usage_model subtracts them as if they were exclusive sub-categories, so the totals are zeroed out.

The priority tier, which has the same shape, is already exempt from that subtraction. This applies the same exemption to flex and flex_*. The detail keys are still recorded as input_flex, output_flex, etc.

Related to langfuse/langfuse#11896 (the same problem for the priority tier).

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Refactor
  • Documentation update
  • Tooling, CI, or repo maintenance

Verification

All 10 parser unit tests passed locally. With the same test file on main, the new flex regression test failed (input was 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.

python -m pytest -q tests/unit/test_parse_usage_model.py
ruff check langfuse tests/unit/test_parse_usage_model.py
ruff format --check langfuse tests/unit/test_parse_usage_model.py
mypy langfuse --no-error-summary

Checklist

  • I self-reviewed the diff using code_review.md.
  • I added or updated tests for behavior changes.
  • I updated docs, examples, or .env.template if needed. (None needed: no documented behavior or configuration changes.)
  • I did not hand-edit generated files; if generated files changed, I used the upstream regeneration path.
  • I did not commit secrets or credentials.

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-openai reports flex usage via flex and flex_* keys in input_token_details / output_token_details; _parse_usage_model was subtracting those from aggregate input / output as if they were exclusive breakdowns (same bug as the priority tier).

The handler now skips subtraction for flex and flex_* alongside the existing priority / priority_* exemption, while still recording prefixed fields like input_flex and output_flex_reasoning. Comments refer to these as service-tier keys rather than priority-only.

A unit test test_flex_tier_not_subtracted locks 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.RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

Keeps input and output counts for OpenAI flex-tier usage in the LangChain callback.

  • Flex-tier usage keeps its full input and output counts.

Reviews (4) · Last reviewed commit: "fix(langchain): keep input and output co..." · Reviewed by Greptile

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@langfuse-ai-gate-approver

langfuse-ai-gate-approver Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

AI review gate — AI review passed

Clean review from: Cursor Bugbot.

Configuration: the repository's .github/ai-gate.yml on the base branch (the defaults without one).

History (latest 5 heads, newest first)

  • ec45b5bc74ee gate pending — Waiting for AI review

This branch has not been deployed

No deployments
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