Decisions

  • Pending: Where to get token usage data (Claude Code logs, API headers, manual estimates)?
  • Pending: Storage format (SQLite, CSV, markdown)?

User Tasks


FR-053: Cost & Token Tracking

Summary

Track token usage and API costs per session, skill, and agent to understand spending patterns and optimize expensive operations.

Problem / Motivation

Opus will increasingly use agents, scheduled tasks, and automated workflows — all consuming tokens. Without visibility into costs:

  • No way to know which skills/agents are expensive
  • Can’t detect cost spikes from loops or inefficient prompts
  • Can’t budget or forecast monthly spend
  • No data to justify optimization efforts (FR-009 Token Efficiency)

As autonomous features come online (briefings, monitoring, self-review), costs could grow unnoticed.

Proposed Solution

Lightweight tracking system that logs token usage per session and rolls it up into a dashboard.

Data Model

FieldDescription
timestampWhen the session/action occurred
session_idUnique session identifier
action_typeskill / agent / manual / scheduled
action_namee.g., /briefing, researcher-agent
input_tokensTokens sent
output_tokensTokens received
modelModel used (opus, sonnet, haiku)
estimated_costCalculated from token counts + pricing

Open Questions

1. Data Source

Question: How to capture token usage data?

OptionDescription
A) Parse Claude Code session logsPost-session extraction from ~/.claude/ logs
B) PostToolUse hook with estimationEstimate tokens per tool call (imprecise)
C) API-level trackingOnly works for programmatic API calls, not Claude Code CLI

Recommendation: Option A — Claude Code already logs usage. Parse after session ends.


Phase Overview

PhaseDescriptionStatus
Phase 1Manual tracking script + cost table
Phase 2Automated post-session logging
Phase 3Dashboard + alerting

Phase 1: Manual Tracking —

Goal: Script to extract token usage from recent sessions.

File / FeatureDetailsOwnerStatus
src/tracking/cost_tracker.pyParse Claude Code logs for token countsopus
Pricing tableModel → cost-per-token mappingopus
vault/00_system/dashboards/cost-dashboard.mdMarkdown dashboard with weekly/monthly totalsopus

Phase 2: Automated Logging —

Goal: Auto-log after every session.

File / FeatureDetailsOwnerStatus
Post-session hook or cronTrigger cost extraction after sessionopus
SQLite storagePersistent cost data in data/opus.dbopus
Per-skill breakdownAttribute costs to specific skills/agentsopus

Test

Manual tests

TestExpectedActualLast
Verify per-model pricing is correctMatches current Anthropic pricingpending-

AI-verified tests

ScenarioExpected behaviorVerification method

E2E tests

ScenarioAssertion

Integration tests

ComponentCoverage

Unit tests

ComponentTestsCoverage

History

DateEventDetails
2026-03-12CreatedIdentified as gap — no visibility into token costs

References

  • FR-009 (Token Efficiency Guidelines) — tracking validates whether efficiency measures work
  • FR-014 (MCP Server Configuration) — sqlite MCP would store cost data
  • FR-025 (Daily Briefing System) — could include daily cost summary