Skip to content
Python

Make the per-reply output-token limit configurable (OPE-177)

Devika Verma2c91bd9a2235
diff --git a/coworker/agent.py b/coworker/agent.py
index 9479718c..8ea2100e 100644
--- a/coworker/agent.py
+++ b/coworker/agent.py
@@ -264,6 +264,11 @@ def build_engine(
workspace_trusted = bool(ws and WorkspaceTrustStore().is_trusted(ws))
config = load_config(ws, workspace_trusted=workspace_trusted)
+ # OPE-177: the configured per-reply output ceiling rides `model_settings`, which
+ # the engine spreads into every provider call (and explorer subagents inherit).
+ # An explicit `max_tokens` from the caller wins over the config value.
+ if config.max_output_tokens is not None and "max_tokens" not in (model_settings or {}):
+ model_settings = {**(model_settings or {}), "max_tokens": config.max_output_tokens}
executor = LocalExecutor(cwd=ws) if ws is not None else None
todo = TodoList()
context = AgentContext(
diff --git a/coworker/config.py b/coworker/config.py
index 43fe33fa..60cb69e3 100644
--- a/coworker/config.py
+++ b/coworker/config.py
@@ -9,6 +9,8 @@ workspace path. Other permission grants remain global-only.
from __future__ import annotations
+import os
+
try:
import tomllib # stdlib since 3.11
except ModuleNotFoundError: # 3.10, the floor requires-python declares
@@ -32,6 +34,12 @@ class Config:
model: str = "gpt-5.6-sol"
mode: str = "interactive"
max_iterations: int = 150
+ # Per-reply output-token ceiling sent to the provider as `max_tokens` (thinking,
+ # visible text and tool-call arguments all count against it). Unset = each
+ # provider's own default (32,000 for Anthropic and OpenAI-compatible endpoints;
+ # Bedrock 4,096). Anthropic recommends ~64,000 at high effort. Environment override:
+ # COWORKER_MAX_OUTPUT_TOKENS. Explicit `build_engine(model_settings=...)` wins.
+ max_output_tokens: Optional[int] = None
allowed_commands: list[str] = field(
default_factory=lambda: list(DEFAULT_ALLOWED_COMMANDS)
)
@@ -79,6 +87,7 @@ _FIELDS = {
"model",
"mode",
"max_iterations",
+ "max_output_tokens",
"allowed_commands",
"auto_allow",
"allowed_domains",
@@ -129,6 +138,21 @@ def workspace_allowed_commands(workspace: str | Path) -> list[str]:
return list(dict.fromkeys(v.strip() for v in value if isinstance(v, str) and v.strip()))
+MAX_OUTPUT_TOKENS_ENV = "COWORKER_MAX_OUTPUT_TOKENS"
+
+
+def _positive_int(value: Any, source: str) -> Optional[int]:
+ """`max_output_tokens` must be a positive integer (bools are ints in Python and
+ TOML `true` would otherwise pass as 1)."""
+ if value is None:
+ return None
+ if isinstance(value, bool) or not isinstance(value, int) or value <= 0:
+ raise ValueError(
+ f"max_output_tokens must be a positive integer, got {value!r} ({source})"
+ )
+ return value
+
+
def load_config(
workspace: Optional[str | Path] = None,
*,
@@ -154,4 +178,12 @@ def load_config(
[*cfg.allowed_commands, *workspace_allowed_commands(workspace)]
)
)
+ cfg.max_output_tokens = _positive_int(cfg.max_output_tokens, "config.toml")
+ raw = (os.environ.get(MAX_OUTPUT_TOKENS_ENV) or "").strip()
+ if raw:
+ try:
+ parsed: Any = int(raw)
+ except ValueError:
+ parsed = raw
+ cfg.max_output_tokens = _positive_int(parsed, MAX_OUTPUT_TOKENS_ENV)
return cfg
diff --git a/coworker/engine.py b/coworker/engine.py
index f181c7fc..e472df8e 100644
--- a/coworker/engine.py
+++ b/coworker/engine.py
@@ -562,6 +562,9 @@ class TurnEngine:
if turn.finish_reason:
# How the reply ended (OPE-173): `stop` / `tool_calls` / `length`.
payload["finish_reason"] = turn.finish_reason
+ if turn.output_limit:
+ # The output ceiling the provider sent (OPE-177).
+ payload["max_output_tokens"] = turn.output_limit
yield Event(EventType.ASSISTANT_MESSAGE, payload)
if not turn.tool_calls:
@@ -1985,10 +1988,19 @@ class TurnEngine:
"""
# Strip the display-only sidecars — `source` (connector cards), `_display`
# (e.g. filter-hidden counts), `ts` (append-time timestamps), `reasoning`
- # (thinking text), `usage` (token counts), and `finish_reason` (how the reply
- # ended) — copying only messages that carry one. Whole `notice` messages
- # (error/interrupted/model-switch markers) are display-only too: dropped entirely.
- _SIDECARS = ("source", "_display", "ts", "reasoning", "usage", "finish_reason")
+ # (thinking text), `usage` (token counts), `finish_reason` (how the reply ended)
+ # and `max_output_tokens` (the ceiling sent) — copying only messages that carry
+ # one. Whole `notice` messages (error/interrupted/model-switch markers) are
+ # display-only too: dropped entirely.
+ _SIDECARS = (
+ "source",
+ "_display",
+ "ts",
+ "reasoning",
+ "usage",
+ "finish_reason",
+ "max_output_tokens",
+ )
# Auto-compaction (OPE-27): everything before the boundary is represented by the
# compacted block. Outbound-only — the canonical history stays intact — and the
# block+tail are byte-stable between turns, so prompt caching keeps working.
@@ -2107,6 +2119,11 @@ def _assistant_message(turn: AssistantTurn, model: Optional[str] = None) -> dict
# every provider call (`_outbound_messages`). The provider's raw value, where
# it differs, lives in that provider's sidecar (e.g. `_anthropic.stop_reason`).
message["finish_reason"] = turn.finish_reason
+ if turn.output_limit:
+ # The per-reply output ceiling the provider actually sent (OPE-177), so a
+ # `length` finish can be read against the limit that produced it. Display
+ # sidecar like `usage`: stripped before every provider call.
+ message["max_output_tokens"] = turn.output_limit
if turn.reasoning:
# Display-only thinking text — rendered by the GUI, stripped for every provider
# (`_outbound_messages`); provider-private replay blocks go via `extras` instead.
diff --git a/coworker/providers/anthropic_provider.py b/coworker/providers/anthropic_provider.py
index 0e4d59c4..7d26de17 100644
--- a/coworker/providers/anthropic_provider.py
+++ b/coworker/providers/anthropic_provider.py
@@ -19,6 +19,8 @@ from chat.completions in ways the converters must absorb:
from __future__ import annotations
+import logging
+
import json
import re
from typing import Any, Optional
@@ -52,6 +54,8 @@ def _usage_from(usage: Any) -> Optional[TokenUsage]:
# accept ≥32k output.
DEFAULT_MAX_TOKENS = 32000
+logger = logging.getLogger(__name__)
+
# Extended thinking is ON by default (owner call 2026-07-23: no user-facing setting —
# most users wouldn't know what a budget is; a per-turn composer control is future work).
# The provider profile's `thinking_budget` remains a hidden override: a number replaces
@@ -454,7 +458,19 @@ class AnthropicProvider(ProviderClient):
# Budget must fit under max_tokens.
budget = int(thinking.get("budget_tokens") or 0)
floor = max(DEFAULT_MAX_TOKENS, budget + 4096)
- if int(filtered.get("max_tokens") or 0) <= budget:
+ requested = int(filtered.get("max_tokens") or 0)
+ if requested <= budget:
+ if requested:
+ # A configured max_output_tokens (OPE-177) that does not clear the
+ # budget would be rejected by the API; say so rather than silently
+ # sending a different ceiling than the one configured.
+ logger.warning(
+ "max_output_tokens=%d is not above the thinking budget (%d); "
+ "sending max_tokens=%d instead",
+ requested,
+ budget,
+ floor,
+ )
filtered["max_tokens"] = floor
if thinking.get("type") in ("enabled", "adaptive"):
# Sampling knobs are rejected alongside thinking (and removed outright on 4.7+).
@@ -539,6 +555,7 @@ class AnthropicProvider(ProviderClient):
reasoning=_reasoning_text(thinking_blocks),
extras=_anthropic_extras(thinking_blocks, stop_reason),
usage=_usage_from(getattr(response, "usage", None)),
+ output_limit=kwargs.get("max_tokens"),
)
def capabilities(self, model: str) -> ModelCapabilities:
@@ -660,5 +677,6 @@ class AnthropicProvider(ProviderClient):
reasoning=_reasoning_text(thinking_blocks),
extras=_anthropic_extras(thinking_blocks, stop_reason),
usage=usage,
+ output_limit=kwargs.get("max_tokens"),
)
)
diff --git a/coworker/providers/base.py b/coworker/providers/base.py
index 93ec660b..694b3d8c 100644
--- a/coworker/providers/base.py
+++ b/coworker/providers/base.py
@@ -71,6 +71,12 @@ class AssistantTurn:
# Token counts for this round-trip, normalized across providers. None when the
# backend didn't report usage (some compat servers) — never guessed.
usage: Optional[TokenUsage] = None
+ # The per-reply output-token ceiling the provider actually sent on this request
+ # (`max_tokens` / `max_completion_tokens` / `max_output_tokens`), after any
+ # provider-side adjustment (Anthropic budget floor, OpenAI rename). None when the
+ # provider left it to the server. Persisted on the assistant message as the
+ # `max_output_tokens` sidecar so a run record states its ceiling (OPE-177).
+ output_limit: Optional[int] = None
@property
def has_tool_calls(self) -> bool:
diff --git a/coworker/providers/bedrock_provider.py b/coworker/providers/bedrock_provider.py
index b24fb6f9..6be7c4e4 100644
--- a/coworker/providers/bedrock_provider.py
+++ b/coworker/providers/bedrock_provider.py
@@ -63,6 +63,10 @@ def _usage_from(usage: Any) -> Optional[TokenUsage]:
# Converse has no required max token param but per-model defaults vary wildly (Meta's is
# 512 — an agent turn gets truncated mid-tool-call); 4096 fits every family's ceiling.
DEFAULT_MAX_TOKENS = 4096
+# Deliberately lower than the 32,000 of the direct Anthropic/OpenAI clients: Bedrock
+# enforces per-model output ceilings that differ by family and region, and a request
+# above the ceiling is a hard 400. `max_output_tokens` in config (OPE-177) raises it
+# for deployments that know their models' limits.
# Converse stopReason → the engine's OpenAI-shaped finish_reason vocabulary.
_STOP_REASON_MAP = {
diff --git a/coworker/providers/openai_provider.py b/coworker/providers/openai_provider.py
index 3e7ca306..c98d0352 100644
--- a/coworker/providers/openai_provider.py
+++ b/coworker/providers/openai_provider.py
@@ -120,6 +120,13 @@ def _param_fix_retry(kwargs: dict[str, Any], exc: Exception) -> dict[str, Any]:
raise exc
+def _output_limit(kwargs: dict[str, Any]) -> Optional[int]:
+ """The completion ceiling actually sent, after `_param_fix_retry` may have renamed
+ `max_tokens` to `max_completion_tokens` or dropped it (server default -> None)."""
+ value = kwargs.get("max_tokens", kwargs.get("max_completion_tokens"))
+ return int(value) if value else None
+
+
def _usage_from(usage: Any) -> Optional[TokenUsage]:
"""chat.completions usage → normalized counts. `prompt_tokens` INCLUDES cached
tokens, so the cached share is subtracted into `cache_read`; no write-side split
@@ -219,6 +226,7 @@ class OpenAIProvider(ProviderClient):
raw=response,
reasoning=_delta_reasoning(message),
usage=_usage_from(getattr(response, "usage", None)),
+ output_limit=_output_limit(kwargs),
)
def capabilities(self, model: str) -> ModelCapabilities:
@@ -317,6 +325,7 @@ class OpenAIProvider(ProviderClient):
finish_reason=finish_reason,
reasoning="".join(reasoning_parts) or None,
usage=usage,
+ output_limit=_output_limit(kwargs),
)
)
diff --git a/docs/config.example.toml b/docs/config.example.toml
index 9638ad23..b3b6b0c9 100644
--- a/docs/config.example.toml
+++ b/docs/config.example.toml
@@ -7,6 +7,9 @@
model = "gpt-5.5" # default model id (override per session in the UI/CLI)
mode = "interactive" # plan | interactive | auto | custom
max_iterations = 12 # max model<->tool iterations per turn before stopping
+# max_output_tokens = 64000 # per-reply output cap sent as max_tokens (thinking + text +
+ # tool arguments). Unset = provider default (32000; Bedrock
+ # 4096). Env override: COWORKER_MAX_OUTPUT_TOKENS.
# Commands auto-allowed without an approval prompt (prefix match).
allowed_commands = [
diff --git a/tests/test_max_output_tokens.py b/tests/test_max_output_tokens.py
new file mode 100644
index 00000000..023f8504
--- /dev/null
+++ b/tests/test_max_output_tokens.py
@@ -0,0 +1,335 @@
+"""OPE-177: the per-reply output-token limit is configurable.
+
+`max_output_tokens` (config.toml, `COWORKER_MAX_OUTPUT_TOKENS`, or an explicit
+`model_settings["max_tokens"]`) reaches every provider request as `max_tokens`; unset keeps
+each provider's own default byte-for-byte. The value the provider actually sent (after the
+Anthropic budget-thinking floor, or the OpenAI `max_completion_tokens` rename) comes back on
+`AssistantTurn.output_limit` and is persisted on the assistant message as the
+`max_output_tokens` sidecar, next to `usage` and `finish_reason`.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import logging
+from contextlib import contextmanager
+from types import SimpleNamespace
+
+import aisuite as ai
+import pytest
+from coworker.agent import build_code_engine
+from coworker.config import load_config
+from coworker.engine import TurnEngine, _assistant_message
+from coworker.events import EventType
+from coworker.permissions import PermissionEngine
+from coworker.providers import (
+ AnthropicProvider,
+ AssistantTurn,
+ ModelCapabilities,
+ OpenAIProvider,
+ ProviderClient,
+)
+from coworker.providers.anthropic_provider import DEFAULT_MAX_TOKENS as ANTHROPIC_DEFAULT
+from coworker.providers.openai_provider import _MAX_TOKENS_ERROR
+from coworker.providers.openai_provider import DEFAULT_MAX_TOKENS as OPENAI_DEFAULT
+from coworker.tools import ToolRegistry
+
+# -- config -------------------------------------------------------------------------
+
+
+def test_config_default_is_unset(tmp_path):
+ cfg = load_config(global_path=tmp_path / "nope.toml")
+ assert cfg.max_output_tokens is None
+
+
+def test_config_global_then_workspace_override(tmp_path):
+ g = tmp_path / "global.toml"
+ g.write_text("max_output_tokens = 64000\n")
+ assert load_config(global_path=g).max_output_tokens == 64000
+ ws = tmp_path / "ws"
+ (ws / ".coworker").mkdir(parents=True)
+ (ws / ".coworker" / "config.toml").write_text("max_output_tokens = 48000\n")
+ assert load_config(ws, global_path=g).max_output_tokens == 48000
+
+
+def test_config_env_override_beats_files(tmp_path, monkeypatch):
+ g = tmp_path / "global.toml"
+ g.write_text("max_output_tokens = 64000\n")
+ monkeypatch.setenv("COWORKER_MAX_OUTPUT_TOKENS", "70000")
+ assert load_config(global_path=g).max_output_tokens == 70000
+
+
+ "toml_value,env_value",
+ [("0", "0"), ("-5", "-5"), ('"lots"', "lots"), ("1.5", "1.5"), ("true", "true")],
+)
+def test_config_rejects_non_positive_or_non_integer(tmp_path, monkeypatch, toml_value, env_value):
+ g = tmp_path / "global.toml"
+ g.write_text(f"max_output_tokens = {toml_value}\n")
+ with pytest.raises(ValueError, match="max_output_tokens"):
+ load_config(global_path=g)
+ g.write_text("")
+ monkeypatch.setenv("COWORKER_MAX_OUTPUT_TOKENS", env_value)
+ with pytest.raises(ValueError, match="max_output_tokens"):
+ load_config(global_path=g)
+
+
+# -- build_engine -------------------------------------------------------------------
+
+
+class _Stub(ProviderClient):
+ def complete(self, **k): # pragma: no cover
+ raise NotImplementedError
+
+ def capabilities(self, m):
+ return ModelCapabilities()
+
+
+def _engine_with_config(tmp_path, toml: str, **kwargs):
+ (tmp_path / ".coworker").mkdir(exist_ok=True)
+ (tmp_path / ".coworker" / "config.toml").write_text(toml)
+ return build_code_engine(workspace=tmp_path, provider=_Stub(), **kwargs)
+
+
+def test_build_engine_passes_config_value_as_max_tokens(tmp_path):
+ engine = _engine_with_config(tmp_path, "max_output_tokens = 64000\n")
+ try:
+ assert engine.model_settings["max_tokens"] == 64000
+ finally:
+ engine.executor.close()
+
+
+def test_build_engine_explicit_model_settings_win_over_config(tmp_path):
+ engine = _engine_with_config(
+ tmp_path, "max_output_tokens = 64000\n", model_settings={"max_tokens": 1000}
+ )
+ try:
+ assert engine.model_settings["max_tokens"] == 1000
+ finally:
+ engine.executor.close()
+
+
+def test_build_engine_unset_leaves_model_settings_empty(tmp_path):
+ engine = _engine_with_config(tmp_path, "max_iterations = 3\n")
+ try:
+ assert engine.model_settings == {}
+ finally:
+ engine.executor.close()
+
+
+# -- Anthropic ----------------------------------------------------------------------
+
+
+class _FakeAnthropic:
+ def __init__(self, response=None, events=None):
+ self.kwargs: dict = {}
+
+ def create(**kwargs):
+ self.kwargs = kwargs
+ if kwargs.get("stream"):
+ return iter(events or [])
+ return response
+
+ @contextmanager
+ def stream(**kwargs):
+ self.kwargs = kwargs
+ yield SimpleNamespace(get_final_message=lambda: response)
+
+ self.messages = SimpleNamespace(create=create, stream=stream)
+ self.beta = SimpleNamespace(messages=SimpleNamespace(create=create, stream=stream))
+
+
+def _anthropic_response(stop_reason="end_turn"):
+ return SimpleNamespace(
+ content=[SimpleNamespace(type="text", text="hi")], stop_reason=stop_reason
+ )
+
+
+def _anthropic_events():
+ return [
+ SimpleNamespace(
+ type="content_block_start",
+ index=0,
+ content_block=SimpleNamespace(type="text", text=""),
+ ),
+ SimpleNamespace(
+ type="content_block_delta",
+ index=0,
+ delta=SimpleNamespace(type="text_delta", text="hi"),
+ ),
+ SimpleNamespace(type="message_delta", delta=SimpleNamespace(stop_reason="end_turn")),
+ ]
+
+
+def test_anthropic_complete_and_stream_send_configured_limit():
+ fake = _FakeAnthropic(response=_anthropic_response(), events=_anthropic_events())
+ provider = AnthropicProvider(client=fake)
+ turn = provider.complete(
+ model="m", messages=[{"role": "user", "content": "x"}], max_tokens=64000
+ )
+ assert fake.kwargs["max_tokens"] == 64000
+ assert turn.output_limit == 64000
+
+ chunks = list(
+ provider.stream(
+ model="m", messages=[{"role": "user", "content": "x"}], max_tokens=64000
+ )
+ )
+ assert fake.kwargs["max_tokens"] == 64000
+ assert chunks[-1].turn.output_limit == 64000
+
+
+def test_anthropic_default_path_unchanged():
+ fake = _FakeAnthropic(response=_anthropic_response())
+ turn = AnthropicProvider(client=fake).complete(
+ model="m", messages=[{"role": "user", "content": "x"}]
+ )
+ assert fake.kwargs["max_tokens"] == ANTHROPIC_DEFAULT
+ assert turn.output_limit == ANTHROPIC_DEFAULT
+
+
+def test_anthropic_budget_floor_overrides_small_value_and_warns(caplog):
+ fake = _FakeAnthropic(response=_anthropic_response())
+ provider = AnthropicProvider(client=fake, thinking_budget=8192)
+ with caplog.at_level(logging.WARNING, logger="coworker.providers.anthropic_provider"):
+ turn = provider.complete(
+ model="claude-haiku-4-5",
+ messages=[{"role": "user", "content": "x"}],
+ max_tokens=4000,
+ )
+ floor = max(ANTHROPIC_DEFAULT, 8192 + 4096)
+ assert fake.kwargs["max_tokens"] == floor
+ assert turn.output_limit == floor
+ assert any("max_output_tokens" in r.getMessage() for r in caplog.records)
+
+ caplog.clear()
+ with caplog.at_level(logging.WARNING, logger="coworker.providers.anthropic_provider"):
+ turn = provider.complete(
+ model="claude-haiku-4-5",
+ messages=[{"role": "user", "content": "x"}],
+ max_tokens=64000,
+ )
+ assert fake.kwargs["max_tokens"] == 64000
+ assert turn.output_limit == 64000
+ assert not caplog.records
+
+
+# -- OpenAI-compatible --------------------------------------------------------------
+
+
+class _FakeOpenAI:
+ """Records every create() kwargs; can reject `max_tokens` once like a reasoning model."""
+
+ def __init__(self, *, stream_chunks=None, response=None, reject_max_tokens_once=False):
+ self.calls: list[dict] = []
+ self._reject = reject_max_tokens_once
+
+ def create(**kwargs):
+ self.calls.append(kwargs)
+ if self._reject and "max_tokens" in kwargs:
+ self._reject = False
+ raise RuntimeError(
+ "Unsupported parameter: " + _MAX_TOKENS_ERROR + " with this model."
+ )
+ return stream_chunks if kwargs.get("stream") else response
+
+ self.chat = SimpleNamespace(completions=SimpleNamespace(create=create))
+
+
+def _openai_chunks():
+ return [
+ SimpleNamespace(
+ choices=[
+ SimpleNamespace(
+ delta=SimpleNamespace(content="hi", tool_calls=None),
+ finish_reason="stop",
+ )
+ ],
+ usage=None,
+ )
+ ]
+
+
+def _openai_response():
+ return SimpleNamespace(
+ choices=[
+ SimpleNamespace(
+ message=SimpleNamespace(content="hi", tool_calls=None), finish_reason="stop"
+ )
+ ],
+ usage=None,
+ )
+
+
+def test_openai_stream_and_complete_send_configured_limit():
+ fake = _FakeOpenAI(stream_chunks=_openai_chunks(), response=_openai_response())
+ provider = OpenAIProvider(client=fake)
+ chunks = list(
+ provider.stream(
+ model="m", messages=[{"role": "user", "content": "x"}], max_tokens=64000
+ )
+ )
+ assert fake.calls[-1]["max_tokens"] == 64000
+ assert chunks[-1].turn.output_limit == 64000
+
+ turn = provider.complete(
+ model="m", messages=[{"role": "user", "content": "x"}], max_tokens=64000
+ )
+ assert fake.calls[-1]["max_tokens"] == 64000
+ assert turn.output_limit == 64000
+
+
+def test_openai_default_path_unchanged():
+ fake = _FakeOpenAI(response=_openai_response())
+ turn = OpenAIProvider(client=fake).complete(
+ model="m", messages=[{"role": "user", "content": "x"}]
+ )
+ assert fake.calls[-1]["max_tokens"] == OPENAI_DEFAULT
+ assert turn.output_limit == OPENAI_DEFAULT
+
+
+def test_openai_rename_retry_keeps_the_configured_limit():
+ fake = _FakeOpenAI(response=_openai_response(), reject_max_tokens_once=True)
+ turn = OpenAIProvider(client=fake).complete(
+ model="m", messages=[{"role": "user", "content": "x"}], max_tokens=64000
+ )
+ assert len(fake.calls) == 2
+ assert "max_tokens" not in fake.calls[-1]
+ assert fake.calls[-1]["max_completion_tokens"] == 64000
+ assert turn.output_limit == 64000
+
+
+# -- engine record ------------------------------------------------------------------
+
+
+class _LimitProvider(ProviderClient):
+ def complete(self, *, model, messages, tools=None, **settings):
+ return AssistantTurn(text="done", finish_reason="stop", output_limit=64000)
+
+ def capabilities(self, model):
+ return ModelCapabilities()
+
+
+def test_engine_records_output_limit_on_message_and_event(tmp_path):
+ registry = ToolRegistry()
+ registry.register_all(ai.toolkits.files(root=str(tmp_path), allow_write=True))
+ engine = TurnEngine(
+ provider=_LimitProvider(),
+ registry=registry,
+ permissions=PermissionEngine(workspace_root=tmp_path),
+ model="gpt-5.5",
+ )
+
+ async def _collect():
+ return [ev async for ev in engine.run("hello")]
+
+ events = asyncio.run(_collect())
+ assistant = next(ev for ev in events if ev.type == EventType.ASSISTANT_MESSAGE)
+ assert assistant.data["max_output_tokens"] == 64000
+ persisted = next(m for m in engine.messages if m.get("role") == "assistant")
+ assert persisted["max_output_tokens"] == 64000
+ assert all("max_output_tokens" not in m for m in engine._outbound_messages())
+
+
+def test_assistant_message_omits_limit_when_unknown():
+ assert "max_output_tokens" not in _assistant_message(AssistantTurn(text="x"))