Xpersona
L

LLM Cost Tracker

CLAWHUB · Unverified

Track OpenClaw LLM token usage and cost from OpenRouter API. Reports: last 24h, 7d, 30d, 90d, 365d with model breakdown. Skills that trigger this: - "llm cos...

62
Rank
84
Safety
964
Downloads

About

What it does, and when to use it.

Capability contract not published. No trust telemetry is available yet. 964 downloads reported by the source. Last updated 10/12/2026.

Avoid when

  • Contract metadata is missing or unavailable for deterministic execution.

Risk flags: missing_or_unavailable_contract, trust_data_unavailable, schema_references_missing

Install and run

Setup complexity: low.

clawhub skill install s17av7k4d5aq1pr3yswt5phdg985p5pm:llm-cost-tracker-openrouter-api
  1. Install using `clawhub skill install s17av7k4d5aq1pr3yswt5phdg985p5pm:llm-cost-tracker-openrouter-api` in an isolated environment before connecting it to live workloads.
  2. No published capability contract is available yet, so validate auth and request/response behavior manually.
  3. Review the upstream CLAWHUB listing at https://clawhub.ai/joeyiptk/llm-cost-tracker-openrouter-api before using production credentials.

Contract: missing

curl -s "https://www.xpersona.co/api/v1/agents/clawhub-joeyiptk-llm-cost-tracker-openrouter-api/snapshot"

Run-check

$0.02 USD

1 measured facts are behind this paywall: success rate and latency, uptime and estimated cost, when not to use it, how to call it, benchmark scores.

Agents pay $0.02 in USDC. A card payment is $0.50, the smallest a card allows.

Documentation

CLAWHUB

40,890 characters of source documentation, loaded on request.

Extracted files

5 files captured from the source.

SKILL.md

---
name: llm-cost-tracker
version: 1.1.2
description: >
  Track OpenClaw LLM token usage and cost from OpenRouter API. Reports: last 24h, 7d, 30d, 90d, 365d with model breakdown. Skills that trigger this: - "llm cost", "token usage", "openrouter cost", "llm spend", "daily cost report" → runs run_tracker.py - "collect usage data" → runs collect_usage.py (populates DB silently) When "collect usage data" is received (e.g. from a cron job), run: python3 scripts/collect_usage.py and return "Done" — do NOT send any message to Telegram or any chat.
read_when: >
  - Running a cost/token usage report - Scheduling a daily/weekly cost report - Setting up on a new machine
---
# llm-cost-tracker

Track and report LLM token usage and cost for OpenClaw via OpenRouter.

## Quick Start

```bash
python3 scripts/collect_usage.py --init      # first-time: create DB + backfill
python3 scripts/run_tracker.py               # telegram report (default)
python3 scripts/run_tracker.py --output terminal  # full terminal report
python3 scripts/run_tracker.py --output debug     # per-request debug
```

## Core Design

- **Source of truth:** `usage.cost.total` from OpenRouter — never recomputed from token counts
- **Data source:** OpenClaw session JSONL files (including `.reset` files from context compaction)
- **Deduplication:** by `openrouter_request_id` — safe to re-run backfill anytime
- **Time windows:** 24h rolling (UTC); 7d/30d/90d/365d calendar days (configurable timezone)

## Schema: `request_facts`

| Column | Source |
|--------|--------|
| openrouter_request_id | responseId (unique key) |
| created_at_utc | entry timestamp |
| model | model ID |
| prompt_tokens | usage.input |
| completion_tokens | usage.output |
| cached_tokens | usage.cacheRead |
| cache_write_tokens | usage.cacheWrite |
| reasoning_tokens | usage.reasoning |
| total_tokens | usage.totalTokens |
| billed_cost | usage.cost.total (canonical) |

## Configuration

`config/env.json` (all fields optional):

```json
{
  "SESSIONS_DIR": "",
  "TIMEZONE": "Asia/Hong_Kong",
  "UTC_OFFSET_HOURS": 8
}
```

- `SESSIONS_DIR`: override session file location (empty = auto-detect)
- `TIMEZONE` / `UTC_OFFSET_HOURS`: for calendar-day window calculations
- API key: auto-detected from `~/.openclaw/agents/main/agent/auth-profiles.json` or `OPENROUTER_API_KEY` env var

## Scheduled Reports

```bash
openclaw cron add --name "llm-cost:collect" --message "collect usage data" \
  --cron "5 0 * * *" --tz "Asia/Hong_Kong" --session isolated --no-deliver

openclaw cron add --name "llm-cost:daily" --message "llm cost" \
  --cron "0 9 * * *" --tz "Asia/Hong_Kong" --session isolated
```

_meta.json

{
  "ownerId": "kn7a3t7cye5xwvkpam99tawre585q0zr",
  "slug": "llm-cost-tracker-openrouter-api",
  "version": "1.1.2",
  "publishedAt": 1779984337744
}

CHANGELOG.md

# Changelog

All notable changes to this skill are documented here.

## 1.1.2 (2026-05-28)
- Added `--days` flag to `collect_usage.py` to allow multi-day collection.
- Fixed HKT/UTC logic gap by using a 48-hour window in scheduled runs.
- Improved collection reliability for rolling 24h reports.
- Excluded local `usage.db` from snapshots (C7 fix).

## 1.1.1
- Added: scripts/setup.py
- Updated: SKILL.md, version.txt

## 1.1.0 (2026-04-30)

### Changed
- Clarified billing rules: use `usage.cost.total` as canonical source, not token-count math
- Split token categories: prompt, cached (cacheRead), cache_write, completion, reasoning, total
- Updated cron examples in SKILL.md with `--no-deliver` flag for collect cron
- Removed "forward output exactly" requirement from report triggers

### Added
- 90-day time window in addition to 24h/7d/30d/365d
- `/365d` trend line in Telegram report format

## 1.0.0 (2026-04-05)

- Initial release
- Track and report LLM token usage and cost via OpenRouter
- Support for 24h rolling and 7d/30d/365d calendar windows
- SQLite append-only DB with deduplication

SETUP_GUIDE.md

# llm-cost-tracker — Setup Guide

## First-Time Setup

```bash
cd <path-to-llm-cost-tracker>
python3 scripts/collect_usage.py --init
```

This creates the DB, backfills all session files (including reset files), and runs a health check.

## Scripts

| Script | Purpose |
|--------|---------|
| `collect_usage.py` | Scan session JSONL files → populate DB |
| `run_tracker.py` | Generate reports, health check, debug |
| `prune_usage.py` | Delete old rows with dry-run support |

### collect_usage.py

```bash
python3 scripts/collect_usage.py --init          # first-run wizard
python3 scripts/collect_usage.py --backfill      # re-scan all sessions
python3 scripts/collect_usage.py                  # collect today only
python3 scripts/collect_usage.py --date 2026-04-24
python3 scripts/collect_usage.py --verify         # DB health check
```

### run_tracker.py

```bash
python3 scripts/run_tracker.py                    # telegram (default)
python3 scripts/run_tracker.py --output terminal  # full terminal report
python3 scripts/run_tracker.py --output json      # JSON for scripting
python3 scripts/run_tracker.py --output debug     # per-request debug
python3 scripts/run_tracker.py --health           # quick health check
```

### prune_usage.py

```bash
python3 scripts/prune_usage.py 2024-04-25 --dry-run   # preview
python3 scripts/prune_usage.py 2024-04-25 --vacuum     # delete + reclaim
```

## Configuration

Edit `config/env.json` (all fields optional):

```json
{
  "SESSIONS_DIR": "",
  "TIMEZONE": "Asia/Hong_Kong",
  "UTC_OFFSET_HOURS": 8
}
```

- **SESSIONS_DIR**: Override if auto-detection fails. Leave empty for auto-detect.
- **TIMEZONE / UTC_OFFSET_HOURS**: For calendar-day windows (default: HKT +8).
- **API key**: Auto-detected from `~/.openclaw/agents/main/agent/auth-profiles.json` or `OPENROUTER_API_KEY` env var. Not stored in env.json.

### Auto-detected paths

Sessions directory search order:
1. `SESSIONS_DIR` from env.json
2. `~/.openclaw/agents/main/sessions`
3. `/data/.openclaw/agents/main/sessions`
4. `~/Library/Application Support/openclaw/agents/main/sessions`

## Cron Setup

```bash
# Midnight: collect data (silent)
openclaw cron add \
  --name "llm-cost:collect" \
  --message "collect usage data" \
  --cron "5 0 * * *" \
  --tz "Asia/Hong_Kong" \
  --session isolated \
  --no-deliver

# 9 AM: daily report to Telegram
openclaw cron add \
  --name "llm-cost:daily" \
  --message "llm cost" \
  --cron "0 9 * * *" \
  --tz "Asia/Hong_Kong" \
  --session isolated
```

Verify: `openclaw cron list`

## Troubleshooting

**Sessions directory not found** — Set `SESSIONS_DIR` in `config/env.json`.

**DB is empty** — Run `python3 scripts/collect_usage.py --init`.

**Cost seems wrong** — Run `--output debug` to inspect per-request costs. Compare with `--health` which shows API key total vs DB total.

**Migrating to a new machine** — Copy the whole folder, then run `--init` again. Cron jobs must be re-created.

## File Structure

```
llm-cost-tracker/
├

SETUP.md

# Setup — llm-cost-tracker

## Requirements
- Python 3.7+
- `tabulate>=0.9.0`

## Install
```bash
pip install tabulate>=0.9.0
```

## First-time setup
```bash
cd skills/llm-cost-tracker
python3 scripts/collect_usage.py --init
```

## Configuration (optional)
Create `config/env.json` to override defaults:
```json
{
  "SESSIONS_DIR": "",
  "TIMEZONE": "Asia/Hong_Kong",
  "UTC_OFFSET_HOURS": 8
}
```

## Cron jobs
```bash
# Midnight: collect usage data (silent)
openclaw cron add --name "llm-cost:collect" --message "collect usage data" \
  --cron "5 0 * * *" --tz "Asia/Hong_Kong" --session isolated --no-deliver

# 9 AM: daily report to Telegram
openclaw cron add --name "llm-cost:daily" --message "llm cost" \
  --cron "0 9 * * *" --tz "Asia/Hong_Kong" --session isolated
```

Sponsored

Ads related to LLM Cost Tracker and adjacent AI workflows.