Skip to content

Anthropic provider reports stale cache-write cost when message_delta carries updated cache creation #997

Description

@Adolanium

In packages/ai/src/providers/anthropic.ts, the cache-write per-token rate is computed once from the cache_creation TTL breakdown in message_start (~550-556) and captured in a closure. The message_delta handler (~682-711) re-reads the latest token counts but keeps passing the stale rate into calculateCost.

When message_delta carries different cache-creation numbers than message_start, reported cost.cacheWrite and cost.total drift from actual billing. This happens behind proxies like LiteLLM that re-aggregate usage, and the 1h vs 5m rate gap under cacheRetention: "long" amplifies the error. The SDK's MessageDeltaUsage type does not declare the cache_creation breakdown (only the scalar), so the current code never reads it from message_delta at all, but proxies do include it at runtime.

Fix: reprice inside the message_delta handler from the latest event.usage.cache_creation breakdown when present, and fall back to the message_start rate otherwise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions