Skip to content

Codex tool loop causes context snowballing and multi-million-token input amplification #44305

Open

Description

@JGameMaker92

Summary

Codex appears to have a serious token-efficiency problem during tool-heavy coding sessions. This is not simply “complex tasks use tokens.” Shell commands, file inspections, tool results, progress messages, and prior model interactions accumulate in working context, and subsequent model requests appear to carry much of that accumulated context again.

tool call → result enters context → next model request includes accumulated context → another result is added → next request carries an even larger context → repeat

This makes tool inefficiency multiplicative rather than additive. A redundant inspection is not only wasteful once: its output can remain in context across many later model requests.

Evidence from complete rollout JSONL

I independently audited completed Codex rollouts using the complete rollout JSONL files as the source of truth, not UI estimates or log tails.

Recent run (~11.4 minutes)

  • 34 model requests
  • 33 tool calls
  • 3,521,627 total input tokens processed
  • 3,368,448 cached input tokens
  • 153,179 noncached input tokens
  • 12,403 output tokens
  • 4,897 reasoning output tokens
  • first request: 16,654 input tokens
  • final/largest request: 158,312 input tokens
  • 23 inspection calls
  • 2 edit calls
  • 5 validation calls
  • 1 delivery call
  • 47 surplus repeated file-inspection references
  • 15 large inspection outputs
  • 2 exact duplicate tool calls
  • 0 compactions

The active request context grew roughly 9.5×, from 16.7K to 158.3K tokens, during one ~11-minute run. Cumulative input processed reached 3.52 million tokens.

More extreme observed run

  • 84 model requests
  • 83 tool calls
  • 12,385,089 total input tokens processed
  • 12,184,320 cached input tokens
  • 200,769 noncached input tokens
  • 45,468 output tokens
  • 11,268 reasoning output tokens
  • context grew from 17,182 to 197,167 input tokens
  • 28 inspection calls
  • 18 edit calls
  • 14 validation calls
  • 15 exact duplicate tool calls
  • 53 surplus repeated file-inspection references
  • 18 large inspection outputs

This run processed more than 12 million cumulative input tokens.

Caching materially helps and I am deliberately separating cached from noncached input here. But caching does not solve the underlying context-growth architecture: the model is still repeatedly operating against an enormous accumulated working context.

Why the tool loop is the problem

Nearly every shell/tool action requires another model turn. That next model turn carries the accumulated conversation/tool state needed to decide what happens next:

model → inspect → model → inspect → model → search →
model → inspect → model → edit → model → test → model → inspect

A tiny visible command such as grep, sed, cat, git diff, or a test invocation can therefore have a large hidden downstream token cost when its output becomes persistent context.

A large inspection introduced early in a run can contribute to the input of dozens of subsequent model requests. Effective consumption starts resembling:

context size × subsequent model requests

rather than:

new information introduced

As a result, context can snowball toward the model limit even while Codex is successfully making progress.

Repeated inspection is especially expensive

In the recent example, 23 of 33 tool calls were inspections while only 2 were edits. The audit detected 47 surplus repeated file-inspection references and 15 large inspection outputs.

Some rereading is obviously legitimate during debugging. The problem is that unnecessary rereads are disproportionately expensive because duplicated information can continue traveling through later model requests.

Users should not need to micromanage a capable coding model's file-reading strategy merely to prevent the orchestration layer from producing massive context amplification.

This is not solved by shrinking AGENTS.md/prompts

I have already aggressively reduced persistent repository instructions and removed unnecessary workflow text. That reduces initial request size but does not address the dominant effect.

Saving 1,000 prompt tokens is insignificant when a run begins around 17K and grows to 150K–200K because tool interactions continually enlarge the working state.

Cached tokens do not make this irrelevant

The majority of cumulative input in these examples was cached. That is useful and should absolutely be preserved.

However, repeatedly exposing 3.5M or 12.4M cumulative input tokens during ordinary tool-driven coding still creates:

  • context-window pressure
  • earlier compaction pressure
  • less effective room for useful reasoning
  • increased latency
  • unnecessary model/tool iterations
  • unpredictable usage/allowance consumption
  • greater risk of losing important state during long tasks

What I would like the Codex team to investigate

Please investigate whether Codex tool execution can preserve a compact structured working state rather than repeatedly injecting the accumulated execution transcript into subsequent model requests.

Potential areas:

  • deduplicate previously inspected file content
  • reference cached tool results instead of reinserting their contents
  • evict obsolete inspection output
  • use delta-based file context after edits
  • maintain structured persistent state for known files/commands/results
  • distinguish durable task state from ephemeral shell output
  • summarize old tool interactions before context becomes enormous
  • detect identical/redundant inspections before executing them
  • reuse already-known repository information
  • allow trivial orchestration decisions without another enormous model request where architecture permits

The design goal I think matters most is:

Preserve Codex's understanding of the task without preserving every byte of the journey it took to acquire that understanding.

Desired behavior

A long-running Codex task should be able to inspect files, edit code, run tests, debug failures, and deliver a coherent change without model input growing almost monotonically toward the context limit.

The effective token cost of a tool call should be reasonably proportional to the genuinely new information it provides, rather than that information being multiplied across every remaining model request.

Reproducibility / discoverability

These measurements come from complete Codex rollout JSONL session logs. The behavior has appeared across many completed runs rather than a single anomalous session. Our current independent audit set contains 331 completed Codex runs, tracking request counts, token growth, compactions, tool calls, duplicate calls, repeated inspections, and large inspection outputs.

Keywords for related reports/automated triage: Codex CLI, token efficiency, context growth, context snowballing, context injection, repeated context, tool calls, tool output, cached input tokens, input token amplification, rollout JSONL, compaction, repeated file inspection, shell tool loop, context window, usage limits.

I can provide sanitized per-turn/token-growth measurements from additional rollout logs if useful for reproduction or diagnosis.

Activity

  1. added
    bugSomething isn't working
    CLIIssues related to the Codex CLI
    contextIssues related to context management (including compaction)
    rate-limitsIssues related to rate limits, quotas, and token usage reporting
    model-behaviorIssues related to behaviors exhibited by the model
    on Sep 9, 2026
  2. github-actions commented on Sep 9, 2026

    @github-actions
    Contributor
  3. snappyio commented on Sep 20, 2026

    @snappyio

    Independent reproduction from a Codex Desktop multi-agent orchestration task.

    For the root coordinator alone, between two telemetry snapshots:

    • input tokens increased by 2,106,140
    • cached input increased by 1,979,136
    • noncached input increased by 127,004
    • output increased by only 9,143
    • later model requests were repeatedly processing roughly 50K–60K input tokens

    This excludes the child workers’ usage, so the complete orchestration cost was higher.

    The workload involved a coordinator delegating repository inspection to workers, waiting for results, and performing follow-up checks. This suggests the context-snowballing effect becomes particularly damaging in multi-agent use: the coordinator has its own expanding tool loop, while each worker creates another independent loop.

    One additional observation: after the task remained idle for several hours, one resumed request processed approximately 58,584 input tokens with no cached input. Subsequent requests were cached again. This appears consistent with prompt-cache expiry making resumed long-lived tasks especially costly, although it does not explain the overall amplification.

    Prompt instructions imposed advisory limits on worker calls and coordinator investigation, but these were not reliably obeyed. This reinforces the need for runtime controls rather than prompt-only mitigation:

    • hard per-turn model/tool-call limits;
    • hard cumulative-input or spending limits;
    • automatic checkpoint/termination when limits are reached;
    • compact worker-result summaries rather than worker transcripts;
    • context pruning before coordinator follow-up calls;
    • automatic compaction based on cumulative processed input or context growth.

    No transcript content is included here; the figures come from the task’s provider telemetry.

    Additional feedback submitted as 01a0bacb-16a9-71e0-b989-756fe114996e, 01a0bb2f-885a-7b02-94c9-3016c1c7ae61

  4. JGameMaker92 commented on Sep 23, 2026

    @JGameMaker92
    Author

    Additional controlled same-model A/B evidence

    I ran a fresh read-only side-by-side test against the same Oracle host using the same task and GPT-5.6 Sol / High.

    Metric This Chat Codex
    Time 10.6 sec 91.5 sec → timed out
    Oracle task reads/tools 3, parallel 11, serial-ish
    Reasoning/tool cycles 1 bounded orchestration 11 reasoning + 11 tool calls
    User-visible answer Yes None
    Exact Codex tokens — 296,784
    Codex input tokens — 295,042
    Cached input — 260,608
    Output — 1,742
    Reasoning output — 534

    The behavioral loop looked like this:

    Your prompt
      ↓
    Codex chooses one way to investigate
      ↓
    That returns the wrong/weak answer
      ↓
    Codex thinks again
      ↓
    Chooses another tool/search
      ↓
    That is empty/incomplete
      ↓
    Codex thinks again
      ↓
    Chooses another route
      ↓
    repeat...
    

    Because Codex is serializing the whole loop through a fresh model inference after almost every tool result.

    The terminal is not slow. In our trace, searches often finished in fractions of a second. But then Codex does:

    tool result → full Sol High inference → next command → tool result → full Sol High inference → …
    

    In the clean test it did that 11 times. The whole run took 91.5 seconds, while the actual commands were mostly fast.

    Worse, each new inference carries a growing context. By the end, one continuation alone was reading about 40,200 input tokens. Cumulatively it had processed 295,042 input tokens just to execute those 11 decisions.

    So “Codex thinks again” really means:

    wait several seconds for another large Sol High inference over an increasingly large conversation/tool history.

    That is why it feels glacial.

    The core problem is serial model re-entry after tiny tool operations. In ChatGPT I can batch three independent Oracle reads in parallel and reason once. Codex is repeatedly reasoning between them.

    That higher-level behavior is what I think needs investigation/fixing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIIssues related to the Codex CLIbugSomething isn't workingcontextIssues related to context management (including compaction)model-behaviorIssues related to behaviors exhibited by the modelrate-limitsIssues related to rate limits, quotas, and token usage reporting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions