feat(usage): report model usage per org with identity and cost (#1855)
Model usage was recorded per call and never read, so an org could not see what it spent. Add the read side: - model_usage carries identity_id. Task calls take it from the task row, chat and curator calls from the calling principal carried in the usage record, so an outbox replay keeps its attribution. Existing task rows are backfilled from tasks. - StatsService.UsageTotals sums the caller org's rows by UTC day, source, provider, model and identity. The org comes from the principal, with no org parameter, so cross-org reads do not exist. - GET /api/usage prices tokens at the catalog's list rate, netting cache hits, and reports calls to unpriced models as tokens only. The published catalog view now carries each model's cost. - A new org Usage page behind a read_usage grant: admin and owner only, with a 7, 30 or 90 day range, totals, the daily series, and model, source and identity breakdowns. The org shell keeps it on the caller's own org.
This commit is contained in:
38 files changed
+1329
-52
No files matched your search
@@ -255,6 +255,9 @@ export const api = {
|
||||
identityCommand: <T = unknown>(id: string, command: string, body: { expected_version: number; display_name: string }) =>
|
||||
request<T>(`/api/identities/${encodeURIComponent(id)}/${encodeURIComponent(command)}`, { method: "POST", body }),
|
||||
summary: <T = unknown>() => request<T>("/api/summary"),
|
||||
// usage reads the org's model usage for the last days. The server derives
|
||||
// the org from the caller and clamps days to 1..365.
|
||||
usage: <T = unknown>(days = 30) => request<T>("/api/usage" + qs({ days })),
|
||||
tasks: <T = unknown>() => request<T>("/api/tasks"),
|
||||
// taskPage reads one page of the board; next is the cursor for the page
|
||||
// after it, or null when this page was the last.
|
||||
|
||||
@@ -34,6 +34,19 @@ export function compact(n: number | null | undefined): string {
|
||||
}).format(n);
|
||||
}
|
||||
|
||||
/** A cost in US dollars: "$1.23", and "$0.0023" for a sub-cent amount. A null
|
||||
* cost means work that could not be priced, not free work, so it reads "—"
|
||||
* rather than "$0". */
|
||||
export function usd(value: number | null | undefined): string {
|
||||
if (value == null || !Number.isFinite(value)) return "—";
|
||||
return new Intl.NumberFormat("en-US", {
|
||||
style: "currency",
|
||||
currency: "USD",
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 4,
|
||||
}).format(value);
|
||||
}
|
||||
|
||||
/** An exact date and time in zone, or in the browser's zone when zone is empty. */
|
||||
export function exactTime(value: Date, zone: string): string {
|
||||
return value.toLocaleString(undefined, zone ? { timeZone: zone, timeZoneName: "short" } : undefined);
|
||||
|
||||
@@ -43,6 +43,10 @@ const orgSpecs: NavSpec[] = [
|
||||
label: "Automation",
|
||||
paths: ["/org/schedules", "/org/scheduling-policy", "/org/channels", "/org/playbooks", "/org/alerts"],
|
||||
},
|
||||
{
|
||||
label: "Usage",
|
||||
paths: ["/org/usage"],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
@@ -24,7 +24,7 @@ const error = ref("");
|
||||
|
||||
// Policies and models act on the caller's own org server-side, so an instance
|
||||
// admin looking at another org is sent back rather than editing the wrong one.
|
||||
const OWN_ORG_ONLY = ["/org/audit", "/org/policies", "/org/models", "/org/connections", "/org/repositories"];
|
||||
const OWN_ORG_ONLY = ["/org/audit", "/org/policies", "/org/models", "/org/connections", "/org/repositories", "/org/usage"];
|
||||
const away = computed(() => !!org.value && org.value.id !== homeOrg.value);
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
|
||||
@@ -0,0 +1,333 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref, watch } from "vue";
|
||||
|
||||
import PageHeader from "@/base/PageHeader.vue";
|
||||
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
|
||||
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
|
||||
import { MetadataList } from "@/components/ui/metadata-list";
|
||||
import { ReadError } from "@/components/ui/read-error";
|
||||
import { Section } from "@/components/ui/section";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { api } from "@/lib/api";
|
||||
import { compact, usd } from "@/lib/format";
|
||||
|
||||
/**
|
||||
* What the org's model calls used and cost over a range: the totals, a row per
|
||||
* day, and the model, source and identity breakdowns that explain them. The
|
||||
* server derives the org from the caller, so the page names no org.
|
||||
*/
|
||||
interface Bucket {
|
||||
calls: number;
|
||||
input_tokens: number;
|
||||
output_tokens: number;
|
||||
cached_tokens: number;
|
||||
cost_usd: number | null;
|
||||
unpriced_calls: number;
|
||||
}
|
||||
|
||||
type DayRow = { day: string } & Bucket;
|
||||
type ModelRow = { provider: string; model: string } & Bucket;
|
||||
type SourceRow = { source: string } & Bucket;
|
||||
type IdentityRow = { identity_id: string; identity_name: string } & Bucket;
|
||||
|
||||
interface UsageReport {
|
||||
days: number;
|
||||
totals: Bucket;
|
||||
by_day: DayRow[];
|
||||
by_model: ModelRow[];
|
||||
by_source: SourceRow[];
|
||||
by_identity: IdentityRow[];
|
||||
}
|
||||
|
||||
const RANGES = [
|
||||
{ value: "7", label: "Last 7 days" },
|
||||
{ value: "30", label: "Last 30 days" },
|
||||
{ value: "90", label: "Last 90 days" },
|
||||
];
|
||||
|
||||
const range = ref("30");
|
||||
const report = ref<UsageReport | null>(null);
|
||||
const loading = ref(true);
|
||||
const error = ref("");
|
||||
// Identifies the current read, so a reply for a range the operator has left is
|
||||
// dropped instead of painted over the newer one.
|
||||
let revision = 0;
|
||||
|
||||
async function load(): Promise<void> {
|
||||
const attempt = ++revision;
|
||||
loading.value = true;
|
||||
error.value = "";
|
||||
// The numbers below the control are the numbers for its range, so the
|
||||
// previous range's report goes before the new read settles.
|
||||
report.value = null;
|
||||
try {
|
||||
const next = await api.usage<UsageReport>(Number(range.value));
|
||||
if (attempt !== revision) return;
|
||||
report.value = next;
|
||||
} catch (cause) {
|
||||
if (attempt !== revision) return;
|
||||
error.value = String((cause as Error).message || cause);
|
||||
} finally {
|
||||
if (attempt === revision) loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => void load());
|
||||
watch(range, () => void load());
|
||||
|
||||
/** Prompt and completion tokens; cached tokens are a subset of the input. */
|
||||
const tokens = (bucket: Bucket): number =>
|
||||
bucket.input_tokens + bucket.output_tokens;
|
||||
|
||||
/** The exact counts behind a compact cell. */
|
||||
function tokenTitle(bucket: Bucket): string {
|
||||
const cached = bucket.cached_tokens
|
||||
? `, ${bucket.cached_tokens.toLocaleString()} cached`
|
||||
: "";
|
||||
return `${tokens(bucket).toLocaleString()} tokens (${bucket.input_tokens.toLocaleString()} in, ${bucket.output_tokens.toLocaleString()} out${cached})`;
|
||||
}
|
||||
|
||||
interface TotalRow {
|
||||
label: string;
|
||||
value?: string;
|
||||
mono?: boolean;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
const totalRows = computed<TotalRow[]>(() => {
|
||||
const totals = report.value?.totals;
|
||||
if (!totals) return [];
|
||||
const rows: TotalRow[] = [
|
||||
{ label: "Model calls", value: totals.calls.toLocaleString() },
|
||||
{
|
||||
label: "Total tokens",
|
||||
value: compact(tokens(totals)),
|
||||
mono: true,
|
||||
title: `${tokens(totals).toLocaleString()} tokens`,
|
||||
},
|
||||
{
|
||||
label: "Input tokens",
|
||||
value: compact(totals.input_tokens),
|
||||
mono: true,
|
||||
title: `${totals.input_tokens.toLocaleString()} tokens`,
|
||||
},
|
||||
{
|
||||
label: "Output tokens",
|
||||
value: compact(totals.output_tokens),
|
||||
mono: true,
|
||||
title: `${totals.output_tokens.toLocaleString()} tokens`,
|
||||
},
|
||||
{
|
||||
label: "Cached tokens",
|
||||
value: compact(totals.cached_tokens),
|
||||
mono: true,
|
||||
title: `${totals.cached_tokens.toLocaleString()} cached tokens, included in input tokens`,
|
||||
},
|
||||
{
|
||||
label: "Cost",
|
||||
value: usd(totals.cost_usd),
|
||||
mono: true,
|
||||
title:
|
||||
totals.cost_usd == null
|
||||
? "No call in this range could be priced"
|
||||
: undefined,
|
||||
},
|
||||
];
|
||||
if (totals.unpriced_calls > 0) {
|
||||
rows.push({
|
||||
label: "Unpriced calls",
|
||||
value: totals.unpriced_calls.toLocaleString(),
|
||||
title:
|
||||
"Calls to models without a catalog price count tokens but have no cost.",
|
||||
});
|
||||
}
|
||||
return rows;
|
||||
});
|
||||
|
||||
const dayColumns: DataTableColumn<DayRow>[] = [
|
||||
{ id: "day", label: "Day", fixed: true, class: "whitespace-nowrap" },
|
||||
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
|
||||
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
|
||||
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
|
||||
];
|
||||
|
||||
const modelColumns: DataTableColumn<ModelRow>[] = [
|
||||
{ id: "provider", label: "Provider", fixed: true, class: "text-fg-muted whitespace-nowrap" },
|
||||
{ id: "model", label: "Model", fixed: true, class: "w-full whitespace-normal" },
|
||||
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
|
||||
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
|
||||
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
|
||||
];
|
||||
|
||||
const sourceColumns: DataTableColumn<SourceRow>[] = [
|
||||
{ id: "source", label: "Source", fixed: true },
|
||||
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
|
||||
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
|
||||
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
|
||||
];
|
||||
|
||||
const identityColumns: DataTableColumn<IdentityRow>[] = [
|
||||
{ id: "identity", label: "Identity", fixed: true, class: "w-full whitespace-normal" },
|
||||
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
|
||||
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
|
||||
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<PageHeader title="Usage">
|
||||
<Select v-model="range">
|
||||
<SelectTrigger class="w-40" size="sm" aria-label="Date range">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem v-for="option in RANGES" :key="option.value" :value="option.value">
|
||||
{{ option.label }}
|
||||
</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</PageHeader>
|
||||
|
||||
<ReadError v-if="error" :message="error" retry-label="Retry" class="mb-4" @retry="load" />
|
||||
|
||||
<Section v-else-if="loading">
|
||||
<p role="status" class="flex items-center gap-2 text-label text-fg-muted">
|
||||
<Spinner />Loading usage…
|
||||
</p>
|
||||
</Section>
|
||||
|
||||
<Section v-else-if="!report || report.totals.calls === 0">
|
||||
<Empty>
|
||||
<EmptyHeader>
|
||||
<EmptyTitle>No model calls in this range.</EmptyTitle>
|
||||
</EmptyHeader>
|
||||
</Empty>
|
||||
</Section>
|
||||
|
||||
<template v-else>
|
||||
<Section title="Totals">
|
||||
<MetadataList :rows="totalRows">
|
||||
<template #value="{ row }">
|
||||
<span
|
||||
class="tabular-nums text-title font-medium"
|
||||
:class="row.mono && 'font-mono'"
|
||||
:title="row.title"
|
||||
>{{ row.value }}</span
|
||||
>
|
||||
</template>
|
||||
</MetadataList>
|
||||
</Section>
|
||||
|
||||
<Section title="Daily">
|
||||
<DataTable
|
||||
:columns="dayColumns"
|
||||
:rows="report?.by_day ?? []"
|
||||
:row-key="(row) => row.day"
|
||||
storage-key="usage-daily"
|
||||
>
|
||||
<template #empty>
|
||||
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
|
||||
</template>
|
||||
<template #cell-day="{ row }">
|
||||
<span class="font-mono text-caption">{{ row.day }}</span>
|
||||
</template>
|
||||
<template #cell-calls="{ row }">
|
||||
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
|
||||
</template>
|
||||
<template #cell-tokens="{ row }">
|
||||
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
|
||||
</template>
|
||||
<template #cell-cost="{ row }">
|
||||
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
|
||||
</template>
|
||||
</DataTable>
|
||||
</Section>
|
||||
|
||||
<Section title="By model">
|
||||
<DataTable
|
||||
:columns="modelColumns"
|
||||
:rows="report?.by_model ?? []"
|
||||
:row-key="(row) => `${row.provider}/${row.model}`"
|
||||
storage-key="usage-by-model"
|
||||
>
|
||||
<template #empty>
|
||||
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
|
||||
</template>
|
||||
<template #cell-provider="{ row }">
|
||||
<span class="font-mono text-caption">{{ row.provider || "–" }}</span>
|
||||
</template>
|
||||
<template #cell-model="{ row }">
|
||||
<span class="font-mono text-caption">{{ row.model || "–" }}</span>
|
||||
</template>
|
||||
<template #cell-calls="{ row }">
|
||||
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
|
||||
</template>
|
||||
<template #cell-tokens="{ row }">
|
||||
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
|
||||
</template>
|
||||
<template #cell-cost="{ row }">
|
||||
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
|
||||
</template>
|
||||
</DataTable>
|
||||
</Section>
|
||||
|
||||
<Section title="By source">
|
||||
<DataTable
|
||||
:columns="sourceColumns"
|
||||
:rows="report?.by_source ?? []"
|
||||
:row-key="(row) => row.source"
|
||||
storage-key="usage-by-source"
|
||||
>
|
||||
<template #empty>
|
||||
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
|
||||
</template>
|
||||
<template #cell-source="{ row }">
|
||||
<span class="font-mono text-caption">{{ row.source || "–" }}</span>
|
||||
</template>
|
||||
<template #cell-calls="{ row }">
|
||||
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
|
||||
</template>
|
||||
<template #cell-tokens="{ row }">
|
||||
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
|
||||
</template>
|
||||
<template #cell-cost="{ row }">
|
||||
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
|
||||
</template>
|
||||
</DataTable>
|
||||
</Section>
|
||||
|
||||
<Section title="By identity">
|
||||
<DataTable
|
||||
:columns="identityColumns"
|
||||
:rows="report?.by_identity ?? []"
|
||||
:row-key="(row) => row.identity_id || 'unknown'"
|
||||
storage-key="usage-by-identity"
|
||||
>
|
||||
<template #empty>
|
||||
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
|
||||
</template>
|
||||
<template #cell-identity="{ row }">
|
||||
<span :title="row.identity_id || undefined">{{ row.identity_name || "Unknown" }}</span>
|
||||
</template>
|
||||
<template #cell-calls="{ row }">
|
||||
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
|
||||
</template>
|
||||
<template #cell-tokens="{ row }">
|
||||
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
|
||||
</template>
|
||||
<template #cell-cost="{ row }">
|
||||
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
|
||||
</template>
|
||||
</DataTable>
|
||||
</Section>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
@@ -14,6 +14,7 @@ const OrgAgentsPage = () => import("@/org/OrgAgentsPage.vue");
|
||||
const OrgWorkspacesPage = () => import("@/org/OrgWorkspacesPage.vue");
|
||||
const OrgAccessPage = () => import("@/org/OrgAccessPage.vue");
|
||||
const OrgModelsPage = () => import("@/org/OrgModelsPage.vue");
|
||||
const OrgUsagePage = () => import("@/org/OrgUsagePage.vue");
|
||||
const SchedulingPolicyPage = () => import("@/settings/SchedulingPolicyPage.vue");
|
||||
const ToolsPage = () => import("@/settings/ToolsPage.vue");
|
||||
const InstructionsPage = () => import("@/settings/InstructionsPage.vue");
|
||||
@@ -306,6 +307,12 @@ const routes = [
|
||||
component: () => import("@/settings/PlaybooksPage.vue"),
|
||||
meta: { section: "org_config", label: "Playbooks", navPath: "/org" },
|
||||
},
|
||||
{
|
||||
path: "usage",
|
||||
name: "org-usage",
|
||||
component: OrgUsagePage,
|
||||
meta: { section: "org_usage", label: "Usage", navPath: "/org" },
|
||||
},
|
||||
],
|
||||
},
|
||||
// The old unified Access area and the org-owned settings that used to sit
|
||||
|
||||
Reference in new issue
Block a user