Skip to content

The agent that grows with you

fix(prompt-caching): a request its caller already marked for caching is never treated as a one-shot

Tekniumf84822bba0e5
diff --git a/agent/prompt_caching.py b/agent/prompt_caching.py
index 79449517bf2..1612ef56abf 100644
--- a/agent/prompt_caching.py
+++ b/agent/prompt_caching.py
@@ -35,10 +35,15 @@ def envelope_tool_part_cache_markers_supported(provider: str | None, base_url: s
def is_one_shot_request(messages: list[dict[str, Any]] | None, tools: list[dict[str, Any]] | None) -> bool:
"""True for a request no later call can read back from cache: no tools, no assistant/tool
turns, at most one user turn (compression summaries, titles, MoA synthesis). A marker there
- only buys a cache write (1.25x input at 5m, 2x at 1h) that nothing ever reads."""
+ only buys a cache write (1.25x input at 5m, 2x at 1h) that nothing ever reads. A caller that
+ already decorated the request (a MoA advisor's first iteration, whose prefix later
+ iterations replay) decided it will reuse it, so that decision wins over the shape."""
if tools:
return False
- roles = [m.get("role") for m in messages or [] if isinstance(m, dict)]
+ msgs = [m for m in messages or [] if isinstance(m, dict)]
+ if any("cache_control" in m or _has_part_marker(m.get("content")) for m in msgs):
+ return False
+ roles = [m.get("role") for m in msgs]
return roles.count("user") <= 1 and not any(r in ("assistant", "tool") for r in roles)
diff --git a/tests/agent/test_prompt_cache_ttl_propagation.py b/tests/agent/test_prompt_cache_ttl_propagation.py
index c64c45fdc51..668af181655 100644
--- a/tests/agent/test_prompt_cache_ttl_propagation.py
+++ b/tests/agent/test_prompt_cache_ttl_propagation.py
@@ -252,3 +252,11 @@ class TestAuxFallbackReplanThreadsTtl:
messages, None, destination=destination
)
assert _collect_cache_controls(out_msgs) == [], destination.provider
+ # A caller that decorated its own request (MoA advisor, iteration 1 of a
+ # per-iteration fan-out) will replay that prefix: its markers survive the replan.
+ from agent.prompt_caching import apply_anthropic_cache_control
+ advisor = apply_anthropic_cache_control(messages, cache_ttl="1h", native_anthropic=True)
+ out_msgs, _ = auxiliary_client._replan_synchronous_cache_sections(
+ advisor, None, destination=auxiliary_client._FallbackDestination(
+ "anthropic", "https://api.anthropic.com", "anthropic_messages", "claude-opus-4.8"))
+ assert _collect_cache_controls(out_msgs), "a pre-decorated advisor request must keep caching"