-
Notifications
You must be signed in to change notification settings - Fork 6.4k
✨ feat(anthropic): add prompt caching model validation utilities #20069
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
base: main
Are you sure you want to change the base?
Conversation
Add validation utility function for Anthropic prompt caching support, following the same pattern as Bedrock Converse integration. Changes: - Add ANTHROPIC_PROMPT_CACHING_SUPPORTED_MODELS constant with 32 model identifiers - Add is_anthropic_prompt_caching_supported_model() function with comprehensive docstring - Add 14 unit tests covering all supported/unsupported models and edge cases - Bump version from 0.9.3 to 0.9.4 - Apply ruff linter fixes Benefits: - Provides standardized model validation for prompt caching - Helps developers reduce costs by up to 90% and latency by up to 85% - Maintains consistency across LlamaIndex integrations - Single source of truth for model support information All tests pass successfully (14/14).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR might be useful, but we should probably add this check for prompt caching support in the main code for the LLM :)
def is_anthropic_prompt_caching_supported_model(model: str) -> bool: | ||
""" | ||
Check if the given Anthropic model supports prompt caching. | ||
Prompt caching allows you to cache frequently used context (like system prompts, | ||
long documents, or tool definitions) to reduce costs by up to 90% and latency | ||
by up to 85% for supported models. | ||
For more information, see: | ||
https://docs.claude.com/en/docs/build-with-claude/prompt-caching | ||
Args: | ||
model: The Anthropic model identifier (e.g., 'claude-sonnet-4-20250514') | ||
Returns: | ||
bool: True if the model supports prompt caching, False otherwise | ||
Examples: | ||
>>> is_anthropic_prompt_caching_supported_model('claude-sonnet-4-20250514') | ||
True | ||
>>> is_anthropic_prompt_caching_supported_model('claude-2.1') | ||
False | ||
>>> is_anthropic_prompt_caching_supported_model('claude-3-5-haiku-20241022') | ||
True | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As much as I appreciate detailed docstrings, I think we can make this a little less detailed :))
Add Anthropic Prompt Caching Model Validation Function
Description
This PR adds a utility function
is_anthropic_prompt_caching_supported_model()
to the Anthropic LLM integration, following the same pattern as the existingis_bedrock_prompt_caching_supported_model()
function in the Bedrock Converse integration.Motivation:
Implementation:
ANTHROPIC_PROMPT_CACHING_SUPPORTED_MODELS
tuple constant containing all supported modelsis_anthropic_prompt_caching_supported_model()
function with comprehensive docstringllama-index-llms-bedrock-converse
Supported Models:
Reference: https://docs.claude.com/en/docs/build-with-claude/prompt-caching
Fixes # (N/A - Enhancement)
New Package?
Version Bump?
Type of Change
How Has This Been Tested?
This change is covered by comprehensive unit testing.
Test Details:
tests/test_anthropic_utils.py
with 14 test casespytest tests/test_anthropic_utils.py -v
✅Test Output:
Suggested Checklist:
uv run make format; uv run make lint
to appease the lint gods (will run before final submission)Files Changed
llama_index/llms/anthropic/utils.py
ANTHROPIC_PROMPT_CACHING_SUPPORTED_MODELS
constant (32 model identifiers)is_anthropic_prompt_caching_supported_model()
functiontests/test_anthropic_utils.py
(NEW)pyproject.toml
Example Usage
Consistency with Existing Code
This implementation follows the exact pattern used in
llama-index-llms-bedrock-converse
:is_{provider}_prompt_caching_supported_model()
{PROVIDER}_PROMPT_CACHING_SUPPORTED_MODELS
Additional Notes
We're happy to make any adjustments based on maintainer feedback. Thank you for considering this contribution to improve the LlamaIndex ecosystem!