Skip to content

feat(llm): prompt caching via cacheable system prefix - #56

Open
abhichandra21 wants to merge 1 commit into
masterfrom
feature/prompt-caching
Open

feat(llm): prompt caching via cacheable system prefix#56
abhichandra21 wants to merge 1 commit into
masterfrom
feature/prompt-caching

Conversation

@abhichandra21

Copy link
Copy Markdown
Owner

Summary

  • Adds an optional system parameter to LLMClient.generate() across the ABC and all three provider implementations (Anthropic, Gemini, OpenAI).
  • Stable, high-reuse content (taste profile, ranking instructions) is passed as system so providers can serve it from a prompt cache: Anthropic uses cache_control: ephemeral, Gemini maps to system_instruction, OpenAI to a leading system message.
  • UsageStats gains cache_read_tokens / cache_write_tokens fields with correct Anthropic pricing (write 1.25×, read 0.10× base input rate).
  • Applied at the two highest-reuse call sites: wizard.py splits per-turn prompt into a cached _system(profile) + volatile _user(state); query_engine.rank_candidates moves instructions + taste profile into system so they cache across queries in a session.

Context

Found mid-stream on local-only branch feature/prompt-caching (1 commit 134de38, branched from master HEAD a59a772). No prior PR existed. Pushed and opened as part of stale-branch reconciliation.

Test plan

  • Run python3 -m pytest tests/test_query_engine.py tests/test_wizard.py -v — both test files updated in this commit
  • Run ./recommend "spy thriller" and confirm cache_read_tokens shows up in debug stats after the second query in a session
  • Confirm Gemini and OpenAI paths still work (no cache_control field leaked to those providers)

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.
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