surrogate-1 / bin /lib /prompt_cache.py
Ashira Pitchayapakayakul
feat: migrate $HOME/.claude/* to $HOME/.surrogate/* (clean separation from Claude Code)
e36381e
raw
history blame contribute delete
935 Bytes
"""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