CodeWhale icon indicating copy to clipboard operation
CodeWhale copied to clipboard

Tool results bloat context: read_file lacks range reading, no per-tool truncation policy, and stale results persist

Open Oliver-ZPLiu opened this issue 6 months ago • 0 comments

Problem

DeepSeek-TUI's token consumption per turn is higher than necessary, for three independent reasons:

1. read_file returns full files unconditionally

read_file currently takes only path (and PDF pages). It always reads and returns the entire file. A 500-line source file (~15KB) goes fully into context even when the model only needs to inspect a few functions.

Impact: Each large read_file adds ~5-20KB of content that stays in context for multiple turns.

Reference: All major coding agents now support line-range reads:

  • Roo Code: read_file(path, offset, limit) with auto-truncation
  • Cursor: dynamic context discovery with file references
  • Claude Code: range-based reads with line numbers

2. No per-tool truncation policy

All tool outputs share the same single threshold (SPILLOVER_THRESHOLD_BYTES = 100KB). Different tools have very different output profiles:

Tool Typical output Ideal strategy
read_file 5-50KB source text ~200 lines / 16KB cap + continuation
grep/search Snippets + paths Top-k + total match count
exec_shell / tests Logs, build output Head+tail + saved reference
ls/list_dir Short listings Full (already small)

A single global threshold cannot serve all these cases well.

3. Stale tool results persist until compaction

Tool results from many turns ago stay fully intact in the conversation window. Compaction only triggers when total tokens exceed a threshold. In the meantime, every API request re-sends old results that the model is unlikely to reference.

Related

  • #1440

Suggested improvements

  • P0: read_file gains start_line/max_lines parameters with default bounded output
  • P1: per-tool visible budget for different output types
  • P2: proactive clearing of old results that are re-readable
  • P3: head+tail truncation for shell/test outputs

Oliver-ZPLiu avatar May 11 '26 13:05 Oliver-ZPLiu