Skip to content

Conversation

serialx
Copy link
Contributor

@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

Copy link
Contributor

github-actions bot commented Jul 7, 2025

This PR is stale because it has been open for 10 days with no activity.

@github-actions github-actions bot added the stale label Jul 7, 2025
@serialx
Copy link
Contributor Author

serialx commented Jul 7, 2025

I've solved this with a custom model that inherits LitellmModel. Closing because this is a very inelegant approach.

@serialx serialx closed this Jul 7, 2025
@serialx
Copy link
Contributor Author

serialx commented Jul 7, 2025

Here's my code if anyone is interested: https://gist.github.com/serialx/31dd60db626b3dcb646dcd8369fd2552

@ZXTFINAL
Copy link

Here's my code if anyone is interested: https://gist.github.com/serialx/31dd60db626b3dcb646dcd8369fd2552

Thanks! It helps a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants