---
slug: "yusukeshib-pi-token-counter"
source_type: "readme"
source_url: "https://cdn.jsdelivr.net/gh/yusukeshib/pi-token-counter@main/README.md"
repo: "https://github.com/yusukeshib/pi-token-counter"
source_file: "README.md"
branch: "main"
---
# @yusukeshib/pi-token-counter

A [pi](https://pi.dev) extension that appends each tool's **own output token
cost** to its tool-use row, color-coded by size. This is the tool's own
contribution to the context (its result content) — not the cumulative total.

## What it looks like

![pi-token-counter in action](https://github.com/yusukeshib/pi-token-counter/raw/HEAD/assets/preview.png)

Each tool-use row gains a trailing token count, colored by how much it adds:

```text
read src/main.rs                 +312 tok
$ npm test -- --watch=false      +1,840 tok
grep "TODO" src                  +6,204 tok
```

| Color             | Meaning                          | Threshold       |
| ----------------- | -------------------------------- | --------------- |
| blue (accent)     | small output                     | `< 1,000` tok   |
| yellow (warning)  | medium output                    | `< 5,000` tok   |
| red (error)       | large output                     | `>= 5,000` tok  |

The token estimate uses the same heuristic as pi's built-in estimator
(text characters / 4).

## Install

```bash
pi install npm:@yusukeshib/pi-token-counter
```

Via git (no npm publish required):

```bash
pi install git:github.com/yusukeshib/pi-token-counter
```

Try it without installing:

```bash
pi -e npm:@yusukeshib/pi-token-counter
```

## How it works

The `tool_result` event carries each tool's result content, so the extension
estimates its tokens there and caches the value by `toolCallId`. It overrides
only `renderCall` for the built-in tools (read / bash / edit / write / grep /
find / ls) to append the colored token count — `renderResult` is left untouched
so pi's built-in result rendering (diffs, syntax highlighting, etc.) is fully
preserved. When a result arrives the row re-renders and `renderCall` reads the
cached estimate.

## License

MIT
