Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions agent/prompt_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,25 @@ def apply_anthropic_cache_control(
messages, all at the same TTL.

Returns:
Deep copy of messages with cache_control breakpoints injected.
Shallow copy of message list with selective deep copies of modified messages.
"""
messages = copy.deepcopy(api_messages)
if not messages:
return messages
if not api_messages:
return api_messages

messages = list(api_messages)
marker = _build_marker(cache_ttl)

breakpoints_used = 0

if messages[0].get("role") == "system":
messages[0] = copy.deepcopy(messages[0])
_apply_cache_marker(messages[0], marker, native_anthropic=native_anthropic)
breakpoints_used += 1

remaining = 4 - breakpoints_used
non_sys = [i for i in range(len(messages)) if messages[i].get("role") != "system"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current main filters candidates through _can_carry_marker(..., native_anthropic=...) so empty/non-carrier OpenRouter turns do not spend breakpoint capacity. Preserve that filter when salvaging this selective-copy change; see current agent/prompt_caching.py:110-115 and tests/agent/test_prompt_caching.py:198-224.

for idx in non_sys[-remaining:]:
messages[idx] = copy.deepcopy(messages[idx])
_apply_cache_marker(messages[idx], marker, native_anthropic=native_anthropic)

return messages