Skip to content

Commit 17c9298

Browse files
radekosmulskiclaude
andcommitted
Add TTL support for Anthropic prompt caching
- Update _add_cache_control to accept optional ttl parameter - Pass ttl through mk_msg_anthropic and mk_msgs_anthropic wrappers - Maintain backward compatibility with default ephemeral caching - Add test examples demonstrating TTL usage ("5m", "1h") 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2832121 commit 17c9298

File tree

2 files changed

+66
-83
lines changed

2 files changed

+66
-83
lines changed

msglm/core.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,14 @@ def mk_msgs(msgs: list, *args, api:str="openai", **kw) -> list:
162162
mk_msgs_openai = partial(mk_msgs, api="openai")
163163

164164
# %% ../nbs/00_core.ipynb
165-
def _add_cache_control(msg, cache=False):
166-
"cache `msg`."
165+
def _add_cache_control(msg, cache=False, ttl=None):
166+
"cache `msg` with optional ttl."
167167
if not cache: return msg
168168
if isinstance(msg["content"], str): msg["content"] = [{"type": "text", "text": msg["content"]}]
169-
if isinstance(msg["content"][-1], dict): msg["content"][-1]["cache_control"] = {"type": "ephemeral"}
170-
elif isinstance(msg["content"][-1], abc.Mapping): msg["content"][-1].cache_control = {"type": "ephemeral"}
169+
cache_control = {"type": "ephemeral"}
170+
if ttl is not None: cache_control["ttl"] = ttl
171+
if isinstance(msg["content"][-1], dict): msg["content"][-1]["cache_control"] = cache_control
172+
elif isinstance(msg["content"][-1], abc.Mapping): msg["content"][-1].cache_control = cache_control
171173
return msg
172174

173175
def _remove_cache_ckpts(msg):
@@ -178,18 +180,18 @@ def _remove_cache_ckpts(msg):
178180
return msg
179181

180182
@delegates(mk_msg)
181-
def mk_msg_anthropic(*args, cache=False, **kwargs):
183+
def mk_msg_anthropic(*args, cache=False, ttl=None, **kwargs):
182184
"Create an Anthropic compatible message."
183185
msg = partial(mk_msg, api="anthropic")(*args, **kwargs)
184-
return _add_cache_control(msg, cache=cache)
186+
return _add_cache_control(msg, cache=cache, ttl=ttl)
185187

186188
@delegates(mk_msgs)
187-
def mk_msgs_anthropic(*args, cache=False, cache_last_ckpt_only=False, **kwargs):
189+
def mk_msgs_anthropic(*args, cache=False, ttl=None, cache_last_ckpt_only=False, **kwargs):
188190
"Create a list of Anthropic compatible messages."
189191
msgs = partial(mk_msgs, api="anthropic")(*args, **kwargs)
190192
if cache_last_ckpt_only: msgs = [_remove_cache_ckpts(m) for m in msgs]
191193
if not msgs: return msgs
192-
msgs[-1] = _add_cache_control(msgs[-1], cache=cache)
194+
msgs[-1] = _add_cache_control(msgs[-1], cache=cache, ttl=ttl)
193195
return msgs
194196

195197
# %% ../nbs/00_core.ipynb

0 commit comments

Comments
 (0)