fix(llm-client): enable Anthropic prompt caching by default - #233
Conversation
WalkthroughThe change adds request-level caching control to ChangesPrompt caching
Estimated code review effort: 4 (Complex) | ~45 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
crates/libsy-llm-client/src/client.rs (1)
936-952: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winBroaden test coverage for the new caching branches.
The new test only covers a single plain-string message content block being marked and then unmarked. It does not exercise: preserving a caller-supplied
cache_controlwhen caching is already enabled (thehas_anthropic_cache_controlearly return), thesystem-prompt fallback, the last-tool fallback, or removal from atoolsarray withinput_schema. Given this logic gates a cost-sensitive feature enabled by default, add cases for these branches.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/libsy-llm-client/src/client.rs` around lines 936 - 952, Expand the test `anthropic_prompt_caching_is_injected_and_can_be_disabled` with cases covering preservation of caller-supplied `cache_control` via the `has_anthropic_cache_control` early return, system-prompt fallback, last-tool fallback, and removal of `cache_control` from tools containing `input_schema`. Assert each branch’s expected JSON behavior while retaining the existing plain-string injection and disablement coverage.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/libsy-llm-client/src/client.rs`:
- Around line 718-779: Add concise comments to the private helpers
mark_last_text_cacheable, has_anthropic_cache_control, remove_cache_controls,
and is_anthropic_cacheable explaining their traversal purpose, the meaning of
Anthropic-cacheable content, the reverse traversal used to select the last text
block, and why input_schema is excluded from recursion. Keep the implementation
unchanged and align the wording with the nearby apply_anthropic_prompt_caching
comment.
- Around line 685-740: Update apply_anthropic_prompt_caching and
mark_last_text_cacheable to identify and mark the last block accepted by
is_anthropic_cacheable, including trailing image, document, tool_use, and
tool_result blocks rather than only text blocks. Add tests covering trailing
tool_use and tool_result blocks, and ensure the implementation does not create
redundant manual cache breakpoints when supported automatic top-level
cache_control is used.
---
Nitpick comments:
In `@crates/libsy-llm-client/src/client.rs`:
- Around line 936-952: Expand the test
`anthropic_prompt_caching_is_injected_and_can_be_disabled` with cases covering
preservation of caller-supplied `cache_control` via the
`has_anthropic_cache_control` early return, system-prompt fallback, last-tool
fallback, and removal of `cache_control` from tools containing `input_schema`.
Assert each branch’s expected JSON behavior while retaining the existing
plain-string injection and disablement coverage.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 58c8b986-f59b-421b-918f-54e783240d64
📒 Files selected for processing (2)
crates/libsy-llm-client/src/client.rscrates/protocol/src/llm.rs
d7636bd to
04ad90e
Compare
04ad90e to
6ddd961
Compare
Signed-off-by: nachiketb <nachiketb@nvidia.com>
6ddd961 to
ad08053
Compare
What
Enable Anthropic prompt caching by default in
TranslatingLlmClient.Why
Native Anthropic requests require an explicit cache breakpoint to use provider prompt caching.
How
For
Backend::Anthropic, the client marks the final message content block with{"cache_control": {"type": "ephemeral"}}after request translation. Other backend formats are unchanged.What to review
Validation
cargo test -p switchyard-llm-client anthropic_prompt_caching_marks_final_messagecargo clippy -p switchyard-llm-client --all-targets -- -D warnings