anthropic_thinking_policy.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """Shared Anthropic prior-turn thinking retention policy.
  2. Anthropic preserves and bills prior assistant thinking on Opus 4.5+, Sonnet
  3. 4.6+ and Haiku 5.5+ (plus the newer Fable/Mythos families). Older models
  4. accept replayed blocks but strip them server-side. Keep the model capability
  5. and the per-endpoint replay contract in one place so message conversion and
  6. context accounting cannot diverge.
  7. Unknown or future Claude ids default to keep: replaying a block the API
  8. strips costs nothing, while stripping on a model that keeps it rewrites the
  9. cached prefix on every call. Only the known last-turn-only generations
  10. (Claude 3, Haiku < 5.5, Opus < 4.5, Sonnet < 4.6) strip. Non-Claude ids never keep.
  11. """
  12. from __future__ import annotations
  13. import re
  14. from typing import Any
  15. from agent.anthropic_endpoints import (
  16. _is_claude_platform_endpoint,
  17. _is_deepseek_anthropic_endpoint,
  18. _is_kimi_family_endpoint,
  19. _is_minimax_anthropic_endpoint,
  20. _is_nous_portal_endpoint,
  21. _is_third_party_anthropic_endpoint,
  22. _model_name_is_deepseek_thinking,
  23. )
  24. _CLAUDE_VERSION_RE = re.compile(
  25. # Semantic minors are short version components. Snapshot dates such as
  26. # claude-opus-4-20250514 must remain 4.0 rather than becoming 4.20250514.
  27. r"claude[-_.](opus|sonnet|haiku|fable|mythos)[-_.](\d+)(?:[-_.](\d{1,2})(?=$|[-_.]))?",
  28. re.IGNORECASE,
  29. )
  30. # Claude 3.x ids put the family after the version (claude-3-5-haiku); 4+ ids are version-gated below.
  31. _LAST_TURN_ONLY_RE = re.compile(r"claude[-_.]?3(?!\d)", re.IGNORECASE)
  32. _KEEP_ALL_FROM = {"opus": (4, 5), "sonnet": (4, 6), "haiku": (5, 5)}
  33. def claude_family_version(model: Any) -> tuple[str, tuple[int, int]] | None:
  34. if not isinstance(model, str):
  35. return None
  36. match = _CLAUDE_VERSION_RE.search(model.strip())
  37. if not match:
  38. return None
  39. family = match.group(1).lower()
  40. major = int(match.group(2))
  41. minor = int(match.group(3) or 0)
  42. return family, (major, minor)
  43. def model_preserves_prior_thinking(model: Any) -> bool:
  44. """Whether Anthropic keeps prior assistant thinking in model-visible context."""
  45. if not isinstance(model, str) or "claude" not in model.lower() or _LAST_TURN_ONLY_RE.search(model):
  46. return False
  47. parsed = claude_family_version(model)
  48. if parsed is None:
  49. return True
  50. family, version = parsed
  51. return version >= _KEEP_ALL_FROM.get(family, (5, 0))
  52. # Routes whose thinking blocks carry signatures the upstream verifies: they replay signed blocks, and a
  53. # signature 400 there is healed by anthropic_thinking_replay's durable suppression.
  54. SIGNED_REPLAY_ROUTES = frozenset({"native", "minimax"})
  55. def _is_off_anthropic(base_url: Any) -> bool:
  56. return _is_third_party_anthropic_endpoint(base_url) and not _is_nous_portal_endpoint(base_url)
  57. # First match wins; an unmatched relay host is ``third_party``. DeepSeek models never sign, so they keep
  58. # the unsigned-only contract on any host but Anthropic's own, Claude platforms included.
  59. _ROUTE_RULES = (
  60. ("kimi", lambda url, model: _is_kimi_family_endpoint(url, model)),
  61. ("deepseek", lambda url, model: _is_deepseek_anthropic_endpoint(url)
  62. or (_is_off_anthropic(url) and _model_name_is_deepseek_thinking(model))),
  63. ("native", lambda url, model: not _is_off_anthropic(url) or _is_claude_platform_endpoint(url)),
  64. ("minimax", lambda url, model: _is_minimax_anthropic_endpoint(url)),
  65. )
  66. def anthropic_thinking_route(base_url: Any, model: Any) -> str:
  67. """Which thinking-replay contract an Anthropic Messages request follows: ``kimi`` (replay as-is),
  68. ``deepseek`` (unsigned only), ``native`` (Anthropic-signed blocks: the direct API, Nous Portal,
  69. Bedrock, Vertex AI, Azure AI Foundry), ``minimax`` (its own signed blocks, returned unchanged on
  70. every turn as its docs require) or ``third_party`` (an unknown relay that cannot verify Anthropic
  71. signatures: strip all). The converter, accounting and signature-rejection state share this."""
  72. return next((route for route, matches in _ROUTE_RULES if matches(base_url, model)), "third_party")
  73. def route_replays_prior_thinking(route: str, model: Any) -> bool:
  74. """Whether ``route`` replays signed thinking on assistant turns before the in-flight tool loop."""
  75. return route == "minimax" or (route == "native" and model_preserves_prior_thinking(model))
  76. def native_anthropic_preserves_prior_thinking(base_url: Any, model: Any) -> bool:
  77. """True when the route replays (and the model keeps) signed thinking from earlier turns, so stale
  78. thinking reaches the wire and accounting must charge the signed carriers."""
  79. return route_replays_prior_thinking(anthropic_thinking_route(base_url, model), model)