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 passing cache controls through anthropic msg

Ishaan Jaff68e24fbf1461
diff --git a/litellm/tests/test_prompt_factory.py b/litellm/tests/test_prompt_factory.py
index f7a715a2204..2351e2c121c 100644
--- a/litellm/tests/test_prompt_factory.py
+++ b/litellm/tests/test_prompt_factory.py
@@ -260,3 +260,48 @@ def test_anthropic_messages_tool_call():
translated_messages[-1]["content"][0]["tool_use_id"]
== "bc8cb4b6-88c4-4138-8993-3a9d9cd51656"
)
+
+
+def test_anthropic_cache_controls_pt():
+ messages = [
+ # 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"},
+ }
+ ],
+ },
+ ]
+
+ translated_messages = anthropic_messages_pt(
+ messages, model="claude-3-5-sonnet-20240620", llm_provider="anthropic"
+ )
+
+ for i, msg in enumerate(translated_messages):
+ if i == 0:
+ assert msg["content"][0]["cache_control"] == {"type": "ephemeral"}
+ elif i == 1:
+ assert "cache_controls" not in msg["content"][0]
+ elif i == 2:
+ assert msg["content"][0]["cache_control"] == {"type": "ephemeral"}
+
+ print("translated_messages: ", translated_messages)