tokenmeter Architecture

AI 编码 Agent 实时可观测性管道 · Claude Code + Codex → SQLite → TUI / Web

hook stdin tail token usage poll 2s events + tokens live persist live query query HTTP Claude Code CLI · 8 hooks Pre/PostToolUse, Stop… Claude JSONL session logs ~/.claude/projects/ Codex JSONL session logs ~/.codex/sessions/ tokenmeter emit hook CLI stdin → unix sock ClaudeLogWatcher goroutine · in daemon INSERT OR IGNORE CodexWatcher goroutine · poll 2s in-memory dedup Unix socket ~/.tokenmeter/tokenmeter.sock tokenmeter daemon Go 1.24 · single binary ▸ Socket listener net.Listen · JSON frames ▸ Event router Pre ↔ Post correlator via tool_use_id ▸ Cost estimator pricing.go · per-model Claude + Codex rates ▸ SQLite writer source_id dedup ▸ Broadcast hub subscribers: TUI, web 2s heartbeat tick ▸ Session lifecycle SessionEnd → interrupt stale GC (2h cutoff) single-instance PID lock ~/.tokenmeter/daemon.pid auto-spawn on `tokenmeter` internal/daemon · internal/event internal/collector tokenmeter TUI bubbletea · 4 tabs Dashboard · Messages Tool Calls · Timeline j/k · Enter · / · [ ] SQLite modernc · pure Go ~/.tokenmeter/data/tokenmeter.db • sessions • agents (parent_agent_id) • tool_calls • token_usage • file_changes idempotent migrations tokenmeter web HTTP Dashboard REST API + embedded SPA cost reports · daily spend auto-starts daemon + watchers Browser localhost:port dark / light mode Legend Source Process / goroutine Persistent store UI consumer Transport live broadcast SQLite query

Event Pipeline

  • • Two ingest paths: unix socket for Claude hooks, file tail for JSONL logs
  • • Unified Event at internal/event — Pre/Post paired by tool_use_id
  • • Cost estimated per-model via pricing.go (Claude + Codex rate tables)
  • INSERT OR IGNORE + source_id unique index makes watcher restarts idempotent

SQLite Schema

  • sessions — platform, tokens, cost, status
  • agentsparent_agent_id tracks subagent hierarchy
  • tool_calls — params, result, duration, outcome
  • token_usage — per-model token & cost rows
  • file_changes — path + change type per session

UI Consumers

  • TUI — bubbletea 4 tabs, 2s poll + live broadcast; keys: j k Enter / [ ]
  • Web — HTTP server reads SQLite, serves embedded SPA with daily cost reports
  • • Both receive the daemon's live event stream — TUI updates instantly, web refreshes via fetch
  • tokenmeter web auto-spawns daemon + watchers on start (no manual daemon needed)