← Back to Plugins
Tools

Token Meter

mysqto By mysqto 👁 133 views ▲ 0 votes

OpenClaw plugin that meters LLM token usage per turn to a local SQLite, with a small CLI + skill for cost summaries.

GitHub

Install

npm install
npm

Configuration Example

[
  {
    "provider": "anthropic",
    "prefix": "claude-opus-4-7",
    "input": 15,
    "output": 75,
    "cacheRead": 1.5,
    "cacheWrite": 18.75
  }
]

README

# openclaw-token-meter

An OpenClaw plugin that logs one row per LLM turn into a local SQLite file
and estimates USD cost from a bundled price table. Ships with a small `token-meter`
CLI to summarise usage by model, agent, session, provider, or arbitrary SQL.

Nothing gets sent off the machine. Only usage metadata is stored (token counts,
model id, timestamps, context budget, reasoning level). **No prompts or responses
are recorded.**

## Install

```bash
git clone https://github.com/mysqto/openclaw-token-meter.git
cd openclaw-token-meter
npm install
npm run plugin:build
openclaw plugins install .
openclaw gateway restart
```

Verify:

```bash
openclaw plugins list | grep token-meter
```

The plugin reacts to the `llm_output` hook, so the first row appears after the
gateway serves at least one model turn.

## Query

The CLI lives at `bin/token-meter.mjs` and is registered under the `token-meter`
bin name in `package.json`.

```bash
# 7-day cost + tokens by model
token-meter summary --days 7 --group-by model

# today, grouped by agent
token-meter summary --days 1 --group-by agent_id

# last 20 turns
token-meter recent --limit 20

# where is the database?
token-meter path
```

You can also query the SQLite file directly:

```bash
sqlite3 "$(token-meter path)"
```

## Schema

| Column               | Type    | Notes                                       |
| -------------------- | ------- | ------------------------------------------- |
| `ts`                 | INTEGER | Unix milliseconds when the row was written  |
| `session_id`         | TEXT    | Harness session id                          |
| `run_id`             | TEXT    | Harness run id                              |
| `agent_id`           | TEXT    | Parsed from session id when possible        |
| `provider`           | TEXT    | anthropic / openai / google / ...           |
| `model`              | TEXT    | Model id as reported by the harness         |
| `resolved_ref`       | TEXT    | `<provider>/<model>` after resolution       |
| `harness_id`         | TEXT    | Backend responsible for the model loop      |
| `input_tokens`       | INTEGER | From `event.usage.input`                    |
| `output_tokens`      | INTEGER | From `event.usage.output`                   |
| `cache_read_tokens`  | INTEGER | From `event.usage.cacheRead`                |
| `cache_write_tokens` | INTEGER | From `event.usage.cacheWrite`               |
| `total_tokens`       | INTEGER | From `event.usage.total`                    |
| `context_budget`     | INTEGER | Effective context cap for the call          |
| `reasoning_effort`   | TEXT    | `off` / `low` / `medium` / `high` / etc.    |
| `fast_mode`          | INTEGER | 1 if fast mode was on                       |
| `cost_usd_est`       | REAL    | Estimate; see caveats                       |

Indices on `ts`, `(model, ts)`, `(session_id, ts)`, `(agent_id, ts)`.

## Config

The plugin manifest declares three optional config fields:

- `dbPath` — override the SQLite location.
- `priceOverridesPath` — JSON file with an array of `ModelPrice` rows to prepend
  to the bundled table.
- `enabled` — set false to keep the plugin installed but silent.

Configured through your OpenClaw plugin config surface (see
`openclaw plugins inspect token-meter`).

## Price table

`src/pricing.ts` contains a small hand-curated price table for common Anthropic,
OpenAI, and Google models. Rates are USD per million tokens. **These will drift**
as vendors update rate cards. Point `priceOverridesPath` at your own JSON to
patch without re-installing.

Shape:

```json
[
  {
    "provider": "anthropic",
    "prefix": "claude-opus-4-7",
    "input": 15,
    "output": 75,
    "cacheRead": 1.5,
    "cacheWrite": 18.75
  }
]
```

Longest prefix wins per `(provider, model)`.

## Skill

The `skill/SKILL.md` file is designed for OpenClaw skill loaders. Copy it into
your OpenClaw skills directory to give assistants a canonical recipe for
answering "how much have I spent" questions using the CLI.

## Caveats

- Estimates only. Provider dashboards are authoritative.
- No historical backfill — rows start from install time.
- If a provider adapter does not populate `usage`, the row is written with
  null token fields; you still get the timestamp and model.

## License

MIT.
tools

Comments

Sign in to leave a comment

Loading comments...