fix(ai): preserve Anthropic cache-write TTL breakdowns - #4236
Conversation
|
@greptile review |
|
Reviews (1): Last reviewed commit: "fix(ai): preserve Anthropic cache-write ..." | Re-trigger Greptile |
|
@richardsolomou here is the JS fix you asked for during the review on the Python PR. I'm keeping this one as a draft until PostHog/posthog#71323 lands. Does this cover the JS side you had in mind, or is there anything else you'd like me to include? |
|
@greptile review |
|
Reviews (2): Last reviewed commit: "fix(ai): preserve Bedrock cache TTL brea..." | Re-trigger Greptile |
|
@richardsolomou all fixed! |
|
Done! |
|
@richardsolomou we still have some pending checks |
|
@gouveags "Commits must have verified signatures" 😬😅 |
3a2a789 to
5896d55
Compare
|
i forced pushed signed commits |
|
Thanks for the help, @marandaneto! Things have been hectic on my side, so I haven’t really had time to look at this before. |
Align the two conflicted AI files before updating the branch, so the main sync can be created through GitHub with a verified signature. The final TTL resolution follows in the next signed commit.
Keep current main streaming and callback behavior while restoring the reviewed Anthropic and Bedrock cache TTL extraction, including the normalized generation fallback required by current LangChain result shapes.
|
@richardsolomou I resolved the conflicts with the latest main and reran the focused @posthog/ai checks. The branch is mergeable again, and the new commits are verified. Could you take another look when you have a chance? |
|
@marandaneto maybe you can help |
Problem
@posthog/aican drop Anthropic's 5-minute and 1-hour cache-write breakdown before capturing an event.The Anthropic streaming wrapper replaces the usage captured from
message_startwith the final usage delta, which may contain only output tokens. The LangChain callback also preserves the aggregate cache-write count but misses the TTL breakdown from both direct Anthropic responses and Bedrock Converse responses.Bedrock Converse retains this data under
response_metadata.usage.cacheDetailsfor non-streaming calls andresponse_metadata.metadata.usage.cacheDetailsafter streaming, but the PostHog callback did not read either shape.Without those fields, ingestion cannot apply the TTL-specific pricing added in PostHog/posthog#71323.
Changes
message_startsurvive later deltas that omit them.The Vercel integration already preserves raw usage and is unchanged.
How did you test this code?
The regression tests consume the mocked Anthropic stream through the wrapper and pass direct Anthropic and Bedrock Converse
AIMessageusage shapes through the LangChain callback to the captured$ai_generationevent.pnpm --filter @posthog/ai test:unit— 517 passed, 84 skippedpnpm --filter @posthog/ai lintpnpm --filter @posthog/ai buildpnpm check:public-apipnpm buildpnpm lintpnpm lint:playgroundpnpm test:functionalThe root
pnpm test:unitrun hit two unrelated five-second timeouts inposthog-nodeunder parallel load. Both affected suites passed together in isolation: 31/31 tests.Release info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset fileRelated work
Deployment note
The new properties are optional, and the existing aggregate cache-write count remains available. The
@posthog/airelease should follow PostHog/posthog#71323 so ingestion can price the TTL breakdown from the first event that includes it.🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Gabriel directed the change. I used Codex to inspect the provider payloads, implement the fix, review regression risks, and run validation; no public session link is available. We kept the change inside the existing Anthropic and LangChain integrations: Vercel is unchanged, and pricing remains an ingestion responsibility.