Skip to content

Let the model replace a junk tool result with a one-line summary (model-judged context pruning) #5362

Description

@nicezic

Describe the feature or problem you'd like to solve

TL;DR: When a tool call returns junk the model didn't intend, let the model swap that output for a one-line summary before the turn ends.
Pay for it once; keep the context clean for the rest of the session.

Human vs AI: the human looks away from junk output, the AI re-reads it every turn

Important

Why prevention can't fix this
You open a file or run a command because you don't know what's in it, so its output size can't be predicted in advance.
A human glances at junk and looks away. A model re-reads it on every turn.
This gives the model the ability to look away. 🙈

Proposed solution

Sequence

TODAY
  turn N        grep -r ...   ──►  ~40 KB of target/ build fingerprints
                model         ──►  "oops, irrelevant"   (but can't drop it)
  turn N+1..50  every request ──►  re-sends the same ~40 KB   ❌ cost + diluted attention

PROPOSED
  turn N        grep -r ...   ──►  ~40 KB
                model         ──►  prune_tool_result(call_id, "grep hit target/ fingerprints, irrelevant")
                context now   ──►  [pruned] grep hit target/ fingerprints, irrelevant   (1 line)
  turn N+1..50  every request ──►  1 line   ✅

Why it's cheap: only the tail changes

[ instructions | turns 1 .. N-1 | junk output | rest of turn N ]
 └───── cached prefix: kept ──────┘ └── re-cached once (small) ──┘

Prompt caching is prefix-based, so pruning a result from the current turn leaves everything before it cached.

Design

Rule Why
Model-judged "This is junk" is a semantic call; no size cap or rule can make it.
Tail-only (current turn) The cached prefix survives; nothing old is rewritten.
Visible Original stays in the user-facing transcript with a one-line notice. Nothing disappears silently.

Bonus: a bigger effective window

Removing junk grows the useful space by more than the junk's share, because the denominator shrinks:

Junk share of context Useful space Effective window gain
10% 90% → 100% +11%
20% 80% → 100% +25%
30% 70% → 100% +43%
50% 50% → 100% +100%

Same hardware, same model: compaction comes later, and what remains is signal. (Junk shares are illustrative.)

Example prompts or workflows

  • grep -r that unexpectedly walks a build/output directory (target/, node_modules/, dist/)
  • cat of a file that turns out to be generated or minified
  • a full pytest / npm install / pip log when only the last error line mattered

In each case the model knows within one glance that the output is noise, but it is carried in every later request.

Additional context

Mechanism Gap
Automatic compaction (with checkpoints) Periodic and lossy; junk rides along until it fires, and the summary can drop what was needed.
Output size limits Cut by size: wanted long output is cut, short-enough junk passes. A grep can be essential one call and junk the next.

"This output is junk" is a semantic judgment only the model can make, at the moment the output arrives.

Everyone wins 🌱

Who What they get
Users Fewer tokens burned per task
The model A clean context, better answers
Providers Less KV cache per session, more sessions per GPU
The planet Less power, fewer GPUs to build

Junk is pure overhead: no answer improves because a build-fingerprint dump was re-read many times. Drop it in every harness, and the whole industry does the same work with less. The cheapest watt is the one you never spend.

Filed across 17 agent harnesses; full list: https://gist.github.com/nicezic/69f13fde6fa080a571825b56fd76392d

Activity

  1. nicezic commented on Nov 5, 2026

    @nicezic
    Author

    Rough impact estimate (Fermi: every number is an assumption)

    TL;DR: a single unnoticed junk result is small. Multiplied by every session and every turn it rides along, it's large.

    Per session

    tokens re-sent per turn after the accident
    
     ~10k ┤ ██████████████████████████████   junk kept      → 10k × 30 turns = 300k wasted
          │
      ~20 ┤ ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁   pruned to 1 line → ~0
          └──────────────────────────────▶ turns N+1 … N+30
    

    Fleet-wide

       1,000,000  sessions / day                (assumed)
     ×         1  junk tool result / session    (~40 KB ≈ 10k tokens)
     ×        30  turns it is carried
     ─────────────────────────────────────────
     ≈       300  billion cached-token reads / day of pure waste
    

    Who gains

    Effect
    Subscription users Same work, slower quota burn
    API users Lower bills
    GitHub Less inference compute on flat-rate plans
    Everyone Less attention diluted by noise → better answers (not counted above, probably the biggest win)

    Caveats

    • Session count, junk frequency and carry length are guesses; the point is the order of magnitude, not the number.
    • Cache reads are discounted, not free, so the saving is real but smaller than raw token counts suggest.

    (If this ships, I'll happily accept a lifetime GitHub Copilot Pro+ plan as payment. Just kidding, sorry! 😄)

  2. nicezic commented on Nov 5, 2026

    @nicezic
    Author

    Quality, not just cost

    TL;DR: clean context makes better agents. Model-judged pruning removes the noise that rule-based clearing keeps.

    Evidence that context hygiene moves results

    Anthropic's context management post reports, on an internal agentic-search eval:

    Setup Result
    Context editing (clearing stale tool results) +29% vs baseline
    Context editing + memory tool +39% vs baseline
    100-turn web-search eval −84% tokens

    Where junk comes from in autonomous runs (SWE-bench / Terminal-Bench style)

    no human in the loop, dozens to hundreds of tool calls
    
     step 12  cat big_module.py      ──►  5,000 lines   (needed: 40)
     step 19  pytest                 ──►  3,000 lines   (needed: 1 traceback)
     step 27  pip install -e .       ──►    400 lines   (needed: 0)
     step 33  find / -name "*.cfg"   ──►  2,000 paths   (needed: 1)
              ...every later step re-reads all of it
    

    The longer the task, the more the noise competes with the signal, and long-horizon tasks are where agent benchmarks are won or lost.

    Why model-judged beats rules here

    Approach What happens to the pytest output above
    Size truncation May cut the one traceback that mattered
    Keep-newest-N clearing Keeps all 3,000 lines: it's recent
    Model-judged (prune_tool_result) Keeps the traceback, prunes the rest to one line

    Suggested A/B

    Arm Harness
    A Current
    B Current + prune_tool_result

    Metrics: resolve rate, tokens per task, and over-prune rate (how often the model pruned something it later needed; this is the main risk to measure).

  3. added
    area:context-memoryContext window, memory, compaction, checkpoints, and instruction loading
    and removed on Nov 6, 2026
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

    area:context-memoryContext window, memory, compaction, checkpoints, and instruction loading

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions