Skip to content

fix(langchain): avoid double counting Anthropic cache writes - #2070

Open
chelsealong wants to merge 1 commit into
langfuse:mainfrom
chelsealong:fix/codex-langfuse-python-19452
Open

chelsealong wants to merge 1 commit into
langfuse:mainfrom
chelsealong:fix/codex-langfuse-python-19452

Conversation

@chelsealong

@chelsealong chelsealong commented Oct 28, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes langfuse/langfuse#19452.

Map Anthropic’s 5-minute and 1-hour cache writes to their usage buckets and retain only unattributed writes in cache_creation_input_tokens. This prevents the aggregate and tier counts from counting the same tokens twice.

The reported example now totals 5,701 tokens instead of 11,245. Regression tests cover that example, an unattributed remainder, missing aggregate, and zero-value tiers.

Prepared with Codex assistance.

Type of change

  • Bug fix

Verification

uv run --frozen pytest -q tests/unit/test_parse_usage_model.py tests/unit/test_langchain.py # 39 passed
uv run --frozen ruff check . # passed
uv run --frozen ruff format --check langfuse/langchain/CallbackHandler.py tests/unit/test_parse_usage_model.py # passed
uv run --frozen mypy langfuse --no-error-summary # passed
git diff --check # passed

Checklist

  • I added or updated tests for behavior 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.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues were found.

Summary

Maps Anthropic’s cache writes to separate 5-minute and 1-hour buckets. Only writes not covered by those buckets remain in cache_creation_input_tokens.

  • LangChain usage records separate Anthropic cache tiers without repeating their tokens.

Reviews (1) · Last reviewed commit: "fix(langchain): avoid double counting An..." · 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.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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

2 participants