Principles
- One record per request. Every routed request already writes an AI Studio
run (
private.ai_studio_runs) or a Live session (private.ai_live_sessions). Analytics, SLOs and alerts read those, so there is no second event stream. - Metadata only. No key material, no prompt, output, tool payload, transcript or provider error message, no user id in rollups, no IP or user agent. This follows the personal data protection standard: runtime logs carry redacted, allowlisted fields and unfiltered provider errors never leave the request.
- Admin-only. Per-key analytics need the workspace’s
manage_workspace_secrets. No view breaks usage down by member, because an admin permission does not authorize disclosure of another person’s activity.
Data model
Run fields
Migration<VERSION-FROM-IRIS>_ai_usage_rollups.sql adds to both
private.ai_studio_runs and private.ai_live_sessions:
Run rows keep AI Studio’s existing metadata retention. The data protection
standard lists that 365-day default as an open gap; these columns do not widen
it, and adopting the standard’s 90-day ceiling for runs closes it.
Rollups
private.workspace_ai_usage_daily has the same shape by day. Hourly rows are
kept 35 days and daily rows 13 months. Both cascade with the workspace, hold no
user ids, and are reachable only through service-role definer RPCs. A Personal
workspace’s rollups describe one person and are erased with that account’s
workspace.
Rollup job
A Cron Control job,ai-usage-rollup, every 15 minutes:
- recomputes the current and previous hour from run rows, in keyset pages of at most 5,000 runs and 20 pages per pass, with a durable cursor;
- replaces each bucket’s values (an idempotent upsert), so retries and duplicate invocations cannot double count;
- also writes the platform-wide observability rollups below;
- runs only while listed in
CRON_CONTROL_ENABLED_JOBSand itsprivate.observability_source_controlrow isonordry_run, which is its stop fence; it never reschedules itself.
private.ai_gateway_models prices and is labeled
an estimate: the provider’s invoice is authoritative.
Error classes
The routing layer maps every provider failure to one class before anything is logged or stored:Analytics
GET /api/v1/workspaces/[wsId]/ai-provider-keys/usage with from, to,
granularity (hour up to 7 days, else day), and optional feature,
provider, model and source filters. The range is at most 400 days and
2,000 points. The handler checks manage_workspace_secrets, then calls
get_workspace_ai_usage(p_ws_id, …), which reads only that workspace’s rows.
AI Studio’s Observe pages (
/runs, /usage) and the workspace AI executions
analytics gain a Credential filter (all, workspace key, platform credits),
so the same requests appear there for their existing readers; those views show
the provider only, never last4.
UI
The Usage and Health views live in the AI provider keys tab (Settings → Developer), beside Keys. Charts use recharts with the tab and chart patterns of the AI executions analytics (apps/web/src/app/[locale]/(dashboard)/[wsId]/ai/executions/components/enhanced-execution-charts.tsx)
and the workspace usage stat cards; colors use the dynamic chart tokens.
healthy, degraded, failing, unused,
revoked), its error classes in the last 24 hours, the last Test key
result and the alert history.
Platform observability
The observability pipeline gains one source:ai-providersis added to theprivate.observability_source_controlsource check and toprivate.observability_metric_allowedwithrequests,errors,customer_key_errors,rate_limited,duration_p95_msandfirst_token_p95_ms. Services are<credential_source>:<provider>(for examplebyok:openai), aggregated across workspaces with no workspace id. Theai-usage-rollupjob writes 900-second buckets throughobservability_write_rollups.observability_burn_ratereads onlycloudflare-workerstoday. The migration generalizes it to the SLO’s own source and metrics; the existingobservability-slo-alerts.sqltests must pass unchanged.
Customer-key errors (
auth, quota, rate_limit) and request errors are not
bad events: the workspace’s key or request causes them, and they drive the
workspace alerts below. Burn alerts reuse the existing thresholds (page at 14.4
over 1 hour and 15 minutes, ticket at 6 over 6 hours and 1 hour), the
50-request minimum and the unavailable-window rule, and go to root members with
view_infrastructure and the Admin Health view. Provider availability SLOs
open tickets only, because a provider outage is reported, not fixed.
Runtime logs use console.warn and console.error with only requestId,
runId, feature, provider, modelId, credentialSource, errorClass,
status and latencyMs. A canary test sends a request whose prompt, key and
provider error contain marker strings and asserts that no log, run, rollup,
event or notification contains them.
Admin alerts
private.send_root_operator_alert delivers only to the root workspace. A
sibling, private.send_workspace_admin_alert(p_ws_id, p_kind, p_dedupe_key, p_code, p_data, p_permissions, p_priority), dedupes through
private.workspace_admin_alerts and calls public.create_notification for at
most 20 members of that workspace who hold every listed permission
(manage_workspace_secrets), on web, email and push per their preferences. The
notification carries a code so each client renders it from its EN or VI
bundle.
Each kind fires at most once per key per 24 hours; a pass delivers at most 20
alerts; resolved alerts are kept 90 days. Payloads hold the provider, the key’s
creation date, the error class, counts and the time window, never
last4, key
material or content.