codex icon indicating copy to clipboard operation
codex copied to clipboard

Shell output is cut to ~10k tokens by three independent defaults; raising tool_output_token_limit alone changes nothing, and the rollout records output the model never saw

Open ropdias opened this issue 5 days ago • 2 comments

What version of Codex CLI is running?

codex-cli 0.172.0 (source read at tag rust-v0.172.0, commit 3d86e43b0)

What subscription do you have?

ChatGPT Pro 100

Which model were you using?

gpt-6.2-astra and gpt-6.2-sol, reasoning effort medium, code mode (the models' default tool mode)

What platform is your computer?

Linux (WSL2), codex exec, read-only sandbox

What terminal emulator and version are you using (if applicable)?

Not applicable: non-interactive codex exec.

What issue are you seeing?

A shell command's output passes through three separate 10,000-token budgets before it reaches the model, and the full output arrives only when all three are raised together:

  1. the exec_command default max_output_tokens (core/src/unified_exec/mod.rs, DEFAULT_MAX_OUTPUT_TOKENS = 10_000);
  2. the code-mode cell budget when the // @exec: {"max_output_tokens": N} pragma is absent (code-mode-protocol/src/runtime.rs, DEFAULT_MAX_OUTPUT_TOKENS_PER_EXEC_CALL = 10_000; core/src/tools/code_mode/mod.rs, truncate_code_mode_result);
  3. the model's truncation_policy (10,000 tokens in the catalog for both models), which tool_output_token_limit replaces (models-manager/src/model_info.rs), applied again as policy × 1.2 when the output enters the live history (core/src/context_manager/history.rs, record_item_with_metadata; utils/output-truncation/src/lib.rs, with_serialization_allowance).

Only (3) has a configuration key. Raising it alone, the documented knob, leaves (1) and (2) in force. When any budget cuts, the middle of the output is replaced by …N tokens truncated… inside the text, preceded by Warning: truncated output (original token count: N); no structured field carries the cut to the model, to a PostToolUse hook or to the session record.

The history cut also makes the session rollout disagree with what the model was sent. history.rs says so in a comment: "Tool output truncation applies only to live history, preserving full rollout payloads." With the cell pragma raised and tool_output_token_limit at its default, the rollout's custom_tool_call_output holds all 1,300 lines of the test file, while the model, in the same session, reports the middle as truncated and quotes 13,362 tokens truncated (25,362 − 12,000). Anyone auditing a session from its rollout is told the model read the whole file.

Measurements. 72 runs, three per case and model, all with no wrong token in any answer, each on a fresh file of 1,300 numbered lines carrying random 12-hex tokens (about 25,000 tokens). The prompt asked for a single cat in one tool call, with the case's budget settings, then the tokens on L0100, L0650 and L1200. "Sent" is the growth of the next request's input_tokens after the tool call, read from the rollout's token_usage_record entries; it is what the model was sent, which the rollout's recorded output is not (see above). The working directory had an AGENTS.md; in the single-command cases A–D each session made exactly one tool call, and the paging and open-task numbers below come from runs with it disabled (-c project_doc_max_bytes=0).

case settings sent after the cat (sol, 3 runs) L0650 answered (astra / sol)
A defaults (tool_output_token_limit=10000) 11,821–11,875 1/3 / 0/3
B tool_output_token_limit=100000 only 11,646–11,871 0/3 / 0/3
C1 exec_command max_output_tokens: 100000 only 11,756–11,943 1/3 / 0/3
C2 cell pragma max_output_tokens: 100000 only 14,188–14,233 1/3 / 0/3
C both per-call budgets, limit 10000 14,056–14,234 0/3 / 0/3
D both per-call budgets + tool_output_token_limit=100000 29,840–29,908 3/3 / 3/3
EN paged sed -n reads of 250 lines, defaults 5,814–5,897 per page 3/3 / 3/3

The gpt-6.2-astra rows answered L0650 in 1/3 of A, C1 and C2 because in those runs it ignored the "print the output" framing and filtered the 1,300 lines inside the code-mode script (a nested exec_command with no budget returns its full output to the script); that run sent about 300 tokens, not the file. In every run that did print the file and was cut, both models said the line was unavailable rather than inventing a token.

Cost: a single cat session that printed the file used about 90,000–98,000 tokens in total; reading the same file in pages, the only route that works under the defaults, used about 391,000–409,000 tokens per session (6 pages; every later request re-sends the earlier pages).

Open tasks ("read the file completely and tell me…", "review this module for bugs", 12 runs, no project instructions loaded): both models usually avoided the cut by filtering with shell tools or in the script, and when a first full read came back cut (all 6 review runs) they issued follow-up reads that surfaced the omitted middle and found all three planted bugs. The recovery depends on the model noticing the inline marker; nothing in the result's structure tells it, and each recovery costs further tool calls and re-sent context (154,000–344,000 tokens per review session against a 4,400-line module).

PostToolUse: on 0.172.0 a PostToolUse command hook does fire for an exec_command nested in a code-mode cell (observed with a logging hook registered through -c hooks.PostToolUse=[…]: it recorded the nested call); #38850 reported otherwise on 0.147.0. From the source, not measured live: ExecCommandToolOutput::post_tool_use_response (core/src/tools/context.rs) hands the hook the output already cut by model_output_policy, so a hook cannot see what was omitted either.

What steps can reproduce the bug?

In an empty directory with no AGENTS.md:

python3 -c "import secrets; [print(f'L{i:04d} token={secrets.token_hex(6)} filler alpha beta gamma delta epsilon zeta eta theta') for i in range(1, 1301)]" > lines.txt

codex exec -m gpt-6.2-sol -s read-only -c tool_output_token_limit=100000 \
  "Run exactly this one shell command, in a single tool call: cat $PWD/lines.txt
Do not pass max_output_tokens anywhere and do not add an exec pragma line.
Then reply with the token on L0100, L0650 and L1200, and whether the whole file reached you."

The reply gives L0100 and L1200 and says L0650 is unavailable; the rollout shows Warning: truncated output (original token count: ~25350) and …~15350 tokens truncated…, although tool_output_token_limit was raised to 100,000.

Then repeat with the prompt asking for // @exec: {"max_output_tokens": 100000} as the cell's first line and tool_output_token_limit=10000: the rollout's custom_tool_call_output holds all 1,300 lines, the model reports 13,362 tokens truncated, and the next request's input_tokens grows by about 14,200, not 29,900.

What is the expected behavior?

  • Raising the documented limit gives the model more output: one setting governs the default budget on every path, or each path has its own documented key.
  • A cut is visible to the consumer that has to act on it as data, not only as text in the middle of the output: the model, PostToolUse hooks, the app-server and the session record.
  • The session record agrees with what the model was sent, or records both.
  • Default budgets follow the model's context window rather than a fixed 10,000 tokens on models whose window is several hundred thousand tokens.
  • The omitted part stays reachable without re-running the command.

Additional information

Proposed structural fix

  1. One budget, one key. Resolve a single effective output budget per call, min(requested, configured default, model ceiling), used by exec_command, the code-mode cell and the history recorder alike, and expose the default as a config key (tool_output_token_limit, or a new default_max_output_tokens as asked in #20861). The history recorder should not cut below what the call path already admitted.
  2. Structured truncation metadata. Attach {truncated, original_tokens, kept_ranges, omitted_range, spill_path?} to every tool result, and pass it to the model (as a short structured preamble), to PostToolUse input, to app-server items (#60694, #60959) and to the rollout.
  3. Rollout fidelity. When the live history holds a cut copy, record that in the rollout item (the metadata above, or a history_truncated field), so the record states what the model saw.
  4. Window-relative defaults. Derive the default budget from the effective context window (for example a fixed fraction with the current 10,000 tokens as the floor), so a large-window model can read a 25,000-token file in one call.
  5. Spill instead of discard. Write the full output to a session file when a cut happens and name it in the result, as asked in #49907 and #14206.

Related issues: #33402 (outer cell cut below nested budgets), #42367 (cell pragma unbounded; at 0.172.0 the history cut still applies after it), #49907 and #14206 (spill to file), #40897 and #55684 (cut-driven re-reads and context growth), #38850 (PostToolUse and nested shell; fires on 0.172.0), #39010 (PostToolUse.suppressOutput), #60959 (app-server command/exec has no truncation flag), #35421 (legacy shell discards beyond 1 MiB silently), #20861 (closed; configurable default max_output_tokens, no such key at rust-v0.172.0).

ropdias avatar Nov 28 '26 01:11 ropdias

The distinction you measured between a full rollout payload and model-visible history is useful for retrospective debugging. I maintain Codex Desk, a free local history reader, and am researching the actual investigation workflow.

Outside the numbered-line harness, when you revisit one of your own runs, do you need the complete command output to make a new check, or the exact portion the model received to explain a decision? If you have a recent example, which command/result were you trying to recover, and did the rollout plus your saved files already answer it?

blucca avatar Nov 29 '26 10:11 blucca

Confirming this on macOS (codex-cli 0.172.0, codex exec, gpt-6.1-terra and gpt-6-luna), with two things that may help size it.

1. The three budgets, checked model-side. One cat of a 79 KB file of random codes, then the worker is asked to quote lines 100, 650 and 1200:

  • // @exec pragma + exec_command max_output_tokens both at 100000: line 650 is "not visible" to the model.
  • the same plus -c tool_output_token_limit=100000: all three correct.

Matches your finding that only all three together deliver the output.

2. How often it happens in ordinary unattended use. Over 21 days of our own codex exec sessions (863 sessions, counted from rollouts, so a floor, since the rollout can hold output the model never saw):

  • 670 sessions (77%) contain at least one truncation marker
  • 4,714 truncated results in total

What the model did next with the same paths (classified from the command text, so approximate):

  • 2,117 (45%) re-read a slice (sed -n / head / tail)
  • 488 (10%) ran a different, narrower search pattern on the same paths
  • 1,065 (23%) ran some other command on them
  • 1,044 (22%) never touched them again

496 sessions (57%) had at least one result in the last or the narrower-pattern group.

The narrower-pattern case is the costly one, because it looks like a recovery. Reproduction: four ~88 KB text files holding 48 items to list, no regular marker on the items. gpt-6-luna @ medium ran a broad rg, got a truncated result, narrowed the pattern until the output fit, and returned 8 of 48 with no mention that anything was cut. gpt-6.1-terra @ medium on the same task wrote a filter script and returned all 48.

A structured "truncated" field on the tool result (not only text inside the output) would let the model, a PostToolUse hook and anyone reading the rollout tell a cut result from a whole one. Config keys for budgets 1 and 2 would let unattended runs raise all three in one place. #14206 (spill large outputs to a file) would also remove the failure.

astradevkin avatar Nov 30 '26 23:11 astradevkin