Skip to content

The agent that grows with you

fix: seed Responses budget escalation from the observed ceiling when no cap is configured

teknium14a1daecc18d4
diff --git a/agent/turn_truncation.py b/agent/turn_truncation.py
index 5d60d1144c4..63d218acecd 100644
--- a/agent/turn_truncation.py
+++ b/agent/turn_truncation.py
@@ -526,10 +526,12 @@ def continue_codex_incomplete(
append_message(messages, {"role": "user", "content": _CODEX_INCOMPLETE_NUDGE})
if not interim_has_content and _codex_finish_reason(response) == "incomplete":
agent._ephemeral_reasoning_off = True
- if agent.max_tokens:
- agent._ephemeral_max_output_tokens = min(
- agent.max_tokens * (2 ** n), max(32768, agent.max_tokens)
- )
+ # No configured cap means the provider's own ceiling was hit: the observed
+ # output_tokens IS that ceiling, so seed the escalation from it (else 4096).
+ usage = getattr(response, "usage", None)
+ observed = getattr(usage, "output_tokens", None) if not isinstance(usage, dict) else usage.get("output_tokens")
+ base = agent.max_tokens or int(observed or 0) or 4096
+ agent._ephemeral_max_output_tokens = min(base * (2 ** n), max(32768, base))
if not agent.quiet_mode:
agent._vprint(f"{agent.log_prefix}↻ Codex response incomplete; continuing turn ({n}/3)")
# Spinner/heartbeat notice: these retries can take minutes and otherwise look
diff --git a/tests/agent/test_codex_incomplete_budget_escalation.py b/tests/agent/test_codex_incomplete_budget_escalation.py
index db325949b86..4fe2d0f3392 100644
--- a/tests/agent/test_codex_incomplete_budget_escalation.py
+++ b/tests/agent/test_codex_incomplete_budget_escalation.py
@@ -1,7 +1,8 @@
"""Responses-wire ``status=incomplete`` continuation must change the request when reasoning
consumed the whole output budget (#90393): a retry with the same ``max_output_tokens`` and the
-same effort re-burns the budget identically and the turn can never converge. A reasoning-only
-``status=completed`` response (Codex "still thinking") keeps today's bare-retry behaviour."""
+same effort re-burns the budget identically and the turn can never converge. A fragment that
+already carries visible text is a normal partial answer and keeps today's bare replay; with no
+configured cap the escalation seeds from the ceiling the provider reported."""
from __future__ import annotations
@@ -11,7 +12,7 @@ from unittest.mock import MagicMock
from agent.turn_truncation import continue_codex_incomplete
-def _agent(max_tokens=2000):
+def _agent(max_tokens: int | None = 2000):
agent = MagicMock()
agent.max_tokens = max_tokens
agent.quiet_mode = True
@@ -57,9 +58,21 @@ def test_budget_exhausted_empty_fragment_raises_cap_and_drops_reasoning():
assert agent._ephemeral_max_output_tokens > first_boost
-def test_reasoning_only_completed_response_keeps_bare_retry():
+def test_no_configured_cap_seeds_escalation_from_observed_ceiling():
+ agent = _agent(max_tokens=None)
+ exhausted = SimpleNamespace(
+ status="incomplete", incomplete_details={"reason": "max_output_tokens"},
+ usage=SimpleNamespace(output_tokens=6000, output_tokens_details={"reasoning_tokens": 5997}),
+ )
+ assert _run(agent, exhausted) is None
+ assert agent._ephemeral_max_output_tokens > 6000
+ # A fragment that carries visible text is a normal partial answer: no override, today's replay.
agent = _agent()
- still_thinking = SimpleNamespace(status="completed", incomplete_details=None)
- assert _run(agent, still_thinking) is None
+ agent._interim_assistant_visible_text.return_value = "First paragraph of the essay"
+ messages = [{"role": "user", "content": "write an essay"}]
+ partial = SimpleNamespace(content="First paragraph of the essay", tool_calls=None, reasoning=None,
+ codex_reasoning_items=None)
+ continue_codex_incomplete(agent, partial, "incomplete", messages=messages,
+ conversation_history=None, api_call_count=1, response=exhausted)
assert agent._ephemeral_reasoning_off is False
assert agent._ephemeral_max_output_tokens is None