module

Smith::LLM::AnthropicCaching

The parts of Anthropic prompt caching that do not depend on the wire format, shared by the two providers that can reach an Anthropic model: Anthropic directly, OpenRouter when it routes to one.

What is shared is the policy — which message to mark, and what a marker looks like. The placement is not, and cannot be: Anthropic speaks the Messages schema, OpenRouter the OpenAI one, and the same breakpoint lands in a differently shaped object in each. Trying to share that too would produce a builder that knows both schemas and serves neither.

Class methods

usage(json : JSON::Any) : Usage

Usage from an OpenAI-shaped usage object that carries Anthropic cache counters — which is what OpenRouter returns on an Anthropic route.

Two things differ from the direct Anthropic route. The counters are named the OpenAI way and live one level down, under prompt_tokens_details. And prompt_tokens here is the whole prompt, cached part included, where smith's Usage#prompt_tokens is the uncached remainder and the cost model adds the cache counters on top of it. Passing the number through as it arrives would bill the cached prefix twice — hence the subtraction, clamped because a provider that reports an inconsistent triple must not turn into a negative token count.

Source