feat(PBI-67/ST-1300): cost-attribution voor thinking-tokens + admin UI

T-792: token-stats + token-history rekenen actual_thinking_tokens nu
mee in de totale kosten (tegen input-rate, conform Anthropic billing).
COALESCE-veilig zodat oude rijen 0 bijdragen i.p.v. NaN. Nieuwe export
`getTokenStatsByKind` aggregeert tokens en kosten per ClaudeJob.kind
zodat we relatieve uitgaven van IDEA_GRILL/IDEA_MAKE_PLAN/PLAN_CHAT/
TASK_IMPLEMENTATION/SPRINT_IMPLEMENTATION kunnen zien.

T-793: admin/jobs Kosten-tabel toont:
  - Nieuwe kolom 'Thinking' (aantal verbruikte thinking-tokens)
  - Mismatch-marker (rood) als requested_model afwijkt van actuele
    model_id — duidt op een worker die de CLI-flag niet doorgaf.
    Tooltip toont aangevraagd model. Geen Sentry/log-noise.

Page-level cost-berekening volgt dezelfde formule (input_price ×
thinking_tokens). 563 tests groen.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-05-08 11:16:22 +02:00
parent 86472674f5
commit d0bebda3ac
4 changed files with 99 additions and 8 deletions

View file

@ -24,6 +24,10 @@ type Job = {
pr_url: string | null
error: string | null
model_id: string | null
actual_thinking_tokens: number | null
requested_model: string | null
requested_thinking_budget: number | null
requested_permission_mode: string | null
cost_usd: number | null
}
@ -131,13 +135,24 @@ function CostRow({ job }: { job: Job }) {
function handleCancel() { startTransition(() => cancelJobAction(job.id)) }
function handleDelete() { startTransition(() => deleteJobAction(job.id)) }
const costLabel = job.cost_usd != null ? `$${job.cost_usd.toFixed(4)}` : '—'
const thinkingLabel = job.actual_thinking_tokens != null ? job.actual_thinking_tokens.toLocaleString('nl-NL') : '—'
const modelMismatch = job.requested_model != null && job.model_id != null && job.requested_model !== job.model_id
const modelTitle = job.requested_model
? `Aangevraagd: ${job.requested_model}${modelMismatch ? ' (mismatch met actueel)' : ''}`
: undefined
return (
<TableRow>
<TableCell className="font-mono text-xs text-muted-foreground">{job.id.slice(0, 8)}</TableCell>
<TableCell className="text-sm">{job.user.username}</TableCell>
<TableCell className="text-sm">{job.product.name}</TableCell>
<TableCell className="text-xs">{KIND_LABEL[job.kind] ?? job.kind}</TableCell>
<TableCell className="text-xs text-muted-foreground">{job.model_id ?? '—'}</TableCell>
<TableCell
className={`text-xs ${modelMismatch ? 'text-priority-high font-medium' : 'text-muted-foreground'}`}
title={modelTitle}
>
{job.model_id ?? '—'}
</TableCell>
<TableCell className="text-xs font-mono text-muted-foreground">{thinkingLabel}</TableCell>
<TableCell className="text-xs font-mono">{costLabel}</TableCell>
<TableCell className="text-xs text-muted-foreground">
{new Date(job.created_at).toLocaleString('nl-NL', { dateStyle: 'short', timeStyle: 'short' })}
@ -164,6 +179,7 @@ function CostsTable({ jobs }: { jobs: Job[] }) {
<TableHead>Product</TableHead>
<TableHead>Type</TableHead>
<TableHead>Model</TableHead>
<TableHead>Thinking</TableHead>
<TableHead>Kosten (USD)</TableHead>
<TableHead>Aangemaakt</TableHead>
<TableHead className="text-right">Acties</TableHead>
@ -172,7 +188,7 @@ function CostsTable({ jobs }: { jobs: Job[] }) {
<TableBody>
{jobs.length === 0 && (
<TableRow>
<TableCell colSpan={8} className="text-center text-muted-foreground py-8">
<TableCell colSpan={9} className="text-center text-muted-foreground py-8">
Geen jobs gevonden
</TableCell>
</TableRow>