diff --git a/docs/my-website/docs/providers/anthropic.md b/docs/my-website/docs/providers/anthropic.md index bcfb698a0f8..cae8657f1a0 100644 --- a/docs/my-website/docs/providers/anthropic.md +++ b/docs/my-website/docs/providers/anthropic.md @@ -444,7 +444,7 @@ Here's what a sample Raw Request from LiteLLM for Anthropic Context Caching look POST Request Sent from LiteLLM: curl -X POST \ https://api.anthropic.com/v1/messages \ --H 'accept: application/json' -H 'anthropic-version: 2023-06-01' -H 'content-type: application/json' -H 'x-api-key: sk-...' -H 'anthropic-beta: prompt-caching-2024-07-31' \ +-H 'accept: application/json' -H 'anthropic-version: 2023-06-01' -H 'content-type: application/json' -H 'x-api-key: sk-...' \ -d '{'model': 'claude-3-5-sonnet-20240620', [ { "role": "user", @@ -472,6 +472,8 @@ https://api.anthropic.com/v1/messages \ "max_tokens": 10 }' ``` + +**Note:** Anthropic no longer requires the `anthropic-beta: prompt-caching-2024-07-31` header. Prompt caching now works automatically when you use `cache_control` in your messages. ::: ### Caching - Large Context Caching diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 51030b5a11c..c71edcdc2d1 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -54,7 +54,10 @@ from litellm.types.utils import ( CompletionTokensDetailsWrapper, ) from litellm.types.utils import Message as LitellmMessage -from litellm.types.utils import PromptTokensDetailsWrapper, ServerToolUse +from litellm.types.utils import ( + PromptTokensDetailsWrapper, + ServerToolUse, +) from litellm.utils import ( ModelResponse, Usage, @@ -204,9 +207,11 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) # Relevant issue: https://github.com/BerriAI/litellm/issues/7755 def get_cache_control_headers(self) -> dict: + # Anthropic no longer requires the prompt-caching beta header + # Prompt caching now works automatically when cache_control is used in messages + # Reference: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching return { "anthropic-version": "2023-06-01", - "anthropic-beta": "prompt-caching-2024-07-31", } def _map_tool_choice( diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 098694f15ae..fcbe9823ed4 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -12,7 +12,11 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import ( ) from litellm.llms.base_llm.base_utils import BaseLLMModelInfo, BaseTokenCounter from litellm.llms.base_llm.chat.transformation import BaseLLMException -from litellm.types.llms.anthropic import AllAnthropicToolsValues, AnthropicMcpServerTool, ANTHROPIC_HOSTED_TOOLS +from litellm.types.llms.anthropic import ( + ANTHROPIC_HOSTED_TOOLS, + AllAnthropicToolsValues, + AnthropicMcpServerTool, +) from litellm.types.llms.openai import AllMessageValues from litellm.types.utils import TokenCountResponse @@ -273,8 +277,9 @@ class AnthropicModelInfo(BaseLLMModelInfo): beta_header = self.get_computer_tool_beta_header(computer_tool_used) betas.append(beta_header) - if prompt_caching_set: - betas.append("prompt-caching-2024-07-31") + # Anthropic no longer requires the prompt-caching beta header + # Prompt caching now works automatically when cache_control is used in messages + # Reference: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching if file_id_used: betas.append("files-api-2025-04-14") @@ -305,8 +310,9 @@ class AnthropicModelInfo(BaseLLMModelInfo): container_with_skills_used: bool = False, ) -> dict: betas = set() - if prompt_caching_set: - betas.add("prompt-caching-2024-07-31") + # Anthropic no longer requires the prompt-caching beta header + # Prompt caching now works automatically when cache_control is used in messages + # Reference: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching if computer_tool_used: beta_header = self.get_computer_tool_beta_header(computer_tool_used) betas.add(beta_header) diff --git a/tests/local_testing/test_anthropic_prompt_caching.py b/tests/local_testing/test_anthropic_prompt_caching.py index 0926bd17b70..c8589dd8844 100644 --- a/tests/local_testing/test_anthropic_prompt_caching.py +++ b/tests/local_testing/test_anthropic_prompt_caching.py @@ -104,7 +104,6 @@ async def test_litellm_anthropic_prompt_caching_tools(): ], extra_headers={ "anthropic-version": "2023-06-01", - "anthropic-beta": "prompt-caching-2024-07-31", }, ) @@ -112,11 +111,12 @@ async def test_litellm_anthropic_prompt_caching_tools(): print("call args=", mock_post.call_args) expected_url = "https://api.anthropic.com/v1/messages" + # Note: anthropic-beta header for prompt-caching is no longer required + # Anthropic now supports prompt caching automatically when cache_control is used expected_headers = { "accept": "application/json", "content-type": "application/json", "anthropic-version": "2023-06-01", - "anthropic-beta": "prompt-caching-2024-07-31", "x-api-key": "mock_api_key", } @@ -285,7 +285,6 @@ async def test_anthropic_api_prompt_caching_basic(): max_tokens=10, extra_headers={ "anthropic-version": "2023-06-01", - "anthropic-beta": "prompt-caching-2024-07-31", }, ) @@ -356,7 +355,6 @@ async def test_anthropic_api_prompt_caching_basic_with_cache_creation(): max_tokens=10, extra_headers={ "anthropic-version": "2023-06-01", - "anthropic-beta": "prompt-caching-2024-07-31", }, ) @@ -645,7 +643,6 @@ async def test_litellm_anthropic_prompt_caching_system(): ], extra_headers={ "anthropic-version": "2023-06-01", - "anthropic-beta": "prompt-caching-2024-07-31", }, ) @@ -657,7 +654,6 @@ async def test_litellm_anthropic_prompt_caching_system(): "accept": "application/json", "content-type": "application/json", "anthropic-version": "2023-06-01", - "anthropic-beta": "prompt-caching-2024-07-31", "x-api-key": "mock_api_key", } diff --git a/tests/old_proxy_tests/tests/test_anthropic_context_caching.py b/tests/old_proxy_tests/tests/test_anthropic_context_caching.py index 7a153295f35..6b37873df4e 100644 --- a/tests/old_proxy_tests/tests/test_anthropic_context_caching.py +++ b/tests/old_proxy_tests/tests/test_anthropic_context_caching.py @@ -30,7 +30,6 @@ response = client.chat.completions.create( ], extra_headers={ "anthropic-version": "2023-06-01", - "anthropic-beta": "prompt-caching-2024-07-31", }, )