Skip to content

feat: prompt caching for the Anthropic adapter - #4

Merged
martian56 merged 1 commit into
mainfrom
prompt-caching
Jul 17, 2026
Merged

martian56 merged 1 commit into
mainfrom
prompt-caching

Conversation

@martian56

Copy link
Copy Markdown
Owner

Adds ChatRequest.cache(on). When set, the Anthropic adapter marks the system prompt and the last tool with a cache_control breakpoint, so repeating the same system-and-tools prefix (a tool-using turn's rounds, or successive turns) reuses it instead of re-billing the whole prefix. Other adapters ignore the flag; OpenAI-compatible endpoints cache long prefixes automatically.

Token accounting: Anthropic reports cached input outside input_tokens, so prompt_tokens is now input + cache-read + cache-creation, and Usage gains cache_read_tokens/cache_write_tokens for pricing. Non-constructing readers (like rook) are unaffected.

Tests: 93 passed, including new coverage for the cache markers and the summed token accounting.

Adds ChatRequest.cache(on): when set, the Anthropic adapter marks the system
prompt and the last tool with a cache_control breakpoint, so a caller that
repeats the same system-and-tools prefix (the rounds of a tool-using turn, or
successive turns) reuses it instead of re-sending and re-billing the whole
prefix. Other adapters ignore the flag.

Token accounting follows: Anthropic reports cached input separately from
input_tokens, so prompt_tokens is now the sum of input, cache-read, and
cache-creation tokens, and Usage gains cache_read_tokens / cache_write_tokens
so a caller can price the cheaper cache reads. OpenAI (cached_tokens) and
Gemini (cachedContentTokenCount) fill cache_read_tokens too; others report 0.
@martian56 martian56 self-assigned this Jul 17, 2026
@martian56
martian56 merged commit 37dd184 into main Jul 17, 2026
3 checks passed
@martian56
martian56 deleted the prompt-caching branch July 17, 2026 09:21
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.

1 participant