feat(llm): prompt caching via cacheable system prefix - #56
Open
abhichandra21 wants to merge 1 commit into
Open
Conversation
Add an optional system parameter to LLMClient.generate() across the ABC and all three providers. Stable, repeated content (notably the taste profile) goes in system so providers can serve it from a prompt cache: AnthropicClient marks it with cache_control ephemeral, Gemini maps it to system_instruction, and OpenAI to a leading system message. UsageStats tracks cache read/write tokens with correct pricing (write 1.25x, read 0.10x of input). Cache cost is recorded for Anthropic only, since Gemini/OpenAI report cached tokens as a subset of input_tokens. Apply at the high-reuse call sites: wizard splits its per-turn prompt into a cached _system(profile) plus a volatile _user(state), and rank_candidates moves instructions + taste profile into system so they cache across queries in a session.
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.
Summary
systemparameter toLLMClient.generate()across the ABC and all three provider implementations (Anthropic, Gemini, OpenAI).systemso providers can serve it from a prompt cache: Anthropic usescache_control: ephemeral, Gemini maps tosystem_instruction, OpenAI to a leading system message.UsageStatsgainscache_read_tokens/cache_write_tokensfields with correct Anthropic pricing (write 1.25×, read 0.10× base input rate).wizard.pysplits per-turn prompt into a cached_system(profile)+ volatile_user(state);query_engine.rank_candidatesmoves instructions + taste profile intosystemso they cache across queries in a session.Context
Found mid-stream on local-only branch
feature/prompt-caching(1 commit134de38, branched from master HEADa59a772). No prior PR existed. Pushed and opened as part of stale-branch reconciliation.Test plan
python3 -m pytest tests/test_query_engine.py tests/test_wizard.py -v— both test files updated in this commit./recommend "spy thriller"and confirmcache_read_tokensshows up in debug stats after the second query in a session