Skip to content
Python

Enable prompt caching on the Anthropic provider

Rohit C Prasad8991d303e0f6
diff --git a/coworker/providers/anthropic_provider.py b/coworker/providers/anthropic_provider.py
index 2cc5e669..05bc8f3a 100644
--- a/coworker/providers/anthropic_provider.py
+++ b/coworker/providers/anthropic_provider.py
@@ -338,6 +338,32 @@ def convert_tools(tools: Optional[list[dict[str, Any]]]) -> list[dict[str, Any]]
return converted
+def _add_cache_breakpoints(kwargs: dict[str, Any]) -> None:
+ """Opt the request into prompt caching (5-minute ephemeral, prefix-matched).
+
+ Two breakpoints, the standard agent-loop shape:
+ - last system block — caches tools + system together (tools render first);
+ - last content block of the final message — caches the whole conversation
+ prefix, so each request re-reads the previous turns' cache and writes only
+ the new tail (append-only history keeps the prefix byte-identical).
+
+ Outbound-only: the canonical history never carries `cache_control` (the final
+ message's blocks are freshly built by convert_messages — thinking replays sit
+ in earlier assistant turns — and the marked block is copied, not mutated).
+ Prefixes under the model's cacheable minimum silently don't cache; reads bill
+ ~0.1x and show up as `cache_read_input_tokens` (the metering's cache_read).
+ """
+ marker = {"type": "ephemeral"}
+ system = kwargs.get("system")
+ if isinstance(system, str) and system:
+ kwargs["system"] = [{"type": "text", "text": system, "cache_control": marker}]
+ messages = kwargs.get("messages") or []
+ if messages:
+ content = messages[-1].get("content")
+ if isinstance(content, list) and content:
+ content[-1] = {**content[-1], "cache_control": marker}
+
+
def _reasoning_text(thinking_blocks: list[dict[str, Any]]) -> Optional[str]:
"""Display text for the GUI's disclosure — thinking text only (redacted stays opaque)."""
text = "".join(
@@ -425,6 +451,7 @@ class AnthropicProvider(ProviderClient):
kwargs["system"] = system
if tools:
kwargs["tools"] = convert_tools(tools)
+ _add_cache_breakpoints(kwargs)
return kwargs
def complete(
diff --git a/tests/test_anthropic_caching.py b/tests/test_anthropic_caching.py
new file mode 100644
index 00000000..f4aea26f
--- /dev/null
+++ b/tests/test_anthropic_caching.py
@@ -0,0 +1,87 @@
+"""Prompt-caching breakpoints on the Anthropic provider (OPE-42 follow-up).
+
+The provider opts every request into 5-minute ephemeral caching: one breakpoint on
+the last system block (tools + system) and one on the final message's last content
+block (conversation prefix). Outbound-only — persisted history stays clean.
+"""
+
+from __future__ import annotations
+
+from coworker.providers.anthropic_provider import AnthropicProvider
+
+MARKER = {"type": "ephemeral"}
+
+
+def _kwargs(messages, tools=None):
+ return AnthropicProvider(client=object())._request_kwargs(
+ model="claude-haiku-4-5", messages=messages, tools=tools, settings={}
+ )
+
+
+def test_system_becomes_cached_block_list():
+ kwargs = _kwargs(
+ [
+ {"role": "system", "content": "be terse"},
+ {"role": "user", "content": "hi"},
+ ]
+ )
+ assert kwargs["system"] == [
+ {"type": "text", "text": "be terse", "cache_control": MARKER}
+ ]
+
+
+def test_last_message_last_block_carries_breakpoint():
+ kwargs = _kwargs(
+ [
+ {"role": "user", "content": "first"},
+ {"role": "assistant", "content": "reply"},
+ {"role": "user", "content": "second"},
+ ]
+ )
+ messages = kwargs["messages"]
+ assert messages[-1]["content"][-1]["cache_control"] == MARKER
+ # Only the FINAL message is marked — earlier turns stay unmarked so the
+ # prefix bytes match the previous request's cache.
+ for message in messages[:-1]:
+ assert all("cache_control" not in b for b in message["content"])
+
+
+def test_tool_result_last_block_carries_breakpoint():
+ kwargs = _kwargs(
+ [
+ {"role": "user", "content": "run it"},
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "id": "t1",
+ "type": "function",
+ "function": {"name": "ls", "arguments": "{}"},
+ }
+ ],
+ },
+ {"role": "tool", "tool_call_id": "t1", "content": "README.md"},
+ ]
+ )
+ last = kwargs["messages"][-1]["content"][-1]
+ assert last["type"] == "tool_result"
+ assert last["cache_control"] == MARKER
+
+
+def test_persisted_history_is_never_mutated():
+ history = [
+ {"role": "system", "content": "be terse"},
+ {"role": "user", "content": "hi"},
+ ]
+ _kwargs(history)
+ assert history == [
+ {"role": "system", "content": "be terse"},
+ {"role": "user", "content": "hi"},
+ ]
+
+
+def test_no_system_prompt_is_fine():
+ kwargs = _kwargs([{"role": "user", "content": "hi"}])
+ assert "system" not in kwargs
+ assert kwargs["messages"][-1]["content"][-1]["cache_control"] == MARKER
diff --git a/tests/test_anthropic_provider.py b/tests/test_anthropic_provider.py
index 4f234e36..8f5b6065 100644
--- a/tests/test_anthropic_provider.py
+++ b/tests/test_anthropic_provider.py
@@ -263,7 +263,11 @@ def test_complete_text_turn_with_defaults():
and not turn.has_tool_calls
)
assert fake.kwargs["model"] == "claude-sonnet-4-6"
- assert fake.kwargs["system"] == "sys"
+ # System rides as a block list so the last block can carry the prompt-cache
+ # breakpoint (see _add_cache_breakpoints).
+ assert fake.kwargs["system"] == [
+ {"type": "text", "text": "sys", "cache_control": {"type": "ephemeral"}}
+ ]
assert fake.kwargs["max_tokens"] == DEFAULT_MAX_TOKENS # required param, injected
assert "tools" not in fake.kwargs