Skip to content

Replace line-based tool output truncation with token-based limits #6426

Description

@provencher

Problem

Currently, Codex uses a naive line-based truncation mechanism for tool outputs:

  • Hard limit: 256 lines OR 10 KiB (whichever is hit first)
  • Strategy: Head+tail truncation showing first 128 lines + last 128 lines

This was introduced in commit 957d449 (August 2025, v0.24.0).

Why This Is Problematic

  1. Arbitrary line limits don't correlate with token usage: A tool output with 256 short lines might only be 1-2k tokens, while another with 100 long lines could be 10k+ tokens. Line-based truncation doesn't account for actual context window pressure.

  2. Forces excessive tool calls: When the model needs comprehensive information (e.g., reading a large file, analyzing test output), it must make many sequential tool calls to work around the 256-line limit, significantly slowing down task completion.

  3. Head+tail strategy loses critical context: Showing first 128 + last 128 lines with a gap in the middle can hide the most important information, especially for:

    • Build outputs where errors appear in the middle
    • Test results with failures scattered throughout
    • File contents where key logic is in the middle sections
  4. Recently made worse for MCP tools: In v0.56, this truncation was extended to MCP tools (previously they weren't truncated until the next turn), making the MCP experience significantly worse.

Proposed Solution

Replace line-based limits with token-based limits similar to how Claude Code handles this:

// Instead of:
const MODEL_FORMAT_MAX_LINES: usize = 256;

// Use:
const MODEL_FORMAT_MAX_TOKENS: usize = 25_000;

Benefits

  1. Aligns with actual context window constraints: Tokens are what matters for the model, not lines
  2. More efficient information transfer: 25k tokens could be 500+ lines of dense code or logs
  3. Fewer tool calls needed: Model can digest more information per call
  4. Faster overall execution: Less back-and-forth means faster task completion

Implementation Notes

  • Use the existing tokenizer in codex-rs/core (already available for truncation in other parts of the codebase)
  • Keep the 10 KiB byte limit as a secondary safeguard to prevent pathological cases
  • Consider keeping head+tail strategy but with token-based boundaries instead of line-based
  • Apply consistently to all tools (built-in and MCP) from the start

Impact

This change would significantly improve:

  • Speed: Fewer tool calls means faster completion
  • Accuracy: Model sees more context per call, makes better decisions
  • MCP experience: Currently degraded in v0.56 due to aggressive line truncation
  • User experience: Less waiting, better results

References

  • Current truncation code: codex-rs/core/src/context_manager/truncate.rs
  • Original line limit introduction: commit 957d449 (Aug 2025)
  • Extension to MCP tools: commit 1c8507b (Oct 2025, v0.54.0)
  • Claude Code tokenizer usage

Metadata

Metadata

Assignees

No one assigned

    Labels

    contextIssues related to context management (including compaction)enhancementNew feature or requesttool-callsIssues related to tool calling

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions