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

Add Prompt Caching Support to Reduce API Costs #14

Open
Kurry opened this issue Dec 15, 2024 · 0 comments
Open

Add Prompt Caching Support to Reduce API Costs #14

Kurry opened this issue Dec 15, 2024 · 0 comments

Comments

@Kurry
Copy link

Kurry commented Dec 15, 2024

Add Prompt Caching Support to Reduce API Costs

Overview

I implemented prompt caching in my fork based on Anthropic's recent updates and saw significant cost reductions from $30-50/day to $1-5/day in API usage.

Implementation Details

The key changes are based on this upstream commit:
anthropics/anthropic-quickstarts@be847c4

The main implementation can be found here:
https://github.com/anthropics/anthropic-quickstarts/blob/main/computer-use-demo/computer_use_demo/loop.py#L116

Benefits

  • Cost Reduction: 90-95% reduction in API costs
  • Performance: Faster response times due to cached prompts
  • Efficiency: Better handling of repetitive tasks and system prompts

Technical Changes Required

  1. Add the anthropic-beta: prompt-caching-2024-07-31 header
  2. Structure system prompts and tools with cache_control
  3. Implement cache tracking for performance monitoring

Example Implementation

system_content = [
    {
        "type": "text",
        "text": "Base system instructions...",
    },
    {
        "type": "text",
        "text": "Large context or tool definitions...",
        "cache_control": {"type": "ephemeral"}
    }
]

Notes

  • My branch is currently ahead of the main, making direct PR difficult
  • The repo seems relatively inactive, so I am sharing as a ticket for future reference
  • Implementation follows Anthropic's official prompt caching guidelines

References

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

No branches or pull requests

1 participant