File size: 935 Bytes
e36381e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""Anthropic prompt caching helper β€” adds cache_control to messages so repeated
system prompts / long contexts cost 10% of full price.
Usage: import this in any bridge that calls Anthropic API directly.
"""
def add_cache_control(messages, threshold=2048):
    """Add cache_control to the longest system message if it's over threshold chars.
    Anthropic cache: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
    Requires anthropic-beta: prompt-caching-2024-07-31 header."""
    if not messages: return messages
    for m in messages:
        if m.get('role') == 'system' and isinstance(m.get('content'), str):
            if len(m['content']) >= threshold:
                # Convert to structured content with cache marker
                m['content'] = [{'type': 'text', 'text': m['content'],
                                  'cache_control': {'type': 'ephemeral'}}]
                break
    return messages