Calculates token totals and metrics for AI session usage snapshots.
// Source: https://github.com/stablyai/orca | License: MIT | Copyright 2026 Lovecast Inc
// Provided via justclaw.it.com; keep this notice with the code.
import type { CodexUsageSnapshot } from './session-scanner-types'
import { asRecord } from './session-scanner-record-value'
export function tokenTotal(value: unknown): number {
const usage = asRecord(value)
if (!usage) {
return 0
}
const explicitTotal =
numberValue(usage.total) || numberValue(usage.totalTokens) || numberValue(usage.total_tokens)
if (explicitTotal > 0) {
return explicitTotal
}
const fields: unknown[] = [
usage.input,
usage.inputTokens,
usage.input_tokens,
usage.output,
usage.outputTokens,
usage.output_tokens,
usage.cacheRead,
usage.cacheReadTokens,
usage.cache
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key