Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

test amnthropic prompt caching

Ishaan Jaff69a640e9c404
diff --git a/docs/my-website/docs/providers/anthropic.md b/docs/my-website/docs/providers/anthropic.md
index 2227b7a6b51..503140158c7 100644
--- a/docs/my-website/docs/providers/anthropic.md
+++ b/docs/my-website/docs/providers/anthropic.md
@@ -208,6 +208,52 @@ print(response)
</TabItem>
</Tabs>
+## **Prompt Caching**
+
+Use Anthropic Prompt Caching
+
+
+[Relevant Anthropic API Docs](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching)
+
+<Tabs>
+<TabItem value="sdk" label="LiteLLM SDK">
+
+```python
+from litellm import completion
+
+resp = litellm.completion(
+ model="vertex_ai_beta/gemini-1.0-pro-001",
+ messages=[{"role": "user", "content": "Who won the world cup?"}],
+ tools=tools,
+ )
+
+print(resp)
+```
+</TabItem>
+<TabItem value="proxy" label="PROXY">
+
+```bash
+curl http://localhost:4000/v1/chat/completions \
+ -H "Content-Type: application/json" \
+ -H "Authorization: Bearer sk-1234" \
+ -d '{
+ "model": "gemini-pro",
+ "messages": [
+ {"role": "user", "content": "Hello, Claude!"}
+ ],
+ "tools": [
+ {
+ "googleSearchRetrieval": {}
+ }
+ ]
+ }'
+
+```
+
+</TabItem>
+</Tabs>
+
+
## Supported Models
`Model Name` 👉 Human-friendly name.
diff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.py
index 4ea9ee3b0f3..969805fb0a6 100644
--- a/litellm/tests/test_completion.py
+++ b/litellm/tests/test_completion.py
@@ -3449,7 +3449,62 @@ def response_format_tests(response: litellm.ModelResponse):
assert isinstance(response.usage.total_tokens, int) # type: ignore
[email protected]("sync_mode", [True, False])
+async def test_anthropic_api_prompt_caching_2():
+ litellm.set_verbose = True
+ response = await litellm.acompletion(
+ model="anthropic/claude-3-5-sonnet-20240620",
+ messages=[
+ # System Message
+ {
+ "role": "system",
+ "content": [
+ {
+ "type": "text",
+ "text": "Here is the full text of a complex legal agreement"
+ * 400,
+ "cache_control": {"type": "ephemeral"},
+ }
+ ],
+ },
+ # marked for caching with the cache_control parameter, so that this checkpoint can read from the previous cache.
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "text",
+ "text": "What are the key terms and conditions in this agreement?",
+ "cache_control": {"type": "ephemeral"},
+ }
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": "Certainly! the key terms and conditions are the following: the contract is 1 year long for $10/mo",
+ },
+ # The final turn is marked with cache-control, for continuing in followups.
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "text",
+ "text": "What are the key terms and conditions in this agreement?",
+ "cache_control": {"type": "ephemeral"},
+ }
+ ],
+ },
+ ],
+ temperature=0.2,
+ max_tokens=10,
+ extra_headers={
+ "anthropic-version": "2023-06-01",
+ "anthropic-beta": "prompt-caching-2024-07-31",
+ },
+ )
+
+ print("response=", response)
+
+
@pytest.mark.parametrize(
"model",
[