Skip to content

The agent that grows with you

fix(anthropic): future Claude ids keep prior thinking by default

kshitijk4poor5d077106b89a
diff --git a/agent/anthropic_thinking_policy.py b/agent/anthropic_thinking_policy.py
index fc81b471874..9772dfd93c1 100644
--- a/agent/anthropic_thinking_policy.py
+++ b/agent/anthropic_thinking_policy.py
@@ -4,6 +4,11 @@ Anthropic preserves and bills prior assistant thinking on Opus 4.5+ and
Sonnet 4.6+ (plus the newer Fable/Mythos families). Older models accept
replayed blocks but strip them server-side. Keep the model capability in
one place so message conversion and context accounting cannot diverge.
+
+Unknown or future Claude ids default to keep: replaying a block the API
+strips costs nothing, while stripping on a model that keeps it rewrites the
+cached prefix on every call. Only the known last-turn-only generations
+(Haiku, Claude 3, Opus < 4.5, Sonnet < 4.6) strip. Non-Claude ids never keep.
"""
from __future__ import annotations
@@ -26,6 +31,7 @@ _CLAUDE_VERSION_RE = re.compile(
r"claude[-_.](opus|sonnet|fable|mythos)[-_.](\d+)(?:[-_.](\d{1,2})(?=$|[-_.]))?",
re.IGNORECASE,
)
+_LAST_TURN_ONLY_RE = re.compile(r"haiku|claude[-_.]?3(?!\d)", re.IGNORECASE)
def _claude_family_version(model: Any) -> tuple[str, tuple[int, int]] | None:
@@ -42,21 +48,13 @@ def _claude_family_version(model: Any) -> tuple[str, tuple[int, int]] | None:
def model_preserves_prior_thinking(model: Any) -> bool:
"""Whether Anthropic keeps prior assistant thinking in model-visible context."""
- if isinstance(model, str) and re.search(
- r"claude[-_.]mythos[-_.]preview", model.strip(), re.IGNORECASE
- ):
- return True
+ if not isinstance(model, str) or "claude" not in model.lower() or _LAST_TURN_ONLY_RE.search(model):
+ return False
parsed = _claude_family_version(model)
if parsed is None:
- return False
+ return True
family, version = parsed
- if family == "opus":
- return version >= (4, 5)
- if family == "sonnet":
- return version >= (4, 6)
- if family in {"fable", "mythos"}:
- return version in {(5, 0), (5, 1)}
- return False
+ return version >= {"opus": (4, 5), "sonnet": (4, 6)}.get(family, (5, 0))
def anthropic_thinking_route(base_url: Any, model: Any) -> str:
diff --git a/agent/anthropic_thinking_replay.py b/agent/anthropic_thinking_replay.py
index b4257ce77a2..8740f06d496 100644
--- a/agent/anthropic_thinking_replay.py
+++ b/agent/anthropic_thinking_replay.py
@@ -5,6 +5,16 @@ signature/data values, then filter those blocks from each rebuilt request copy.
needed beyond the immediate retry because context selection and process resume can rebuild
from canonical history later. The state belongs to one session (and is carried onto its
compression continuation); it applies only where Anthropic signs the blocks.
+
+The fingerprinting is deliberately coarse: one signature 400 marks every signed block in the
+rejected request, so the session falls back to stripping that history (one cache miss, then
+stable) while blocks produced later still replay. The 400's ``messages.N.content.M`` path indexes
+the converted wire, but recovery only sees the pre-conversion ``api_messages``; system extraction,
+tool-result folding and same-role merges shift both indexes, so targeting one block from that path
+could suppress a valid block and resend the bad one.
+
+Recovery covers every native model, including last-turn-only ones: their latest turn still carries
+signed blocks in ordered carriers, which the old ``reasoning_details``-only repair left in place.
"""
from __future__ import annotations
diff --git a/tests/agent/test_anthropic_preserved_thinking_replay.py b/tests/agent/test_anthropic_preserved_thinking_replay.py
index 49ed283d587..7716e31886f 100644
--- a/tests/agent/test_anthropic_preserved_thinking_replay.py
+++ b/tests/agent/test_anthropic_preserved_thinking_replay.py
@@ -191,6 +191,10 @@ _PRESERVED = [
("claude-opus-4-5-20251101", True), ("claude-opus-4-6-20250414", True),
("claude-sonnet-4-5-20250929", False), ("claude-opus-5", True), ("claude-sonnet-5-5", True),
("claude-fable-5-1", True), ("claude-mythos-5", True), ("claude-mythos-preview", True),
+ # Future ids keep by default; known last-turn-only generations and non-Claude ids do not.
+ ("claude-fable-5-2", True), ("claude-mythos-6", True), ("claude-newfamily-7", True),
+ ("claude-haiku-5", False), ("claude-3-7-sonnet-20250219", False), ("claude-sonnet-4", False),
+ ("hermes-4-405b", False),
]
# case -> (model, route, number of growing thinking blocks that must reach the wire)
_ACCOUNTING_CASES = {
@@ -295,7 +299,7 @@ def test_estimates_charge_exactly_the_thinking_the_wire_replays(tmp_path, monkey
@pytest.mark.parametrize(
"boundary",
["same_agent", "rebuild", "session_switch", "compression_child", "context_selection",
- "carrier", "unfingerprintable", "kimi_route"],
+ "carrier", "unfingerprintable", "kimi_route", "future_family"],
)
def test_rejected_thinking_never_returns_and_nothing_else_is_suppressed(
tmp_path, monkeypatch, boundary
@@ -307,7 +311,11 @@ def test_rejected_thinking_never_returns_and_nothing_else_is_suppressed(
from agent.conversation_compression import _carry_session_state_to_child
db = _session_db(tmp_path, "s1", "s2")
- agent = _agent(db, route=KIMI, model="kimi-k2.5") if boundary == "kimi_route" else _agent(db)
+ agent = (
+ _agent(db, route=KIMI, model="kimi-k2.5") if boundary == "kimi_route"
+ else _agent(db, model="claude-fable-5-2") if boundary == "future_family"
+ else _agent(db)
+ )
agent._usage_anchor = {"prompt_tokens": 999}
_patch_assembly_loop(
monkeypatch, _select_canonical_clone if boundary == "context_selection" else None