# Optimizer Install
<!--
ref: @protocol/optimizer-install
tags: [optimizer, install, claude-code, hooks, efficiency]
triggers: [install optimizer, set up optimizer, track agent efficiency, optimizer collector]
version: 15
updated_at: 2026-11-11T01:32:28.496Z
files: [optimizer.mjs]
-->

Install the Optimizer collector — a SessionStart hook that pushes token counts (never text) from Claude Code transcripts to protocol.supply through a write-only scoped token, so efficiency can be tracked over time.

Installs `optimizer.mjs` (attached below) as a background push on every Claude Code session start. What it measures and how to read it: `@protocol/efficiency-measurement`.

**Needs:** Node 18+, `jq`, and `curly` already set up for protocol.supply (`@protocol/install`). **Works on:** macOS and Linux.

## What leaves the machine

Per session: its UUID, timestamps, model ids, Claude Code version, token counts, turn/tool/correction counts, three booleans (accepted, used protocol, interactive) and a project label (the git remote's repo name, else the folder name). Per push, the usage meter Claude Code last cached from `/usage`: the 5-hour and weekly percentages, their reset times and when they were read, never the account id. It draws the Fleet page's weekly budget chart. **Never** prompt text, assistant text, tool input or output, file paths, branches or commit subjects. Step 1 shows you the exact payload before anything is sent.

## Steps

**Agents:** do every step with the operator watching, and stop on any error. Never edit `~/.claude/settings.json` without showing the change first.

### 1. Fetch the collector and look before sending

```bash
mkdir -p ~/.claude/optimizer && chmod 700 ~/.claude/optimizer
curly protocol "@protocol/optimizer-install_/optimizer.mjs" > ~/.claude/optimizer/optimizer.mjs
node ~/.claude/optimizer/optimizer.mjs report --days 28          # local only
node ~/.claude/optimizer/optimizer.mjs rollup --days 1 | head -60  # the exact payload shape
```

*Why:* `report` and `rollup` make no network calls. You see your own numbers, and exactly what a push would carry, before deciding to send anything.

### 2. Mint a write-only token, straight into curly

```bash
TOK=$(curly protocol /token -sS -X POST -H 'Content-Type: application/json' \
  -d '{"name":"optimizer-collector","scopes":["write:optimizer"]}' | jq -r .secret) \
  && [ "${TOK#tok_}" != "$TOK" ] \
  && curly init protocol-optimizer "$TOK" host=https://api.protocol.supply auth=bearer >/dev/null \
  && echo "protocol-optimizer ready"; unset TOK
```

*Why scoped:* the hook runs unattended on every session start. A `write:optimizer` token can push collector data and nothing else — it cannot read your repos, edit entries, or mint another token — so a leaked hook credential is worth almost nothing. *Why one pipeline:* the secret goes from the mint response into curly's config without being printed, typed or pasted, so it never lands in a transcript or shell history.

### 3. Register and backfill

```bash
node ~/.claude/optimizer/optimizer.mjs push --service protocol-optimizer
```

It prints `pushed N session(s) … as collector ocol_…`. *Why now, by hand:* the first push sends the last 30 days, and Claude Code prunes transcripts after about 30 days, so this is your only chance at a "before" baseline. Running it by hand surfaces any error while you're watching, instead of in a silent hook log.

**Reinstalling?** Keep `~/.claude/optimizer/state.json`. It holds the collector's install key; delete it and the next push registers a second collector, which counts the same sessions twice.

### 4. Add the SessionStart hook

```bash
node ~/.claude/optimizer/optimizer.mjs install
jq '.hooks.SessionStart' ~/.claude/settings.json   # show the operator the added entry
```

It backs up `settings.json` to `settings.json.bak` and appends one hook, keeping your other SessionStart hooks. It prints the line it wrote:

```
PATH="<your PATH now>:$PATH" node ~/.claude/optimizer/optimizer.mjs push --every 30 >> ~/.claude/optimizer/push.log 2>&1
```

Running it again changes nothing. If you move Node, re-run it to refresh the PATH.

*Why each part:*
- **The pinned PATH.** A hook runs under `/bin/sh` without your login shell's PATH, so a session started from a launcher or a fleet pane can't find an nvm `node`, `curly`, or curly's `yq`. The push then never happens; the only sign is `node: command not found` in `push.log`. `install` pins the PATH it ran under, where you just proved curly works in step 3.
- **`async: true`.** The push runs beside the session and never delays startup.
- **`--every 30`.** The push is skipped when the last one was under 30 minutes ago, so a burst of session starts costs one scan, not twenty. A lock file also makes simultaneous starts skip rather than queue.
- **The redirect.** A SessionStart hook's stdout is added to the model's context. Sending output to a log keeps the push out of every session.

**This step is the install. Steps 1 to 3 only send data once.** A push by hand registers the machine and puts it on the Fleet page, and then nothing pushes again. This went unseen for a day on 2026-11-03. Now a push by hand prints `WARNING, this machine is not an installed collector` when any of these is true:
- there is no hook;
- the hook runs a bare `node`;
- the push is running from a copy other than the installed one;
- the push is using the full-access `protocol` token.

**Re-pushing or backfilling by hand?** Run `~/.claude/optimizer/optimizer.mjs` (never a repo copy) with `--service protocol-optimizer`, and act on any warning it prints.

### 5. Verify

```bash
claude -p "Reply ok." < /dev/null; sleep 20; tail -1 ~/.claude/optimizer/push.log
curly protocol "/optimizer/collector"
```

The log's last line should be a fresh `pushed N session(s)`, or `last push Nm ago; skipped (--every 30)` if the last push was under 30 minutes ago. Either line proves the hook fired.

If you see `command not found`, re-run step 4 from a shell where `curly protocol /ls` works. The collector list should show this machine. *Why:* the hook runs out of sight, so prove it fired once rather than assuming it did.

## Reading results

```bash
curly protocol "/optimizer/metric?from=<ISO>&to=<ISO>&cut=<ISO>"   # server, all your machines
node ~/.claude/optimizer/optimizer.mjs report --from <ISO> --to <ISO> --cut <ISO> --json   # local, this machine
```

For the same window and cut, the two agree field for field on a single machine. How to choose the cut and read the verdict: `@protocol/efficiency-measurement`.

## Uninstall

1. Remove the hook entry from `~/.claude/settings.json`. *Why first:* it stops new pushes.
2. Revoke the token: `curly protocol /token` to find the `optimizer-collector` id, then `curly protocol /token/<id>/revoke -X POST`. *Why:* a revoked credential is safe even if a copy survives somewhere.
3. Optionally, `rm -r ~/.claude/optimizer`.
4. Retire the machine so the Optimizer stops warning that it has gone quiet: Settings › Collectors › Retire, or `curly protocol /optimizer/collector/<ocol_id>/retire -X POST` (the id is in `curly protocol /optimizer/collector`). *Why retire, not delete:* its sessions stay in your history and keep counting; only the stale warnings stop. A push from it un-retires it, and `/unretire` undoes it by hand.

## What the push carries besides counts

Since #309 slice C each session also carries its **title** (Claude Code's own generated ai-title, a short summary, never prompt text) and its **lane** (the tmux / agent-deck session name). Both go only to the owner's own account and are on by default. Pass `--no-titles` to `push` (and `rollup`) to send neither; the keys are then absent from the payload.

---

*Related: `@content/web/xinyadu/2026-11-29-claude-code-task-framing-study` · `@content/web/github/2026-11-27-resume-command-osc-7577-issue-112408` · `@content/web/github/2026-11-26-opus-context-fills-faster-issue-112194` · `@content/web/github/2026-11-26-interactive-session-misclassified-background-issue-112133` · `@content/web/github/2026-11-26-autodream-silently-ignored-issue-112310`*

---

*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*

*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*