A skills-first optimization toolkit for Claude Code (+ Cursor) that takes any repo from zero to a fully optimized, token-efficient agent setup β in under two minutes.
npx @drmahdikazempour/agent-stack init --allCopy the block below into a Claude Code session at the root of any repo. It installs and activates the full stack, audits the result, baselines token usage, and pushes to main β all in one go.
Set up agent-stack in this repo and ship it. Do all of the following:
1. INSTALL + ACTIVATE the full stack:
npx -y @drmahdikazempour/agent-stack@latest init --all --yes
This detects the host, installs the external tools (rtk, code-review-graph,
graphify, caveman, claude-handoff, gbrain) over the built-in fallbacks,
generates CLAUDE.md + AGENTS.md with the tool coordinator, wires the hooks,
and captures a token baseline. If any tool prints "Finish installing"
guidance (missing toolchain), run that command for me, then continue.
2. AUDIT + LINT the result and summarize:
npx @drmahdikazempour/agent-stack audit
npx @drmahdikazempour/agent-stack doctor
Report the CLAUDE.md token budget, skill/MCP budgets, hook conflicts, and
which tools are available vs absent.
3. TRACK TOKEN USAGE β explain how it works and show the baseline:
npx @drmahdikazempour/agent-stack measure --since 7d
Tell me the baseline tokens/day, where usage is logged
(.agent-stack/usage.jsonl), and that I should re-run measure after ~a week
to see the % reduction. The Stop hook logs ccusage every turn automatically.
4. COMMIT + PUSH TO MAIN:
Stage the generated files, commit with a clear message, and push to main.
If I'm on a feature branch, open a PR and merge it to main instead.
Then confirm CI is green.
Walk me through each step's output as you go, and stop to ask me only if a
step genuinely needs my input (e.g. a missing toolchain you can't install).
Tip: prefer
init --all(full stack). For a lighter setup, replace step 1 with plainnpx -y @drmahdikazempour/agent-stack@latest init --yes.
- Paste this into Claude Code
- Why agent-stack
- Quick start
- How
initworks - Built-in token cutters
- Profiles
- Command reference
- Architecture
- How it cuts tokens
- Measuring savings
- Development
- Roadmap
- FAQ
- References
The Claude token-optimization ecosystem is fragmented into single-layer point tools β shell-output compressors, context graphs, output styles, measurement, continuity. None of them compose. Setting up a repo today means hand-picking 5β10 tools, reading each install doc, hand-merging hooks, hand-writing CLAUDE.md, mirroring to Cursor, and measuring savings yourself.
Note
agent-stack does it in one command β and ships the token-cutting machinery built in, so there's nothing fictional to install and nothing to wire by hand.
| Without agent-stack | With agent-stack |
|---|---|
| Hand-pick & install 5β10 tools | npx β¦ init --all |
Hand-write CLAUDE.md |
Generated, β€ 800 tokens, verified |
Manually merge settings.json hooks |
Single safe merge, sole writer |
| Mirror everything to Cursor by hand | Auto-mirrored, kept in sync |
| Guess at savings | Measured with ccusage |
cd your-repo
# Smart defaults (auto-detects host, profile, package manager):
npx @drmahdikazempour/agent-stack init
# β¦or turn on EVERYTHING at once (max profile):
npx @drmahdikazempour/agent-stack init --allWhat you'll see (click to expand)
agent-stack v0.2.0
Detected:
Host: Claude Code + Cursor
Repo: TypeScript / Next.js / pnpm
Profile: max (confidence: high)
Will write:
20 files β claude, cursor
.claude/settings.json (2 hooks merged)
Proceed? [Y/n] y
β Adapters: ccusage(installed)
β Generated 20 files
β Wired 2 hooks into settings.json
β All skills load, all hooks present, CLAUDE.md verified
β Built code map β .agent-stack/graph.md
β Baseline: 12,340 tokens/day (ccusage, last 7d avg)
Done.
Tip
Install it once globally to get the short agent-stack command everywhere:
npm i -g @drmahdikazempour/agent-stack
agent-stack init --allOne shot, ten steps, fully reversible. --dry-run stops after the plan; --yes skips the single confirm.
detect ββΆ plan ββΆ confirm ββΆ back up ββΆ install ββΆ generate ββΆ wire hooks
β β
--dry-run β (stop) βΌ
activate
summarize βββ baseline βββ code map βββββββββββββββββββββββββββββ β
fails βββΆ roll back
| # | Step | What happens |
|---|---|---|
| 1 | Detect | Host(s), repo type, framework, package manager, existing configs, git state |
| 2 | Plan | Prints a one-screen plan (--dry-run stops here) |
| 3 | Confirm | A single Proceed? [Y/n] (--yes skips) |
| 4 | Back up | Copies any existing config into .agent-stack.bak.<ts>/ |
| 5 | Install | ccusage only β everything else is built in |
| 6 | Generate | CLAUDE.md, skills, subagents, commands, .claudeignore, Cursor mirror, MCP scaffold |
| 7 | Wire hooks | One merged write to .claude/settings.json (sole writer, dedupes, never clobbers yours) |
| 8 | Activate | Verifies each skill loads, each hook is present, CLAUDE.md exists β rolls back on failure |
| 9 | Code map | Builds the initial .agent-stack/graph.md |
| 10 | Baseline | Records a ccusage token snapshot for later comparison |
These ship inside the package β no external install, nothing fictional. They are what actually reduce tokens:
agent-stack graph refresh # rebuild .agent-stack/graph.md
agent-stack graph query <symbol> # find where it's definedA compact index mapping every source file β its exported symbols. The agent greps one small file to find where something lives instead of reading whole directories. Refreshed automatically on SessionStart. Supports TypeScript/JavaScript, Python, Go, and Rust.
# Code map
_142 files, 906 top-level symbols. Grep this to find a symbol before opening source._
- `src/core/detect.ts`: detect
- `src/wire-hooks.ts`: mergeHooks, wireHooks, planHooks, countOurHooks
- β¦
npm run build 2>&1 | npx @drmahdikazempour/agent-stack compressA stdin β stdout filter that strips ANSI codes, folds duplicate lines (line (Γ42)), and head/tail-elides huge output β β 60 % fewer characters on a 500-line log, so noisy commands cost a fraction of the context.
.claudeignorekeepsnode_modules, build output, media, and lockfiles out of context.CLAUDE.mdis generated factual-and-tight β β€ 800 tokens at startup, verified bydoctor.- Subagents (
stack-explorer,stack-reviewer) return conclusions, not file dumps. - Terse mode (the
maxprofile) enforces minimal-word answers.
A profile bundles a graph backend + compression + skill set + hook config. init auto-picks one; swap later with profile use.
| Profile | Graph | Compression | Auto-picked when |
|---|---|---|---|
π’ code (default) |
built-in map | built-in | normal code repo |
π΅ review |
built-in map | built-in | > 500 commits and CODEOWNERS |
π£ multimodal |
built-in map | built-in | β₯ 5 PDFs / video / large images |
π‘ spec |
built-in map | built-in | spec-kit / cc-spex detected |
βͺ research |
none | built-in | --profile research --allow-noncommercial |
π΄ max |
external graph + built-in fallback | built-in + terse + rtk | --all β full external stack on at once |
agent-stack profile use review # swap & regenerate
agent-stack profile show # current profileSetup β run once per repo
agent-stack init [--all] [--yes] [--dry-run] [--targets claude,cursor]
[--profile <name>] [--no-install] [--allow-noncommercial]
[--overwrite] [--force]Token cutters β standalone, in pipes, or via hooks
agent-stack compress # cmd 2>&1 | agent-stack compress
agent-stack graph refresh # rebuild the code map
agent-stack graph query <term> # find a symbol / fileMaintenance β post-install, on demand
agent-stack audit # token counts + budget report
agent-stack optimize # apply audit fixes (with approval)
agent-stack doctor # lint everything (exit 1 on issues)
agent-stack measure [--since 7d] # ccusage baseline vs current
agent-stack profile use <name> # swap profile; regenerate
agent-stack graph use <name> # swap to an external graph (if installed)
agent-stack handoff write|resume # continuity across sessions
agent-stack sync # regenerate Cursor mirror from CLAUDE.md
agent-stack uninstall # restore backup, remove generated filesinit flags in detail
| Flag | Effect |
|---|---|
--all |
Full external stack at once (the max profile): rtk + code-review-graph + graphify + caveman + claude-handoff + gbrain |
--yes |
Skip the single confirm prompt |
--dry-run |
Print the plan, write nothing |
--targets claude,cursor |
Force the host list (Cursor gets the portable subset: rtk + MCP graph tools) |
--profile <name> |
Force a profile (code review multimodal spec research max) |
--no-install |
Write configs only; print install guidance instead of installing |
--overwrite |
Replace existing files instead of merging (still backs up) |
--force |
Re-run even if already installed |
One source of truth, two faces, zero runtime dependencies.
@drmahdikazempour/agent-stack
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CLI (src/) β
β builtin/ graph (code map) Β· compress β
β generate/ claude Β· cursor Β· mcp β
β wire-hooks β sole writer of settings.json β
β activate β verify, or roll back on failure β
β skills/ 5 Agent Skills β
βββββββββββββββββ¬βββββββββββββββββββββββ¬βββββββββββββββββ
writes β mirrors β
βΌ βΌ
π Claude Code π΅ Cursor
CLAUDE.md .cursor/rules/*.mdc
.claude/skills Β· agents AGENTS.md
commands Β· settings.json
.claudeignore Β· graph.md
Skills decide when; the CLI decides how. Skills call the CLI under the hood; you normally run
initonce and never touch the CLI again.
Repository layout
agent-stack/
βββ bin/agent-stack.js # npx entrypoint
βββ src/
β βββ cli.ts # arg parsing + command dispatch
β βββ constants.ts # all spec values (token budgets, limits)
β βββ core/ # detect Β· plan Β· safe-writer Β· backup Β· token estimator
β βββ builtin/ # graph (code map) Β· compress (output compression)
β βββ generate/ # claude Β· cursor Β· mcp Β· coordinator file builders
β βββ adapters/ # registry Β· detect-tools Β· install Β· hooks
β βββ wire-hooks.ts # SOLE writer of settings.json hooks
β βββ activate.ts # post-write verification chain
β βββ audit.ts # token-budget linting
β βββ commands/ # init + maintenance commands
βββ skills/ # 5 Agent Skills (stack-bootstrap, -doctor, β¦)
βββ integrations/ # profiles.json Β· tools.json
βββ templates/ # generation notes
βββ test/ # vitest: unit Β· golden Β· e2e init in a tmpdir
The savings come from changing what enters the context window, not from a black box:
β Before β
After (agent-stack)
βββββββββββββββββββββββββ βββββββββββββββββββββββββββββββββ
read 12 files to find βββΆ grep graph.md β open 1 file
one function
paste a 500-line log βββΆ pipe through compress β ~60% smaller
verbose CLAUDE.md + βββΆ β€800-token CLAUDE.md + .claudeignore
node_modules noise
| Lever | Mechanism | Typical effect |
|---|---|---|
| Code map | grep graph.md instead of reading files |
fewer, smaller file reads |
| Compression | fold/elide large command output | β 60 % fewer chars on big logs |
.claudeignore |
exclude junk from context | no node_modules/build noise |
Tight CLAUDE.md |
factual root β€ 800 tokens | lower fixed startup cost |
| Subagents | return summaries, not dumps | bounded sub-task context |
Savings are measured, never claimed β via the neutral ccusage tool.
# init already stored a baseline. After ~a week of work:
agent-stack measure --since 7dagent-stack measure (since 7d)
Current: 7,180 input tokens/day (ccusage)
Baseline: 12,340 input tokens/day (captured 2026-05-24)
β41.8% input-token reduction vs baseline (target β₯ 40%)
git clone https://github.com/drmahdikazempour/agent-stack
cd agent-stack
npm install
npm run build # tsup β dist/ (ESM + CJS + d.ts)
npm test # vitest: unit + golden + e2e init in a tmpdir
npm run typecheck
node bin/agent-stack.js doctor --skills-only # lint shipped skillsImportant
CI runs typecheck β build β skill lint β tests β a license guard (fails if src/ imports a non-permissive adapter). Publishing is tag-triggered and idempotent (skips if the version is already on npm). Releases use Changesets.
- v0.1 β one-shot
init, profiles, hook merger, generators, 5 skills, maintenance commands, tests, CI - v0.2 β built-in code map + output compression,
maxprofile /--all, honest install model, robust publish CI - v0.3 β Claude plugin wrapper, real third-party graph adapters auto-detected, deeper
optimizecodemods - future β offline LLMLingua/DSPy compression pass, long-term memory tier
Does it install a bunch of random binaries?
No. Only ccusage (the genuine Claude Code usage tool) is auto-installed. Graph and compression are built in. Third-party tools are used only if their real binary is already on your PATH β never installed by name.
Will it clobber my existing CLAUDE.md / settings.json?
No. Everything is backed up to .agent-stack.bak.<ts>/ first, CLAUDE.md is merged, and the hook merger only adds its own entries (tagged + deduped) β your hooks are preserved.
Is it safe to run twice?
Yes β init is idempotent. A matching prior install is a no-op unless you pass --force.
How do I undo it?
agent-stack uninstall restores the pre-init backup and removes the generated files.
agent-stack composes a permissive, real tool stack. It vendors none of it β its built-in code map and compression are original MIT code that act as the fallback when a tool isn't installed. Every integrated tool is MIT or Apache-2.0; nothing non-permissive is wired in. Tools are detected first, then installed via their own toolchains (cargo / uv / pipx / bun / claude plugin), with guidance when a toolchain is missing. Full, transparent attribution with licenses and exact install commands lives in CREDITS.md and integrations/tools.json.
The max profile (init --all) activates, all at once:
| Tool | License | Integration | Job |
|---|---|---|---|
| ccusage | MIT | npm binary | Token-usage measurement (always on) |
| rtk | Apache-2.0 | PATH binary | Command proxy β cut heavy command output 60-90% |
| code-review-graph | MIT | MCP server | Primary code map (graph with edges + impact radius) |
| graphify | MIT | CLI / skill | Knowledge graph for whole-repo, multi-file-type questions |
| caveman | MIT | Claude Code plugin | Terse-output mode |
| claude-handoff | MIT | Claude Code plugin | Session continuity (/handoff:*) |
| gbrain | MIT | Bun CLI / plugin | Persistent cross-session memory |
The generated CLAUDE.md and AGENTS.md carry a tool coordinator that routes each job to the right tool, with the built-ins named as the explicit fallback. Cursor gets only the portable subset (rtk + the MCP/CLI graph tools). Inspiration (not integrated): claude-token-optimizer, superpowers, vercel-labs/skills, gstack.
- π¦ npm β @drmahdikazempour/agent-stack
- π GitHub β drmahdikazempour/agent-stack
- π Credits β CREDITS.md
- π Claude Code docs β docs.anthropic.com/claude-code
- π€ Agent Skills β Claude Agent Skills
- π Cursor rules β docs.cursor.com
- π ccusage β github.com/ryoppippi/ccusage
MIT licensed Β· Built for people who'd rather ship than wire configs.
Made with Claude Code.