Skip to content

feat: Support Anthropic Claude prompt caching key "cache_control" #908

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/agents/models/chatcmpl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
return current_assistant_msg

for item in items:
# Anthropic Claude needs explicit prompt cache directive
# But "cache_control" key is not supported by OpenAI models
# So we need to remove it from the item and add it manually to the message
# Check if item is dict-like and try to get cache_control key
cache_control = None
if hasattr(item, "get") and hasattr(item, "__contains__"):
cache_control = item.get("cache_control", None)
if "cache_control" in item:
del item["cache_control"] # type: ignore[typeddict-item]

# 1) Check easy input message
if easy_msg := cls.maybe_easy_input_message(item):
role = easy_msg["role"]
Expand All @@ -301,27 +311,35 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
"role": "user",
"content": cls.extract_all_content(content),
}
if cache_control:
msg_user["cache_control"] = cache_control # type: ignore[typeddict-unknown-key]
result.append(msg_user)
elif role == "system":
flush_assistant_message()
msg_system: ChatCompletionSystemMessageParam = {
"role": "system",
"content": cls.extract_text_content(content),
}
if cache_control:
msg_user["cache_control"] = cache_control # type: ignore[typeddict-unknown-key]
result.append(msg_system)
elif role == "developer":
flush_assistant_message()
msg_developer: ChatCompletionDeveloperMessageParam = {
"role": "developer",
"content": cls.extract_text_content(content),
}
if cache_control:
msg_user["cache_control"] = cache_control # type: ignore[typeddict-unknown-key]
result.append(msg_developer)
elif role == "assistant":
flush_assistant_message()
msg_assistant: ChatCompletionAssistantMessageParam = {
"role": "assistant",
"content": cls.extract_text_content(content),
}
if cache_control:
msg_user["cache_control"] = cache_control # type: ignore[typeddict-unknown-key]
result.append(msg_assistant)
else:
raise UserError(f"Unexpected role in easy_input_message: {role}")
Expand All @@ -337,18 +355,24 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
"role": "user",
"content": cls.extract_all_content(content),
}
if cache_control:
msg_user["cache_control"] = cache_control # type: ignore[typeddict-unknown-key]
result.append(msg_user)
elif role == "system":
msg_system = {
"role": "system",
"content": cls.extract_text_content(content),
}
if cache_control:
msg_user["cache_control"] = cache_control # type: ignore[typeddict-unknown-key]
result.append(msg_system)
elif role == "developer":
msg_developer = {
"role": "developer",
"content": cls.extract_text_content(content),
}
if cache_control:
msg_user["cache_control"] = cache_control # type: ignore[typeddict-unknown-key]
result.append(msg_developer)
else:
raise UserError(f"Unexpected role in input_message: {role}")
Expand Down