Skip to content

feat: token efficiency system — 40-60% output reduction#40

Merged
rohitg00 merged 4 commits intomainfrom
feat/token-efficiency
Apr 17, 2026
Merged

feat: token efficiency system — 40-60% output reduction#40
rohitg00 merged 4 commits intomainfrom
feat/token-efficiency

Conversation

@rohitg00
Copy link
Copy Markdown
Owner

@rohitg00 rohitg00 commented Apr 1, 2026

Summary

  • Adds token efficiency system inspired by drona23/claude-token-efficient (MIT)
  • New skill, rule file, and 3 PreToolUse hook scripts to reduce token waste by 40-60%
  • Updates 5 existing files to integrate token efficiency patterns throughout

What's New

New Files (5)

File Purpose
rules/token-efficiency.mdc Anti-sycophancy, ASCII-only, read-before-write, one-pass coding, no re-reads
skills/token-efficiency/SKILL.md Comprehensive guide with task profiles, tool-call budgets, impact measurement
scripts/read-before-write.js PreToolUse hook: warns on Write/Edit without prior Read
scripts/reread-tracker.js PreToolUse hook: detects unnecessary re-reads of unchanged files
scripts/tool-call-budget.js PreToolUse hook: tracks calls against budget thresholds (20/30/50/80)

Updated Files (7)

File Changes
hooks/hooks.json 3 new PreToolUse hooks wired up
rules/core-rules.md Token efficiency rules added to Communication + Context sections
rules/communication-style.mdc Anti-sycophancy rules, output reduction patterns
rules/context-discipline.mdc One-pass discipline, tool-call budgets, no re-reads
skills/cost-tracker/SKILL.md Tool-call budget table, 4 new optimization strategies
skills/context-optimizer/SKILL.md Token Efficiency section with output reduction + task profiles
package.json Version bump to 3.2.0, new keywords

Token Efficiency Techniques

Output Reduction (40-60% savings):

  • No sycophantic openers ("Sure!", "Great question!")
  • No closing fluff ("Let me know if you need anything!")
  • No prompt restatement before answering
  • Code first, explanation only if non-obvious
  • ASCII-only output (no em dashes, smart quotes)

Behavioral Efficiency:

  • One-pass coding: write solution, test, stop if green
  • Read-before-write enforcement via hook
  • Re-read detection and warnings
  • Tool-call budgets with 80% threshold warnings

Task Profiles:

  • Coding: code first, minimal explanation
  • Agent/Pipeline: structured output only, parseable
  • Analysis: finding first, tables over paragraphs

Test plan

  • Verify hooks load without errors on session start
  • Write a file without reading it first — should see warning
  • Re-read an unchanged file — should see warning at 2nd read
  • Make 16+ tool calls — should see budget warning at 16 (80% of 20)
  • Verify existing hooks still work (quality-gate, secret detection)

Summary by CodeRabbit

  • New Features

    • Expanded workflows: Skills 17→24, Agents 6→8, Commands 16→21
    • New commands: /permission-tuner, /cost-tracker, /mcp-audit
    • Token-efficiency workflow and tool-call budget controls
    • Hook events expanded: 18→24 events and 23→29 scripts
  • Documentation

    • Updated README with v3.2 highlights, feature comparison, tips, and related projects
    • Strengthened communication, context-discipline, and token-efficiency guidance

rohitg00 added 3 commits April 1, 2026 13:56
- Add competitor comparison table (vs Superpowers, ECC, gstack, GSD)
- Add What's New in v3.2 section with all new features
- Add Tips from the Community section with attributed quotes
- Add Related Projects section
- Update all counts: 24 skills, 8 agents, 21 commands, 24 hook events
- Expand all tables with new skills, agents, commands, and hooks
- Update structure section with correct counts
- Fix ECC link: anthropics/courses (404) → affaan-m/everything-claude-code
- Fix best-practice link: anthropics/courses (404) → shanraisshan/claude-code-best-practice
- Fix competitor counts: ECC 143→140+, 71→60+; gstack 33→18+, 0→5+
- Add source links to competitor names in comparison table header
…s, one-pass discipline

New skill, rule, and 3 hook scripts to reduce token waste by 40-60%:

- rules/token-efficiency.mdc: anti-sycophancy, ASCII-only, read-before-write,
  one-pass coding, no re-reads, structured output preference
- skills/token-efficiency/SKILL.md: comprehensive guide with task profiles
  (coding/agent/analysis), tool-call budget tiers, measuring impact
- scripts/read-before-write.js: PreToolUse hook warns on Write/Edit without
  prior Read of the target file
- scripts/reread-tracker.js: PreToolUse hook detects unnecessary re-reads of
  unchanged files
- scripts/tool-call-budget.js: PreToolUse hook tracks call count against
  budget thresholds (20/30/50/80) with 80% warnings

Updated existing files:
- hooks/hooks.json: 3 new PreToolUse hooks (read-before-write, reread-tracker,
  tool-call-budget)
- rules/core-rules.md: added token efficiency rules to Communication and Context
- rules/communication-style.mdc: added anti-sycophancy and output reduction rules
- rules/context-discipline.mdc: added one-pass discipline and tool-call budgets
- skills/cost-tracker/SKILL.md: added tool-call budget table and 4 new strategies
- skills/context-optimizer/SKILL.md: added Token Efficiency section with output
  reduction, behavioral efficiency, and task profiles

Inspired by drona23/claude-token-efficient (MIT).
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 1, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 41922795-e15f-438c-af18-7d868394e9f6

📥 Commits

Reviewing files that changed from the base of the PR and between f58c986 and 4e4fcba.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (3)
  • scripts/read-before-write.js
  • scripts/reread-tracker.js
  • scripts/tool-call-budget.js
🚧 Files skipped from review as they are similar to previous changes (3)
  • scripts/tool-call-budget.js
  • scripts/read-before-write.js
  • scripts/reread-tracker.js

📝 Walkthrough

Walkthrough

Updated repo to v3.2: documentation and README refreshed (feature counts, new sections, new commands), added three session-tracking CLI scripts for read-before-write, re-read detection, and tool-call budgeting, extended hooks to invoke those scripts, and tightened rule/skill docs for token-efficiency and communication style.

Changes

Cohort / File(s) Summary
Version & Metadata
package.json
Bumped version 3.1.0 → 3.2.0; added keywords token-efficiency, anti-sycophancy, tool-call-budget.
README & Docs
README.md
Updated v3.2 header counts (Skills 17→24, Agents 6→8, Commands 16→21, Hook events/scripts 18/23→24/29), added "What's New in v3.2", comparison table, community tips, related projects, and new commands (/permission-tuner, /cost-tracker, /mcp-audit).
Hook Configuration
hooks/hooks.json
Added/expanded PreToolUse hooks: run scripts/read-before-write.js for Edit/Write, added Read matcher to run scripts/reread-tracker.js + scripts/read-before-write.js, updated catch-all description to reflect tool-call budget tracking.
Session-tracking Scripts
scripts/read-before-write.js, scripts/reread-tracker.js, scripts/tool-call-budget.js
New Node.js CLI scripts: read-before-write records reads and warns on writes that weren't read; reread-tracker detects repeated reads of unchanged files and exits non-zero after threshold; tool-call-budget persists per-session call counts and emits warnings/limits. All persist state under os.tmpdir()/pro-workflow.
Rules & Communication
rules/communication-style.mdc, rules/context-discipline.mdc, rules/core-rules.md, rules/token-efficiency.mdc
Expanded/added rules enforcing: no filler/sycophantic phrasing, no prompt restatement, ASCII-only punctuation, code-first responses, read-before-write discipline, no re-reads unless changed, one-pass coding, and tool-call budgets. New token-efficiency.mdc created.
Skills
skills/context-optimizer/SKILL.md, skills/cost-tracker/SKILL.md, skills/token-efficiency/SKILL.md
Docs updated/added to capture token-efficiency guidance, tool-call budgets, one-pass discipline, read-before-write enforcement, output profiles, and related metrics.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Hook as PreToolUse Hook
    participant RBW as read-before-write.js
    participant RRT as reread-tracker.js
    participant TCB as tool-call-budget.js
    participant Store as Session State (tmpdir)

    Client->>Hook: Tool=Read(file_path)
    Hook->>RRT: stdin: file_path
    RRT->>Store: read mtime, counter
    RRT-->>Hook: status (may exit non-zero if repeated)
    Hook->>RBW: stdin: file_path
    RBW->>Store: record read timestamp
    Hook->>TCB: record tool call
    TCB->>Store: increment session counter
    TCB-->>Hook: warn/limit messages

    Client->>Hook: Tool=Write(file_path)
    Hook->>RBW: stdin: file_path
    RBW->>Store: verify prior read
    RBW-->>Hook: warn if not read
    Hook->>TCB: record tool call
    TCB->>Store: increment & check thresholds
    TCB-->>Hook: warn/limit messages
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 Hop, I tally reads and calls tonight,

JSON footprints kept in tmpdir light,
No fluff, just checks before writing true,
Budgets warn and rereads bid adieu,
v3.2 — tidy hops and token-wise delight.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: introducing a token efficiency system with specific output reduction targets (40-60%). It is concise, clear, and directly related to the core changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/token-efficiency

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Nitpick comments (4)
skills/cost-tracker/SKILL.md (1)

51-59: Consider a single source of truth for budget tiers.

This table duplicates the budget matrix in skills/token-efficiency/SKILL.md; centralizing or cross-linking would prevent future drift between docs.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@skills/cost-tracker/SKILL.md` around lines 51 - 59, The budget matrix in the
skills/cost-tracker/SKILL.md (the table with headers "Task Type | Tool-Call
Budget | Wrap-Up At") duplicates the matrix in skills/token-efficiency/SKILL.md;
create a single source of truth by removing the duplicated table from
skills/cost-tracker/SKILL.md and replacing it with a clear cross-link to the
canonical matrix (or move the canonical table into a new shared doc like
skills/budget-tiers/SKILL.md), update any references to point to the chosen
canonical location, and add a short comment in both SKILL.md files indicating
where the authoritative budget tiers live so future edits only change one file.
scripts/read-before-write.js (1)

40-54: Read tracking file updates are vulnerable to lost writes under concurrency.

This read-modify-write cycle is not atomic; concurrent tool events can overwrite each other and drop tracked reads.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/read-before-write.js` around lines 40 - 54, The read-modify-write on
readFiles/readTrackFile can lose concurrent updates; instead, when handling the
'Read' tool in the block that reads and writes readTrackFile, acquire an atomic
update by re-reading the file immediately before write, merging the new entry by
taking the max timestamp for the same filePath with any existing value, and then
performing an atomic replace (write to a temp file and fs.rename to replace
readTrackFile) so writes are atomic; optionally retry the read-merge-rename loop
on conflict/errors to ensure concurrent updates are merged rather than
overwritten.
scripts/tool-call-budget.js (1)

49-51: Remove redundant hardcoded check for 65 calls.

This check is inconsistent with the general threshold logic above:

  • The loop already warns at 64 (80 * 0.8) for approaching the 80-call limit
  • Then this fires separately at exactly 65 with a different message

This creates confusing back-to-back warnings at 64 and 65. If 65 is the intended wrap-up point for large features, integrate it into the thresholds array instead.

Proposed fix
-  if (count === 65) {
-    console.error('[TokenEfficiency] 65 tool calls — approaching large-feature limit. Wrap up current task.');
-  }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/tool-call-budget.js` around lines 49 - 51, Remove the hardcoded if
(count === 65) console.error branch and instead add the 65-call wrap-up
threshold into the existing thresholds array/logic used for warnings (the same
mechanism that produced the 64 warning). Update the thresholds data (or
threshold entries) to include an entry for 65 with the desired message
("[TokenEfficiency] 65 tool calls — approaching large-feature limit. Wrap up
current task."), and ensure the loop that checks count against thresholds uses
that entry so you no longer emit back-to-back 64/65 messages; reference the
count variable and the thresholds array/threshold checking code where warnings
are produced.
scripts/reread-tracker.js (1)

24-28: Reading from /dev/stdin is not portable to Windows.

This approach works on Linux and macOS but will fail on Windows. If Windows support is needed, consider using an event-based stdin read pattern like scripts/worktree-create.js (context snippet 2).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/reread-tracker.js` around lines 24 - 28, The code uses
fs.readFileSync('/dev/stdin', 'utf8') which is not portable to Windows; replace
the synchronous /dev/stdin read with an event-based read from process.stdin:
remove the try/catch block around fs.readFileSync('/dev/stdin', 'utf8'), instead
collect chunks on process.stdin 'data' events into the existing input variable
and handle completion on the 'end' event (and call process.exit(0) if input is
empty); ensure you keep the same surrounding logic that uses input so behavior
remains unchanged (look for the input variable and the try/catch/process.exit(0)
lines to update).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@scripts/read-before-write.js`:
- Around line 50-53: The code stores raw toolInput.file_path as the key in
readFiles causing duplicates for relative vs absolute paths; normalize paths
before using them by converting toolInput.file_path to a canonical form (e.g.,
use Node's path.resolve/path.normalize) and use that normalizedPath when
assigning readFiles[normalizedPath] and when later comparing or checking reads
(references: toolInput.file_path, readFiles, readTrackFile); apply the same
normalization logic to the other block mentioned (lines 59-69) so all lookups
and writes use the canonical path consistently.
- Around line 17-22: Sanitize the sessionId before using it in readTrackFile to
prevent path traversal: in the logic that builds sessionId (symbol: sessionId)
strip or replace any path separators and suspicious characters (e.g., slashes,
backslashes, "..") or restrict to a safe character class (alphanumeric,
dashes/underscores), and if the sanitized value is empty, fall back to a safe
default or a short hash; then construct readTrackFile with the sanitized
sessionId (symbols: readTrackFile, getTempDir, ensureDir) so filenames cannot
escape the tempDir.
- Line 25: The code reads stdin using fs.readFileSync('/dev/stdin', 'utf8'),
which is Unix-only and breaks on Windows; update the call to use the file
descriptor number instead: replace the '/dev/stdin' argument with 0 in the
fs.readFileSync call (the statement referencing input and fs.readFileSync) so it
becomes portable across Node.js-supported platforms.

In `@scripts/reread-tracker.js`:
- Around line 64-70: The current reread detection in reread-tracker.js (the
block using tracked[`${filePath}:count`], readCount, and filePath) only logs
errors and exits with code 0; update it to enforce the "Hard rules" by
terminating the process with a non-zero exit when readCount >= 2 (e.g., after
emitting the console.error call, call process.exit(1) or throw an Error) so
violations stop the tool, and ensure the error message mentions the rule and the
file name for clarity; alternatively, if you prefer warnings, update SKILL.md to
mark Read-Before-Write as guidance instead of "Hard rules"—but pick one approach
and implement it consistently.

In `@scripts/tool-call-budget.js`:
- Around line 30-47: The thresholds currently compute warnAt =
Math.floor(t.limit * 0.8) causing warn values (16,24,40,64) to differ from the
documented Wrap-Up At values (15,25,40,65); update the implementation to use
explicit warn values to match the docs by changing the thresholds array entries
(the thresholds constant) to include a warn property (e.g., { limit: 20, warn:
15, label: 'quick-fix budget (20 calls)' }) and replace the computed warnAt with
t.warn when comparing against count inside the loop (the logic around warnAt,
thresholds, and the count comparisons should reference t.warn instead of
Math.floor(...)).

---

Nitpick comments:
In `@scripts/read-before-write.js`:
- Around line 40-54: The read-modify-write on readFiles/readTrackFile can lose
concurrent updates; instead, when handling the 'Read' tool in the block that
reads and writes readTrackFile, acquire an atomic update by re-reading the file
immediately before write, merging the new entry by taking the max timestamp for
the same filePath with any existing value, and then performing an atomic replace
(write to a temp file and fs.rename to replace readTrackFile) so writes are
atomic; optionally retry the read-merge-rename loop on conflict/errors to ensure
concurrent updates are merged rather than overwritten.

In `@scripts/reread-tracker.js`:
- Around line 24-28: The code uses fs.readFileSync('/dev/stdin', 'utf8') which
is not portable to Windows; replace the synchronous /dev/stdin read with an
event-based read from process.stdin: remove the try/catch block around
fs.readFileSync('/dev/stdin', 'utf8'), instead collect chunks on process.stdin
'data' events into the existing input variable and handle completion on the
'end' event (and call process.exit(0) if input is empty); ensure you keep the
same surrounding logic that uses input so behavior remains unchanged (look for
the input variable and the try/catch/process.exit(0) lines to update).

In `@scripts/tool-call-budget.js`:
- Around line 49-51: Remove the hardcoded if (count === 65) console.error branch
and instead add the 65-call wrap-up threshold into the existing thresholds
array/logic used for warnings (the same mechanism that produced the 64 warning).
Update the thresholds data (or threshold entries) to include an entry for 65
with the desired message ("[TokenEfficiency] 65 tool calls — approaching
large-feature limit. Wrap up current task."), and ensure the loop that checks
count against thresholds uses that entry so you no longer emit back-to-back
64/65 messages; reference the count variable and the thresholds array/threshold
checking code where warnings are produced.

In `@skills/cost-tracker/SKILL.md`:
- Around line 51-59: The budget matrix in the skills/cost-tracker/SKILL.md (the
table with headers "Task Type | Tool-Call Budget | Wrap-Up At") duplicates the
matrix in skills/token-efficiency/SKILL.md; create a single source of truth by
removing the duplicated table from skills/cost-tracker/SKILL.md and replacing it
with a clear cross-link to the canonical matrix (or move the canonical table
into a new shared doc like skills/budget-tiers/SKILL.md), update any references
to point to the chosen canonical location, and add a short comment in both
SKILL.md files indicating where the authoritative budget tiers live so future
edits only change one file.
🪄 Autofix (Beta)

✅ Autofix completed


ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 70788312-2e7a-4280-aeb5-beb119dda5f3

📥 Commits

Reviewing files that changed from the base of the PR and between dc11253 and f58c986.

📒 Files selected for processing (13)
  • README.md
  • hooks/hooks.json
  • package.json
  • rules/communication-style.mdc
  • rules/context-discipline.mdc
  • rules/core-rules.md
  • rules/token-efficiency.mdc
  • scripts/read-before-write.js
  • scripts/reread-tracker.js
  • scripts/tool-call-budget.js
  • skills/context-optimizer/SKILL.md
  • skills/cost-tracker/SKILL.md
  • skills/token-efficiency/SKILL.md

Comment thread scripts/read-before-write.js Outdated
Comment thread scripts/read-before-write.js Outdated
Comment thread scripts/read-before-write.js
Comment thread scripts/reread-tracker.js
Comment thread scripts/tool-call-budget.js
@aawadallak
Copy link
Copy Markdown

Hi, I noticed PR #40 is still open and wanted to ask whether there’s any plan to merge it, or if it’s currently blocked for some reason.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 16, 2026

Note

Autofix is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it.

Fixes Applied Successfully

Fixed 4 file(s) based on 5 unresolved review comments.

Files modified:

  • package-lock.json
  • scripts/read-before-write.js
  • scripts/reread-tracker.js
  • scripts/tool-call-budget.js

Commit: 4e4fcba0a7a5288446eac30ff0dc0c3ae9879538

The changes have been pushed to the feat/token-efficiency branch.

Time taken: 2m 37s

Fixed 4 file(s) based on 5 unresolved review comments.

Co-authored-by: CodeRabbit <noreply@coderabbit.ai>
@rohitg00 rohitg00 merged commit 0b21f18 into main Apr 17, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants