turnlens
AGENTS.md4 starsChanged 4 months agoMITView on GitHub
Per-turn token and cost monitoring for AI coding agents. It answers one question that agent session files do not answer directly: what did this single prompt and its response cost? TypeScript, ESM, zero runtime dependencies. This file holds what is worth knowing in every task; everything else is routed to below. lint runs itself: a Stop hook in .claude/settings.json blocks the end of any turn that wrote a .ts file and left a lint error. The user-facing surface is turnlens…
- Installs packages
In this repository4 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- design-docSkill
- live-verificationSkill
- typescript-best-practicesSkill
The file
We split this AGENTS.md into the rules it gives an agent
5 rules. About 0 are common, 4 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 37
Never write a claim about this code from memory: Not a return type, not a column count, not what a test file covers, not a line number.
- Distinctive: line 58
Before a factual sentence about the codebase, run the read.
- Distinctive: line 59
Before an expected output, derive it from the decision it belongs to, or run the command and paste what came back.
- Distinctive: line 61
When a document is finished, sweep it as a cold reader would: every path, symbol, line number and figure resolved against the repository, …
- Very specific: line 68
The home directory is resolved by
core/home.ts: Do not call os.homedir() anywhere else.
≈ “Common” is an estimate: at least 1 in 5 of the 16,597 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
TurnLens
Per-turn token and cost monitoring for AI coding agents. It answers one question that agent session files do not answer directly: what did this single prompt and its response cost?
TypeScript, ESM, zero runtime dependencies. This file holds what is worth knowing in every task; everything else is routed to below.
Commands
npm test vitest
npm run typecheck includes test/
npm run lint eslint, type-aware, src and test
npm run check the three above, in order
npm run build tsc, cleans dist/ first
npm run pricing:snapshot regenerates src/pricing/snapshot.generated.ts
npm run verify:costs cross-checks pricing against ccusagelint runs itself: a Stop hook in .claude/settings.json blocks the end of
any turn that wrote a .ts file and left a lint error.
The user-facing surface is turnlens --help. It is not copied here, because a
copy is right until the next flag is added.
Skills
live-verification: checking TurnLens against live agent data, or taking any figure that will be treated as evidence.design-doc: designing work that this session will also implement.typescript-best-practices: writing, reviewing or refactoring.ts.
Open the file
Never write a claim about this code from memory. Not a return type, not a column count, not what a test file covers, not a line number. Open it. This costs seconds and the alternative has already cost a working day.
Measured on one design document, 11 August 2026: roughly half its defects were
sentences written without opening the source. formatCostUsd was described as
producing a number when it returns a toFixed(6) string. The CSV was called 24
columns when CSV_HEADER held 23 that day, which is the sort of figure that
rots: it is 24 now, and the point is that neither number was ever counted.
test/history.test.ts was named as covering readRecordedKeys on the strength
of its filename alone, when it tests formatHistoryBlock and never opens a CSV.
A green test run is not evidence that a document is right. The worst of those
defects put a new CSV column mid-schema instead of last, which would have
re-mapped every existing row's duration_ms into it. Nothing would have failed:
readRecordedKeys reads only turn_id, status and timestamp, all of them
before the insertion point. The whole suite would have passed while every user's
spreadsheet read a duration as a cost breakdown.
Three habits follow, and they apply to plans and documents as much as to code.
- Before a factual sentence about the codebase, run the read.
- Before an expected output, derive it from the decision it belongs to, or run the command and paste what came back.
- When a document is finished, sweep it as a cold reader would: every path, symbol, line number and figure resolved against the repository, and every section checked against the ones it could contradict. Do this before handing it to a reviewer, not after.
Always
- The home directory is resolved by
core/home.ts. Do not callos.homedir()anywhere else. Two modules resolving it separately is two homes in one program. - An unpriced turn is empty, never
0.cost_statussays why. A zero joins a spreadsheet sum and cannot be told from a genuinely free turn. - A closed turn is never repriced.
pricing_versionis a column for this reason; the rate that paid for a row travels on that row. - A report dates a turn by
startedAt, its prompt; everything else usesat, its close. The CSV, the live table andlastActivityanswer "when did this finish". A report answers "what did I spend that day", and under the closing timestamp that day depended on how long the agent thought.atis the fallback when no start was seen.#resetTurnStatemust keep clearing#startedAt:modeldeliberately persists across a boundary and a start must not. - Nothing is fetched while a session is being watched, or while a report is
being built. Pricing resolves once, before either loop, and
lookupis synchronous. Adding anawaitto that path breaks a guarantee no test covers, and a report replays every transcript on the machine through it. - The update check shares that one moment and adds no other. It is issued
with
Promise.allbeside the pricing request incli.ts, so it costs the slower of the two rather than the sum. It must never become a background task:update-notifierspawns an unref'ed child that outlives the process, which is precisely the fetch-while-watching this codebase forbids. Every failure on that path is silence, and a version from the registry is checked against^\d+\.\d+\.\d+$before it reaches a terminal. - A token count of zero is printed; a cost that could not be computed is
blank. The two look alike and mean opposite things. A count is never unknown,
so blanking a zero would make a known figure look unknowable; a cost of
0would read as free. - Long-context, priority, flex, batch and regional rates are deliberately unused. They look like an omission and are not.
message.usage.iterationsis not summed. The record's own usage already contains ordinarymessageentries.advisor_messageis the one measured additional kind and is read separately with its own model.replaySessionincore/replay.tsis the batch path: one file in, priced turns out, and nowhere for them to go. The watcher, the report andimportHistoryall sit on it. Anything that reads a whole transcript belongs there rather than beside it.importHistoryis still deliberately unreachable from the CLI. A--import-historyflag was considered and rejected. What it owns now is only the CSV: deduping and appending. It stopped renumbering when a row's number became the turn's place in the transcript.- The CSV is written, not read. A row's number is the turn's position in the
session and the exit summary is folded from the session's turns, so neither
depends on a file that lands wherever the command was run.
readRecordedKeysis the one read left andimportHistoryis its only caller, because merging a replay into an existing file means knowing what that file holds. Adding a second read puts program state back into an output. - Display width is deliberately counted in code units. A half-built Unicode table that is wrong in a new way is worse than an honest note.
- Colour is a
Paintpassed in, applied after fitting, and never nested. A renderer never asks whether colour is on: the default isPLAIN, which is why every width test measures what a reader sees. An escape lengthens a string without occupying a column, so painting a value on its way intofitmakes the cell short by its own escapes. And escapes do not nest, so cells and rules are painted separately: an inner reset ends the outer colour and the rest of the row comes out plain. Stripping every escape must leave the output unchanged. - No lifecycle script is ever declared in
package.json. Notprepare, notpostinstall, notinstall. None of them would run for someone installing TurnLens, but declaring one is enough to make npm warn every installer that this package runs code at install time, and npm's remedy for that warning errors on a global install. This is the whole reason there is no pre-commit hook: development tooling lives in.claude/and.github/, which npm never reads.npm install turnlensprints one line, and that is a property to check, not to assume. - A change is not finished until the document its
update-whennames is edited, where such a document exists. See below.
Documents
If a docs/ directory is present, read docs/README.md first. It indexes
the working documents and says which to open for which task: one per pipeline
area, plus the architecture, the decisions, the measurements and the roadmap.
That directory is not part of this repository. It is gitignored and stays on the machines that develop TurnLens, so a clone will not have it, and this section is written to be true either way. Working from a clone, the code and its comments are the whole record: read those, and do not conclude a decision was never made merely because you cannot find a file recording it.
A document tells you where to look and what was already decided. Before changing code, stating what is true now, or refusing a change because it was decided against, open the source it names. A record knows why a decision was made; it does not know what would break today.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

