Persist short-lived usage facts for /usage and local diagnostics. The data answers:
Usage observability is not a permanent billing ledger. Rows are retained for 30 days and may be pruned on startup or after writes.
model_usage is the canonical token source. One row represents one logical model request recorded
at the runtime boundary, including main turns, compact summaries, title generation, target
completion verification, workflow child sessions, and subagents.
message and part may keep token snapshots for transcript rendering, but /usage must not read
them as the canonical source after this migration. part is a UI transcript unit, not a billing or
usage unit.
Persist provider-normalized token fields separately:
input_tokensoutput_tokensreasoning_tokenscache_creation_input_tokenscache_read_input_tokensprovider_total_tokenscomputed_total_tokensinput_tokens stores provider-normalized total input. For AI SDK v6 Anthropic
usage this already includes ordinary input, cache-hit input, and cache-write
input. cache_read_input_tokens is cache-hit volume and
cache_creation_input_tokens is cache-write volume; both stay separate only as
breakdown fields so /usage can render cache behavior without hiding it.
computed_total_tokens is:
input_tokens + output_tokens
reasoning_tokens is stored as its own provider detail. It is not added to computed_total_tokens
by default because providers may already include it in output totals.
model_usageOne row per logical model request. The row includes request identity, query source, model identity, latency, token split, retry/error flags, and raw usage JSON for later adapter debugging.
turn_usageOne row per user turn. The row aggregates model usage and tool usage for that turn. It is the source for activity-style turn counts and loop-level latency.
tool_usageOne row per tool call. The row tracks tool name, side-effect metadata, approval status, latency, output byte counts, truncation, errors, and cancellation flags.
Rollups are intentionally deferred. /usage can group over 30 days of facts directly; if the facts
grow expensive later, daily rollups can be rebuilt from these tables.
The store prunes rows older than 30 days using:
delete from model_usage where started_at < ?;
delete from turn_usage where started_at < ?;
delete from tool_usage where started_at < ?;
The cutoff is Date.now() - 30 days. Pruning must never block the model/tool path if it fails; it is
best-effort observability.
model_usage after every successful ModelComplete and after model request
failures when a request reached the runtime boundary.turn_usage at turn completion or turn error.tool_usage from tool lifecycle events:
tool_call_scheduled, permission_requested, permission_resolved,
permission_denied, tool_call_started, tool_call_progress, tool_call_result, and
tool_call_error.All writes must use trace/session/turn identifiers already present in the runtime trace context. Statistics rows must not store full prompts, full tool input/output, secrets, tokens, cookies, or private file contents.
/usage Query ShapeDaily model usage:
select
date(started_at / 1000, 'unixepoch', 'localtime') as day,
provider_id,
model_id,
sum(input_tokens) as input_tokens,
sum(output_tokens) as output_tokens,
sum(cache_creation_input_tokens) as cache_creation_input_tokens,
sum(cache_read_input_tokens) as cache_read_input_tokens,
sum(computed_total_tokens) as computed_total_tokens,
count(*) as model_request_count,
avg(duration_ms) as avg_duration_ms,
avg(time_to_first_token_ms) as avg_time_to_first_token_ms
from model_usage
where started_at >= ?
group by day, provider_id, model_id
order by day asc;
Daily turn activity:
select
date(started_at / 1000, 'unixepoch', 'localtime') as day,
count(*) as turn_count,
sum(computed_total_tokens) as computed_total_tokens
from turn_usage
where started_at >= ?
group by day
order by day asc;
Tool latency:
select
tool_name,
count(*) as calls,
avg(duration_ms) as avg_duration_ms,
sum(case when status = 'error' then 1 else 0 end) as error_count
from tool_usage
where started_at >= ?
group by tool_name
order by calls desc;
model_usage path.