Skip to content

Commit 4e7c5df

Browse files
alexsoyesclaude
andcommitted
docs(framework): conform token-optimization recipe to recipe contract
Align the recipe with references/recipe-contract.md: drop the Time row (no longer in the template), name the section "Steps to <outcome>" instead of a bare Steps, remove the "ranked by impact" filler line, and give each step how-bullets plus a concrete example. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 96dee3d commit 4e7c5df

1 file changed

Lines changed: 58 additions & 51 deletions

File tree

recipes/token-optimization.md

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,117 +2,124 @@
22

33
> **Goal:** Cut token usage and cost in AI coding assistants without losing output quality.
44
5-
| | |
6-
| ----------------- | ---------------------------------------------------------------------------- |
7-
| **Level** | Intermediate |
8-
| **Time** | ~15 min |
5+
| | |
6+
| ----------------- | ---------------------------------------------------------------------------------- |
7+
| **Level** | Intermediate |
98
| **Prerequisites** | An AI coding assistant (Claude Code, GitHub Copilot, Codex, Cursor, …); a terminal |
109

1110
## Why
1211

13-
Tokens are the bill. Every turn re-sends your context window — instructions, file reads, command output, the model's own narration — and you pay for all of it, every time. Most of it is waste: filler prose, noisy logs, stale context, oversized instruction files. The tips below attack each source. Stack them and large savings are realistic; every percentage here is **as reported by the tool**, not an independent benchmark.
12+
**Token usage** is the bill. Every turn re-sends your whole **context window** — instructions, file reads, command output, narration — and you pay for it again. Most of it is waste: filler prose, noisy logs, stale context, bloated instruction files. Reach for this recipe when **cost** climbs faster than output.
1413

15-
> **Measure before you optimise.** You cannot cut what you do not see.
14+
## Steps to cut token usage
1615

17-
## Best tips
16+
### 🟢 Beginner
1817

19-
Ranked by impact-to-effort. Start at the top.
18+
#### 1) 📊 Measure first
2019

21-
#### 1) Measure first
20+
You cannot cut what you cannot see, so find what fills the window before changing anything.
2221

23-
You cannot improve what you do not track, and the bill is rarely where you think — cache reads usually dominate, not generation.
24-
25-
- In Claude Code: `/context` shows what fills the window, `/cost` shows the session cost and plan usage, `/insights` reports your session patterns and friction points.
26-
- For a per-prompt breakdown across every session, use [`prompt-analytics-for-claude-code`](https://github.com/romainfjgaspard/prompt-analytics-for-claude-code).
22+
- Built-in: run `/context`, `/cost`, and `/insights` in Claude Code.
23+
- Cross-session detail: install [`prompt-analytics-for-claude-code`](https://github.com/romainfjgaspard/prompt-analytics-for-claude-code) and run it with `uvx`.
2724

2825
```bash
2926
$ uvx --from prompt-analytics-for-claude-code prompt-analytics summary
30-
# per-prompt tokens & cost from your local logs — cache reads dominate the total
27+
# per-prompt tokens & cost from local logs — cache reads usually dominate
3128
```
3229

33-
#### 2) Trim your instruction file
30+
#### 2) ✂️ Trim your instruction file
3431

35-
`CLAUDE.md` (or `.github/copilot-instructions.md`) ships in **every** turn, so each wasted line is taxed on every message.
32+
Your instruction file ships every turn, so each cut line saves on every message.
3633

37-
- Keep it short; add explicit conciseness rules.
38-
- Drop-in ruleset: [`drona23/claude-token-efficient`](https://github.com/drona23/claude-token-efficient) (savings vary by model; measure your own).
34+
- It lives at `CLAUDE.md` (or `.github/copilot-instructions.md`).
35+
- Cut it to essentials and add explicit conciseness rules.
36+
- Reuse the [`claude-token-efficient`](https://github.com/drona23/claude-token-efficient) ruleset.
3937

4038
```md
4139
# CLAUDE.md
4240
- Terse answers. No preamble, no "Let me…", no closing summary.
4341
- Keep verbatim: code, quoted errors, security warnings. Cut the rest.
4442
```
4543

46-
#### 3) Make the agent talk less
44+
#### 3) 🗜️ Compact deliberately
45+
46+
Compacting on your terms keeps what matters instead of letting auto-compaction guess.
47+
48+
- Watch context use and act around 60–70%.
49+
- Run `/compact` with focus instructions naming what to keep.
50+
51+
```bash
52+
$ /compact keep the repro steps and the failing test; drop the file dumps
53+
```
54+
55+
### 🟡 Intermediate
56+
57+
#### 4) 🗣️ Make the agent talk less
4758

48-
Output is repetition — "Let me explain…", "Here's a summary…". You pay to generate every word.
59+
Output is repetition you pay to generate, so cap the chatter.
4960

50-
- The [`caveman`](https://github.com/JuliusBrussee/caveman) skill forces short, filler-free replies (reported ~65% output-token cut, code and data intact). Auto-detects 30+ agents.
61+
- Install the [`caveman`](https://github.com/JuliusBrussee/caveman) skill (auto-detects 30+ agents).
62+
- It forces short, filler-free replies (reported ~65% output cut, code intact).
5163

5264
```text
53-
before: "Great question! Let me walk you through each of the steps involved…"
65+
before: "Great question! Let me walk you through each step involved…"
5466
after: "3 steps:"
5567
```
5668

57-
#### 4) Filter noisy command output
69+
#### 5) 🧹 Filter noisy command output
5870

59-
Test runs, installs, and build logs flood the context with lines the model never needs.
71+
Test, install, and build logs flood context with lines the model never needs.
6072

61-
- A CLI proxy strips them before they land: [`RTK`](https://github.com/rtk-ai/rtk) (Rust) or [`SNIP`](https://github.com/edouard-claude/snip) (Go, YAML filters). Both report 60–90% on noisy output, zero overhead when no filter matches.
73+
- Install a CLI proxy: [`RTK`](https://github.com/rtk-ai/rtk) (Rust) or [`SNIP`](https://github.com/edouard-claude/snip) (Go, YAML filters).
74+
- Run noisy commands through it; it keeps only the signal (reported 60–90%).
6275

6376
```bash
6477
$ rtk proxy npm test
6578
# full build log in → only failures + final summary kept
6679
```
6780

68-
#### 5) Prefer CLI over MCP
81+
#### 6) 🔌 Prefer CLI over MCP
6982

70-
An MCP server loads its full tool schema into **every** turn; a CLI call costs a few tokens only when you use it.
83+
An MCP server loads its full schema every turn, while a CLI costs tokens only when called.
7184

72-
- Reach for the CLI when one exists; keep MCP for what truly needs it. See [`mcp-installation.md`](mcp-installation.md).
85+
- Use the CLI when one exists (`gh`, `acli`, …); keep MCP for what has none.
86+
- See [`mcp-installation.md`](mcp-installation.md).
7387

7488
```bash
75-
$ gh pr list # a few tokens per call
76-
# vs a GitHub MCP server: its whole schema rides along every turn
89+
$ gh pr list # a few tokens per call, vs a GitHub MCP schema riding along every turn
7790
```
7891

79-
#### 6) Use progressive disclosure
92+
### 🔴 Expert
8093

81-
Don't paste big procedural docs into context. Load knowledge only when the task needs it.
94+
#### 7) 📚 Use progressive disclosure
8295

83-
- Install an AIDD framework so skills, rules, and runbooks load on demand instead of riding along every turn.
96+
Load knowledge only when the task needs it, instead of riding along every turn.
97+
98+
- Install an AIDD framework so skills, rules, and runbooks load on demand.
8499

85100
```text
86101
skill description matched → its steps load
87102
no match → 0 tokens spent
88103
```
89104

90-
#### 7) Compact deliberately
91-
92-
Auto-compaction fires late and may drop what you wanted kept.
93-
94-
- Run `/compact` around 60–70% context, passing focus instructions for what to retain.
95-
96-
```bash
97-
$ /compact keep the repro steps and the failing test; drop the file dumps
98-
```
99-
100-
#### 8) Route by difficulty
105+
#### 8) 🎯 Route by difficulty
101106

102-
The top model on a routine task is wasted spend.
107+
The top model on routine work is wasted spend.
103108

104-
- Send research and routine work to a cheaper/faster model or a fresh subagent context; reserve the strongest model for the hard reasoning.
109+
- Send research and boilerplate to a cheaper model or a fresh subagent.
110+
- Reserve the strongest model for hard reasoning.
105111

106112
```text
107113
research / boilerplate → small model or subagent
108114
architecture / tricky bug → top model
109115
```
110116

111-
#### 9) Cap extended thinking
117+
#### 9) Cap extended thinking
112118

113-
Extended reasoning can silently add thousands of tokens per turn on tasks that don't need it.
119+
Extended reasoning can silently add thousands of tokens on tasks that don't need it.
114120

115-
- Control it with `MAX_THINKING_TOKENS` in your settings — set `0` to turn extended thinking off when you don't need it. See the [Claude Code settings docs](https://code.claude.com/docs/en/settings).
121+
- Set `MAX_THINKING_TOKENS` to `0` for routine work.
122+
- See the [Claude Code settings docs](https://code.claude.com/docs/en/settings).
116123

117124
```bash
118125
# settings.json (env)
@@ -121,9 +128,9 @@ Extended reasoning can silently add thousands of tokens per turn on tasks that d
121128

122129
## Verify
123130

124-
- Run `/cost` (or `prompt-analytics summary`) before and after a typical task — the token count should drop measurably.
131+
- Run `/cost` before and after a typical task — the token count drops measurably.
125132
- `/context` shows your instruction file and tool schemas taking a smaller share of the window.
126-
- A noisy command (test suite, `npm install`) routed through RTK or SNIP returns far fewer lines than the raw run.
133+
- A noisy command routed through RTK or SNIP returns far fewer lines than the raw run.
127134

128135
## Related
129136

0 commit comments

Comments
 (0)