The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(anthropic): price the carrier the converter actually replays
kshitijk4poor8c11a233e230
diff --git a/agent/anthropic_message_convert.py b/agent/anthropic_message_convert.pyindex b83257b3fb1..4094829db68 100644--- a/agent/anthropic_message_convert.py+++ b/agent/anthropic_message_convert.py@@ -377,6 +377,18 @@ def _replay_ordered_blocks(m: Dict[str, Any], ordered_blocks: List[Any]) -> Optireturn replayed+def assistant_replay_carrier(m: Dict[str, Any]) -> Tuple[bool, List[Dict[str, Any]]]:+ """``(ordered, blocks)``: the sanitized ordered sidecar when any block survives it (the whole turn+ then replays from it), else the ``reasoning_details`` thinking blocks. Single owner of carrier+ precedence for conversion, accounting and rejected-signature mirrors."""+ ordered_blocks = m.get("anthropic_content_blocks")+ if isinstance(ordered_blocks, list) and ordered_blocks:+ replayed = _replay_ordered_blocks(m, ordered_blocks)+ if replayed:+ return True, replayed+ return False, _extract_preserved_thinking_blocks(m)++def _convert_assistant_message(m: Dict[str, Any]) -> Dict[str, Any]:"""Assistant message -> Anthropic content blocks (thinking, text, tool_use, Kimi/DeepSeekreasoning_content injection)."""@@ -387,12 +399,9 @@ def _convert_assistant_message(m: Dict[str, Any]) -> Dict[str, Any]:# than relocated. #56195 covered the complementary shape (blank content -> top-level marker); this is# the interleaved thinking + preamble-text + tool_use shape.content = m.get("content", "")- ordered_blocks = m.get("anthropic_content_blocks")- if isinstance(ordered_blocks, list) and ordered_blocks:- replayed = _replay_ordered_blocks(m, ordered_blocks)- if replayed:- return {"role": "assistant", "content": replayed}- blocks = _extract_preserved_thinking_blocks(m)+ ordered, blocks = assistant_replay_carrier(m)+ if ordered:+ return {"role": "assistant", "content": blocks}# Blank text blocks are dropped; a cache marker riding on one is relocated onto the last# surviving cacheable block (prompt_caching sets cache_control on content[-1], which may be# exactly the blank block).diff --git a/agent/message_sanitization.py b/agent/message_sanitization.pyindex 4049bea51e5..22e336e8a6b 100644--- a/agent/message_sanitization.py+++ b/agent/message_sanitization.py@@ -650,10 +650,10 @@ def native_anthropic_accounting_projection(messages: Any) -> tuple[Any, tuple[stif not isinstance(messages, list):return messages, ()+ from agent.anthropic_message_convert import assistant_replay_carrier+projected = []replayed_thinking: list[str] = []- replayable_ordered_types = {"thinking", "redacted_thinking", "text", "tool_use", "image"}- thinking_types = {"thinking", "redacted_thinking"}for message in messages:if not isinstance(message, dict) or message.get("role") != "assistant":projected.append(message)@@ -668,39 +668,17 @@ def native_anthropic_accounting_projection(messages: Any) -> tuple[Any, tuple[stif key in message:shadow[key] = message[key]- ordered = message.get("anthropic_content_blocks")- details = message.get("reasoning_details")-- ordered_authoritative = isinstance(ordered, list) and any(- isinstance(block, dict) and block.get("type") in replayable_ordered_types- for block in ordered- )- details_authoritative = (- not ordered_authoritative- and isinstance(details, list)- and any(- isinstance(block, dict) and block.get("type") in thinking_types- for block in details- )- )- carrier = ordered if ordered_authoritative else details if details_authoritative else None-- # The converter returns/replays the authoritative carrier before considering- # reasoning_content, so it must not be charged in addition to that carrier.- if carrier is not None:+ _, carrier = assistant_replay_carrier(message)+ # The converter ignores reasoning_content for an ordered turn and only injects it when the+ # details carrier holds no thinking, so it must not be charged in addition to the carrier.+ if carrier:shadow.pop("reasoning_content", None)- readable = []- if isinstance(carrier, list):- for block in carrier:- if (- isinstance(block, dict)- and block.get("type") == "thinking"- and isinstance(block.get("thinking"), str)- and block.get("thinking")- ):- readable.append(block["thinking"])- replayed_thinking.extend(readable)+ replayed_thinking.extend(+ block["thinking"]+ for block in carrier+ if block.get("type") == "thinking" and isinstance(block.get("thinking"), str) and block["thinking"]+ )projected.append(shadow)return projected, tuple(replayed_thinking)