Skip to content

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

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

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

Conversation

@gouveags

Copy link
Copy Markdown

Note

This PR depends on PostHog/posthog#71323 and will remain a draft until the server-side event contract is merged.

💡 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: 1629 passed, 59 skipped.
  • Focused AI/token suite: 135 passed, 9 skipped.
  • Ruff format and lint.
  • Mypy across 172 source files.
  • Public API snapshot and import-warning checks.
  • Package build and Twine validation.

📝 Checklist

If releasing new changes

  • Added a Sampo patch 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
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

1 participant