claude-code - 💡(How to fix) Fix [FEATURE] Built-in token optimization: read caching, test output filtering, large file blocking [2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#49048Fetched 2026-04-17 08:52:20
View on GitHub
Comments
2
Participants
3
Timeline
5
Reactions
0
Timeline (top)
labeled ×3commented ×2
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing requests and this feature hasn't been requested yet
  • This is a single feature request (not multiple features)

Problem Statement

Claude Code consumes tokens every time it reads a file, runs a test, or checks logs. In a typical coding session, I observed several wasteful patterns:

  1. Redundant file reads: The same file gets read 3-5 times per session (before edit, after edit, during review, when referencing). Each re-read consumes tokens for identical content that Claude already has in context.

  2. Verbose test output: Running pytest or jest produces 200+ lines of output (progress dots, warnings, capture logs, fixture setup), but Claude only needs the session start, failures, and summary to make decisions.

  3. Unbounded log output: Commands like cat app.log or docker logs dump entire files into the context when the last 100 lines would suffice.

  4. Large file consumption: Files with 1000+ lines get read in full even when Claude only needs a specific section, consuming massive amounts of tokens unnecessarily.

These patterns compound in longer sessions and lead to faster context exhaustion and higher token costs.

Proposed Solution

I built a set of 6 hook scripts that solve these problems using Claude Code's existing hook system. The hooks work transparently without changing how the user interacts with Claude Code:

Read caching (pre-read.sh + post-file-cache.sh):

  • First read: passes through normally, caches a snapshot
  • Re-read (unchanged): blocked entirely with "File unchanged" message
  • Re-read (after edit): returns only the unified diff, not the full file

Test output filtering (pre-bash.sh + pre-bash.helper.filter.sh):

  • Detects test commands (pytest, jest, vitest, Django test)
  • Pipes output through an awk filter that extracts only: session start, collected count, failures, errors, and final summary
  • 200+ lines become ~8 lines

Log output limiting (pre-bash.sh):

  • Detects log commands without existing truncation (cat *.log, docker logs, journalctl)
  • Automatically appends | tail -100

Large file blocking (pre-read.sh):

  • Files over 1000 lines are blocked unless offset/limit is specified
  • Forces targeted reads instead of full-file consumption

Compaction awareness (post-compact.sh + session-start.sh):

  • Clears read cache after context compaction (since Claude loses detailed file content)
  • Cleans up stale caches older than 7 days

I'd love to see some form of these optimizations built into Claude Code natively, or at minimum, included as an official example/recipe in the docs.

Alternative Solutions

I'm currently using these hooks via a standalone repository with a one-command installer:

Repository: https://github.com/soonswan-study/claude-code-thrifty

The installer symlinks hooks to ~/.claude/hooks/ and merges config into ~/.claude/settings.json. It works well but requires manual installation and jq as a dependency.

Session Lifecycle:

Session Lifecycle

Read Cache Flow:

Read Cache Flow

Test Output Filter:

Test Output Filter

Large File Protection:

Large File Protection

Priority

Medium - Would be very helpful

Feature Category

Performance and speed

Use Case Example

Example scenario:

  1. I'm working on a Django backend with 50+ files
  2. I ask Claude to fix a bug in services/payment.py (400 lines)
  3. Claude reads the file, edits it, then reads it again to verify — that's 800 lines of tokens for one read of identical content
  4. I then ask Claude to run tests: pytest tests/test_payment.py -v which outputs 150 lines, but only 8 lines matter (collected count + 1 failure + summary)
  5. With read caching, the second read is blocked (0 tokens). With test filtering, 150 lines become 8 lines
  6. Over a 30-minute session with multiple files and test runs, this compounds to significant token savings

Additional Context

extent analysis

TL;DR

Implementing hook scripts for read caching, test output filtering, log output limiting, and large file blocking can significantly reduce token consumption in Claude Code.

Guidance

  • Review the proposed hook scripts in the claude-code-thrifty repository to understand how they address redundant file reads, verbose test output, unbounded log output, and large file consumption.
  • Consider integrating these optimizations into Claude Code natively or including them as official examples in the documentation to improve performance and reduce token costs.
  • Evaluate the use of jq for JSON parsing and bash 4.0+ as requirements for the hook scripts.
  • Test the hook scripts in a controlled environment to verify their effectiveness in reducing token consumption before implementing them in production.

Example

# pre-read.sh example
if [ -f "$cache_file" ] && ! $edited; then
  echo "File unchanged"
  exit 0
fi

This example shows how the pre-read.sh script checks if a file has been edited before reading it, and if not, returns a "File unchanged" message to block the read and conserve tokens.

Notes

The proposed solution relies on the existing hook system in Claude Code, which may have limitations or compatibility issues with certain versions or configurations. Additionally, the effectiveness of the hook scripts may vary depending on the specific use case and workflow.

Recommendation

Apply the workaround by using the claude-code-thrifty repository and its hook scripts to optimize token consumption, as integrating these optimizations into Claude Code natively may require significant development and testing efforts.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix [FEATURE] Built-in token optimization: read caching, test output filtering, large file blocking [2 comments, 3 participants]