Skip to content

drmahdikazempour/agent-stack

Repository files navigation

🧰 agent-stack

One command. Everything set up. Activated. Measured.

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.

npm version CI license node zero deps

npx @drmahdikazempour/agent-stack init --all

πŸ€– Paste this into Claude Code

Copy 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 plain npx -y @drmahdikazempour/agent-stack@latest init --yes.


πŸ“– Table of contents


πŸ’‘ Why agent-stack

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

πŸš€ Quick start

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 --all
What 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 --all

βš™οΈ How init works

One 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

πŸ”§ Built-in token cutters

These ship inside the package β€” no external install, nothing fictional. They are what actually reduce tokens:

πŸ—ΊοΈ Code map

agent-stack graph refresh        # rebuild .agent-stack/graph.md
agent-stack graph query <symbol> # find where it's defined

A 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
- …

πŸ—œοΈ Output compression

npm run build 2>&1 | npx @drmahdikazempour/agent-stack compress

A 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.

πŸͺΆ Structural savings (always on)

  • .claudeignore keeps node_modules, build output, media, and lockfiles out of context.
  • CLAUDE.md is generated factual-and-tight β€” ≀ 800 tokens at startup, verified by doctor.
  • Subagents (stack-explorer, stack-reviewer) return conclusions, not file dumps.
  • Terse mode (the max profile) enforces minimal-word answers.

🎚️ Profiles

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 profile

πŸ“Ÿ Command reference

Setup β€” 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 / file

Maintenance β€” 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 files
init 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

πŸ—οΈ Architecture

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 init once 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

πŸ“‰ How it cuts tokens

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

πŸ“Š Measuring savings

Savings are measured, never claimed β€” via the neutral ccusage tool.

# init already stored a baseline. After ~a week of work:
agent-stack measure --since 7d
agent-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%)

πŸ› οΈ Development

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 skills

Important

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.


πŸ—ΊοΈ Roadmap

  • v0.1 β€” one-shot init, profiles, hook merger, generators, 5 skills, maintenance commands, tests, CI
  • v0.2 β€” built-in code map + output compression, max profile / --all, honest install model, robust publish CI
  • v0.3 β€” Claude plugin wrapper, real third-party graph adapters auto-detected, deeper optimize codemods
  • future β€” offline LLMLingua/DSPy compression pass, long-term memory tier

❓ FAQ

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.


πŸ™ Credits & prior art

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.

πŸ”— References


MIT licensed Β· Built for people who'd rather ship than wire configs.

Made with Claude Code.