-
Notifications
You must be signed in to change notification settings - Fork 15
🤖 fix: implement proper Anthropic cache strategy for system prompts and tools #690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+414
−8
Conversation
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
…nd tools
- Add cache control for system messages by converting them to cached messages
- Implement tool definition caching for Anthropic models
- Improve message caching to use proper providerOptions structure
- Add comprehensive unit tests for cache strategy functions
- Add integration tests to verify end-to-end functionality
This significantly reduces token usage and costs when using Anthropic models by:
- Caching system prompts that rarely change
- Caching static tool definitions
- Properly caching conversation history up to the current message
Follows Vercel AI SDK documentation for Anthropic cache control using
providerOptions with { anthropic: { cacheControl: { type: 'ephemeral' } } }
…thropic When converting system message to cached message for Anthropic models, the system parameter must be undefined (not empty string) to avoid API error: 'system: text content blocks must be non-empty' This ensures the AI SDK doesn't send an empty system block to Anthropic.
- Add 8 new tests in streamManager.test.ts covering: - System message caching - Tool definition caching - Message caching at correct positions - Non-Anthropic model behavior - System parameter handling (undefined vs string) - Cache control structure validation - Add integration test for empty system message handling - All tests pass without requiring API keys - Tests verify correct providerOptions structure sent to AI SDK
- Close previous test block before adding new describe block - Remove duplicate closing braces - All tests now pass successfully
- Replace async functions without await with Promise.resolve() - Fix type assertions to use recommended pattern (as unknown as T) - Remove streamManager cache tests (duplicated in cacheStrategy.test.ts) - Run prettier formatting on all modified files - All static checks now pass
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
This PR fixes the Anthropic cache strategy to properly cache system prompts, tool definitions, and conversation messages according to the Vercel AI SDK documentation.
Problem
The current implementation only cached conversation messages but missed two critical components:
Solution
1. Enhanced Cache Strategy Module
createCachedSystemMessage()to convert system prompts to cached messagesapplyCacheControlToTools()to cache tool definitionssupportsAnthropicCache()helper for cleaner code2. StreamManager Integration
streamText()3. Comprehensive Testing
Impact
This change significantly reduces token usage and costs when using Anthropic models by:
Implementation Details
Uses the proper
providerOptionsstructure as documented:Testing
bun test src/common/utils/ai/cacheStrategy.test.tsmake typecheckmake buildGenerated with
mux