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

Conversation

serialx
Copy link

@serialx serialx commented Jun 20, 2025

Anthropic models require explicit cache control blocks for the caching to work. And the way they achieve this is by adding an additional key to the text block like:

    "system": [
      {
        "type": "text",
        "text": "You are an AI assistant tasked with analyzing literary works. Your goal is to provide insightful commentary on themes, characters, and writing style.\n"
      },
      {
        "type": "text",
        "text": "<the entire contents of Pride and Prejudice>",
        "cache_control": {"type": "ephemeral"}
      }
    ],
    "messages": [
      {
        "role": "user",
        "content": "Analyze the major themes in Pride and Prejudice."
      }
    ]

ref: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching

Since "cache_control" key is not in official OpenAI API specs, there are various type errors I needed to maneuver through. Code is not ideal, but I tried to create a patch with minimal impact.

With this patch, we can inject cache control keys to the input like:

input_items: list[TResponseInputItem] = []
input_items.append({"role": "user", "content": "Hello, world", "cache_control": {"type": "ephemeral"}})
result = await Runner.run(current_agent, input_items)

This solves #905

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant