Repository navigation
feat: prompt caching for the Anthropic adapter - #4
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
ChatRequest.cache(on). When set, the Anthropic adapter marks the system prompt and the last tool with acache_controlbreakpoint, 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, soprompt_tokensis now input + cache-read + cache-creation, andUsagegainscache_read_tokens/cache_write_tokensfor pricing. Non-constructing readers (like rook) are unaffected.Tests: 93 passed, including new coverage for the cache markers and the summed token accounting.