Skip to content

feat(memory): implement token usage tracking (DC-MEM-005)#513

Merged
radoxtech merged 2 commits intomainfrom
feat/token-usage-tracking-#94
Apr 2, 2026
Merged

feat(memory): implement token usage tracking (DC-MEM-005)#513
radoxtech merged 2 commits intomainfrom
feat/token-usage-tracking-#94

Conversation

@radoxtech
Copy link
Copy Markdown
Owner

Summary

  • Add token_usage table (migration 008) with session_id FK to sessions, turn_id linkage, agent_id, model, provider, tokens_in/out, cost_usd, and timestamp
  • Implement TokenUsageRepository with write path (record), per-session/agent/model aggregation queries, time-range filtering, and budget threshold check (checkBudget)
  • Add Zod schemas for TokenUsage, RecordTokenUsageInput, SessionUsageSummary, ModelUsageBreakdown, and AgentUsageSummary types
  • Export all new types and repository from @diricode/memory

Schema

CREATE TABLE token_usage (
  id TEXT PRIMARY KEY,
  session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
  turn_id TEXT,
  agent_id TEXT,
  model TEXT NOT NULL,
  provider TEXT,
  tokens_in INTEGER NOT NULL DEFAULT 0,
  tokens_out INTEGER NOT NULL DEFAULT 0,
  cost_usd REAL NOT NULL DEFAULT 0.0,
  timestamp TEXT NOT NULL DEFAULT (datetime('now'))
);
-- Indexes: (session_id, timestamp), (turn_id), (model), (agent_id)

API Contracts

Provides stable interfaces for:

  • DC-SAFE-004 (cost guardrails): checkBudget(sessionId, threshold) for live budget enforcement
  • DC-OBS-* (observability): getSessionTotals, getAgentTotals, getModelBreakdown for metrics

Closes #94

@radoxtech radoxtech force-pushed the feat/token-usage-tracking-#94 branch from 28eff2f to 8aa4c06 Compare April 2, 2026 16:40
@radoxtech radoxtech merged commit 7bb4c6f into main Apr 2, 2026
1 check passed
@radoxtech radoxtech deleted the feat/token-usage-tracking-#94 branch April 3, 2026 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DC-MEM-005: Token usage tracking [MVP-1]

1 participant