Skip to content

fix(ai): preserve Anthropic cache-write TTL breakdowns - #746

Open
gouveags wants to merge 2 commits into
PostHog:mainfrom
gouveags:fix/anthropic-cache-ttl-breakdown
Open

fix(ai): preserve Anthropic cache-write TTL breakdowns#746
gouveags wants to merge 2 commits into
PostHog:mainfrom
gouveags:fix/anthropic-cache-ttl-breakdown

Conversation

@gouveags

@gouveags gouveags commented Jul 16, 2026

Copy link
Copy Markdown

Note

This PR depends on PostHog/posthog#71323 for server-side pricing. It can merge independently, but TTL-specific cost calculation takes effect only after the server-side change ships.

💡 Motivation and Context

PostHog/posthog#71323 adds separate costing for Anthropic's 5-minute and 1-hour cache writes. The Python SDK already preserves that breakdown in its native Anthropic wrappers, but two integrations drop it before ingestion.

Fixes #745. Related to PostHog/posthog#71306. Depends on PostHog/posthog#71323.

This change:

  • Makes the LangChain callback emit the aggregate cache-write count and a complete 5-minute/1-hour property pair. LangGraph uses the same callback path.
  • Preserves raw usage in Claude Agent stream events and result-message fallback, matching the native Anthropic integration.
  • Treats manually supplied TTL token properties as passthrough token data.
  • Adds regression coverage for native Anthropic sync, async, streaming, and the shared Anthropic Bedrock wrapper path.

The PostHog Bedrock and Vertex clients reuse the native Anthropic message adapters. Aggregate-only events remain unchanged; invalid or empty TTL details fall back to the aggregate, and a valid breakdown replaces rather than adds to it.

Direct boto3 calls are outside this PR because posthog-python has no boto3 Bedrock wrapper. The OTel integration forwards spans through a separate endpoint and is not changed.

💚 How did you test it?

  • Full suite on the PR head: 1826 passed, 67 skipped; one unrelated, order-dependent logging assertion failed in posthog/test/test_consumer.py::TestConsumer::test_message_only_error_logs_include_posthog_prefix, and it passes in isolation.
  • Focused AI/token suite: 143 passed, 9 skipped.
  • Ruff format and lint.
  • Mypy across 186 source files.
  • Public API snapshot and import-warning checks.
  • Package build and Twine validation.

📝 Checklist

If releasing new changes

  • Added a Sampo minor changeset.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

I used Codex to inspect the server contract, audit the Python SDK integration paths, implement the focused changes, and run independent test, code, and writing reviews. No public session link is available.

The first pass focused on the LangChain callback. The broader audit showed that native Anthropic wrappers already preserve raw usage, while Claude Agent needed the same raw-usage path. The final diff reuses those existing mechanisms instead of adding provider pricing or a new integration layer.

@gouveags

Copy link
Copy Markdown
Author

@greptile review

@greptile-apps

greptile-apps Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "fix(ai): preserve Anthropic cache-write ..." | Re-trigger Greptile

@turnipdabeets
turnipdabeets requested a review from a team July 17, 2026 00:10
@gouveags
gouveags marked this pull request as ready for review July 17, 2026 16:04
@gouveags
gouveags requested a review from a team as a code owner July 17, 2026 16:04
@gouveags
gouveags marked this pull request as draft July 20, 2026 20:24

@richardsolomou richardsolomou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good, thank you! Could you also open a targeted posthog-js follow-up for the equivalent gaps there? Vercel already preserves the raw usage, but Anthropic streaming and the LangChain integration appear to drop the TTL breakdown.

@gouveags
gouveags marked this pull request as ready for review July 23, 2026 13:28
@gouveags

gouveags commented Jul 23, 2026

Copy link
Copy Markdown
Author

@richardsolomou I opened the targeted JS follow-up in PostHog/posthog-js#4236. Thanks for pointing me in that direction!

@marandaneto

Copy link
Copy Markdown
Member

Commits must have verified signatures.

@gouveags can you fix your commits and force push?

Comment thread posthog/test/ai/anthropic/test_anthropic.py
Comment thread .sampo/changesets/anthropic-cache-ttl-breakdown.md Outdated
Comment thread posthog/ai/claude_agent_sdk/processor.py Outdated
Comment thread posthog/ai/utils.py Outdated
@gouveags
gouveags force-pushed the fix/anthropic-cache-ttl-breakdown branch from a489857 to 7135dd1 Compare July 27, 2026 14:34
@gouveags

gouveags commented Jul 27, 2026

Copy link
Copy Markdown
Author

@marandaneto Sorry about the unsigned commit. I’ve rebuilt the branch with a verified signature. Could you check it again? Thanks for the heads-up.

Does this guideline apply across all PostHog repositories, or is it specific to this one? (asking because in other repos nobody noticed this).

@gouveags

Copy link
Copy Markdown
Author

@carlos-marchal-ph all fixed, let me know if you notice something else that needs polishing! Thanks for the feedback!

@@ -0,0 +1,5 @@
---
pypi/posthog: minor

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is that a fix or new feat? title is fix, but the changeset is minor

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.

Some Python AI integrations drop Anthropic cache-write TTL details

4 participants