Skip to content

fix(langchain): avoid double counting Anthropic prompt cache creation tokens - #2088

Open
viliamgonda wants to merge 1 commit into
langfuse:mainfrom
viliamgonda:fix/langchain-anthropic-cache-write-tokens
Open

viliamgonda wants to merge 1 commit into
langfuse:mainfrom
viliamgonda:fix/langchain-anthropic-cache-write-tokens

Conversation

@viliamgonda

@viliamgonda viliamgonda commented Nov 1, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes langfuse/langfuse#19452

In CallbackHandler._parse_usage_model, Anthropic prompt caching reports cache-write breakdown under cache_creation (ephemeral_5m_input_tokens, ephemeral_1h_input_tokens) alongside the aggregate cache_creation_input_tokens.

Previously, _parse_usage_model flattened the nested dict into cache_creation_ephemeral_* and also set/kept cache_creation_input_tokens. Because both the aggregate count and TTL tiers describe the same tokens, downstream bucket summation counted prompt cache creation twice.

This change:

  1. Maps Anthropic's TTL breakdown tiers to canonical Langfuse keys (input_cache_creation_5m, input_cache_creation_1h), matching server model pricing.
  2. Deducts attributed TTL tokens from aggregate counters (cache_creation_input_tokens, input_cache_creation), retaining only unattributed remainder tokens.
  3. Adds regression tests verifying proper de-duplication and remainder handling.

Type of change

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

Verification

List the main commands you ran:

uv run --frozen pytest tests/unit/test_parse_usage_model.py
LANGFUSE_PUBLIC_KEY=pk-lf-test LANGFUSE_SECRET_KEY=sk-lf-test uv run --frozen pytest -n auto --dist worksteal tests/unit
uv run --frozen ruff check .
uv run --frozen ruff format --check .
uv run --frozen 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.
  • 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.

Summary

Fixes double counting of Anthropic cache creation tokens in _parse_usage_model.

  • LangChain usage parsing records Anthropic cache tiers without repeating their counts.

No actionable issues were found. Tests were reviewed but not run.

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

CLAassistant commented Nov 1, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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