Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 33x 33x 12x 12x 12x 6x 12x 12x 12x 12x 10x 12x 12x 12x 12x 36x 5x 5x 5x | import { SectionCard, Skeleton } from "@/components/detail";
import { CostPeriod, CostSource, StepUsageBreakdown } from "@/objects/Cost";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { Layers } from "lucide-react";
import React, { useState } from "react";
import { formatCount, formatUsd } from "./format";
import { stepLabel } from "./steps";
/**
* Where a resource's tokens go — beside its cost card on the agent and
* skill pages. Its newest answers' model calls, summed by the step of the
* pipeline that made each one: triage, the answer, the quality check,
* retrieval's planner and reranker, revisions. Read from svc-ai's call
* records (`GET /app/stats/steps`), by step and never by model.
* See `docs/guides/cost-monitoring.md#where-the-tokens-go`.
*/
const PERIODS: { key: CostPeriod; label: string }[] = [
{ key: "last_7_days", label: "7d" },
{ key: "last_30_days", label: "30d" },
{ key: "this_month", label: "Month" },
];
export interface TokenStepsCardProps {
source: CostSource;
sourceId: string;
/** "agent", "skill" — used in the empty state. */
noun: string;
className?: string;
}
const TokenStepsCard: React.FC<TokenStepsCardProps> = ({
source,
sourceId,
noun,
className,
}) => {
const { getApiHandler } = useApiHandler();
const [period, setPeriod] = useState<CostPeriod>("last_30_days");
const { data, isLoading } = useQuery<StepUsageBreakdown | null>({
queryKey: [QUERY_KEYS.GET.RESOURCE_STEPS, source, sourceId, period],
queryFn: () =>
getApiHandler().stats_router.getSteps({
source_type: source,
source_id: sourceId,
period,
}),
});
const breakdown: StepUsageBreakdown | null =
data && Array.isArray(data.steps) ? data : null;
const steps = breakdown?.steps ?? [];
const total = steps.reduce((sum, s) => sum + s.cost_usd, 0);
const tokens = (s: { prompt_tokens: number; completion_tokens: number }) =>
s.prompt_tokens + s.completion_tokens;
const totalTokens = steps.reduce((sum, s) => sum + tokens(s), 0);
const turns = breakdown?.turns ?? 0;
const unpriced = steps.some((s) => s.unpriced_calls > 0);
return (
<SectionCard
icon={Layers}
title="Where the tokens go"
subtitle={breakdown?.period_label}
className={className}
action={
<div
role="tablist"
aria-label="Token breakdown period"
className="flex rounded-lg border border-gray-200 dark:border-gray-600 p-0.5"
>
{PERIODS.map((p) => (
<button
key={p.key}
type="button"
role="tab"
aria-selected={period === p.key}
onClick={() => setPeriod(p.key)}
className={`px-2 py-0.5 rounded-md text-[11px] font-medium transition-colors ${
period === p.key
? "bg-gray-900 dark:bg-white text-white dark:text-gray-900"
: "text-gray-500 dark:text-gray-400 hover:text-gray-700"
}`}
>
{p.label}
</button>
))}
</div>
}
>
{isLoading ? (
<div className="space-y-2">
<Skeleton className="h-5 w-48" />
<Skeleton className="h-20 w-full" />
</div>
) : !breakdown || breakdown.status === "unavailable" ? (
<p className="text-sm text-gray-400 italic">
Where the tokens went is unavailable right now.
</p>
) : turns === 0 || steps.length === 0 ? (
<p className="text-sm text-gray-400 dark:text-gray-500 italic">
This {noun} has no recorded answers in this period.
</p>
) : (
<div className="space-y-3">
<p className="text-[11px] text-gray-500 dark:text-gray-400">
Per answer:{" "}
<span className="font-semibold text-gray-800 dark:text-gray-100 tabular-nums">
{unpriced ? "≥ " : ""}
{formatUsd(total / turns)}
</span>{" "}
· {formatCount(Math.round(totalTokens / turns))} tokens · across{" "}
{breakdown.turns_in_period > breakdown.turns_asked
? `the newest ${breakdown.turns_asked.toLocaleString()} of ${breakdown.turns_in_period.toLocaleString()} answers`
: `${turns.toLocaleString()} answer${turns === 1 ? "" : "s"}`}
</p>
<ul className="space-y-1.5" aria-label="Steps by cost">
{steps.map((s, i) => {
const { label, node } = stepLabel(s);
const share = total > 0 ? s.cost_usd / total : 0;
return (
<li key={`${s.node}:${s.step}:${s.pass_index}:${s.kind}:${i}`}>
<div className="flex items-baseline justify-between gap-3 text-xs">
<span className="min-w-0 truncate text-gray-700 dark:text-gray-200">
{label}
{node && (
<span className="ml-1.5 font-mono text-[10px] text-gray-400 dark:text-gray-500">
{node}
</span>
)}
</span>
<span className="shrink-0 tabular-nums text-gray-600 dark:text-gray-300">
{formatUsd(s.cost_usd / turns)}
<span className="ml-1.5 text-[10px] text-gray-400 dark:text-gray-500">
{formatCount(Math.round(tokens(s) / turns))} tok
{s.cache_read_tokens > 0 &&
` · ${Math.round((s.cache_read_tokens / Math.max(s.prompt_tokens, 1)) * 100)}% cached`}
</span>
</span>
</div>
<div className="mt-0.5 h-1 rounded-full bg-gray-100 dark:bg-gray-700 overflow-hidden">
<div
className="h-full rounded-full bg-amber-400"
style={{ width: `${Math.max(share * 100, 1)}%` }}
/>
</div>
</li>
);
})}
</ul>
<p className="text-[11px] text-gray-400 dark:text-gray-500">
Each figure is per answer. Calls made before steps were recorded
read as “Not recorded”.
</p>
</div>
)}
</SectionCard>
);
};
export default TokenStepsCard;
|