Skip to content
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

Interim Anthropic Prompt Caching Workaround #952

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ronakrm
Copy link

@ronakrm ronakrm commented Feb 20, 2025

"Enables" prompt cache control through the system prompt for Anthropic models.

It's basically just a hack that -very roughly- checks if your system prompt string looks like a list, assumes that its been json.dumped, json.loads it, and converts everything to Anthropic's format.

Also adds the returned cache usage values to pydantic-ai's Usage.details, so you can see if it's working or not.

The way I'm using this now is by:

system_prompt_json = [
    ...
    {
        "type": "text",
        "text": my_string_prompt,
        "cache_control": {"type": "ephemeral"},
    }
    ...
]
system_prompt_string = json.dumps(system_prompt_json)
agent = Agent[AgentConfig](
    system_prompt=system_prompt_string,
    ...

This shouldn't be merged, but I thought I'd put it up in case others found this useful. Really happy with pydantic-ai and this was one thing that would have otherwise forced me to write/wrap a bunch of other stuff just to get around this.

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