diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py index f2b496a99a..c67550de78 100644 --- a/agent/conversation_loop.py +++ b/agent/conversation_loop.py @@ -2679,10 +2679,12 @@ def run_conversation( # disagreement that dead-looped compaction. from agent.turn_context import _agent_stale_thinking_on_wire - approx_tokens = estimate_messages_tokens_rough( - api_messages, - charge_stale_thinking=_agent_stale_thinking_on_wire(agent), - ) + if _agent_stale_thinking_on_wire(agent): + approx_tokens = estimate_messages_tokens_rough(api_messages) + else: + approx_tokens = estimate_messages_tokens_rough( + api_messages, charge_stale_thinking=False + ) # Route-aware pressure: when the upcoming request is eligible for # native Responses compaction the transport will checkpoint-prune # the payload before sending — the generic durable-history figure diff --git a/agent/model_metadata.py b/agent/model_metadata.py index 2783c73458..f6fa3fdfbe 100644 --- a/agent/model_metadata.py +++ b/agent/model_metadata.py @@ -3836,9 +3836,15 @@ def estimate_request_tokens_rough( if system_prompt: total += estimate_tokens_rough(system_prompt) if messages: - total += estimate_messages_tokens_rough( - messages, charge_stale_thinking=charge_stale_thinking - ) + if charge_stale_thinking: + # Positional-compatible call: test seams and plugin engines + # monkeypatch estimate_messages_tokens_rough with (messages)-only + # signatures; only the route-aware False path needs the kwarg. + total += estimate_messages_tokens_rough(messages) + else: + total += estimate_messages_tokens_rough( + messages, charge_stale_thinking=False + ) if tools: total += _estimate_tools_tokens_rough(tools) return total diff --git a/agent/turn_context.py b/agent/turn_context.py index 698f630b69..3ffaa3f7cd 100644 --- a/agent/turn_context.py +++ b/agent/turn_context.py @@ -95,11 +95,17 @@ def _preflight_request_tokens( "using generic transcript estimate", exc_info=True, ) + if _agent_stale_thinking_on_wire(agent): + return estimate_request_tokens_rough( + messages, + system_prompt=system_prompt or "", + tools=tools, + ) return estimate_request_tokens_rough( messages, system_prompt=system_prompt or "", tools=tools, - charge_stale_thinking=_agent_stale_thinking_on_wire(agent), + charge_stale_thinking=False, )