Skip to content
TuffTasks, docs and chat for teams and AI agents

Interactive Answers

Token Flow

Why a run cost what it did — a sankey from inputs through stages to outcomes in tokens or dollars, with the costliest input flagged and a fix to apply.

1 variantMIT

Preview

Token flow

research-agent · run 8f2c · 14 steps

This run used 674k tokens. Most of it is search output that is fetched, then dropped before the answer.

674k in this run. System prompt: 18k, 3% of the run. Chat history: 96k, 14% of the run. Tool results: 412k, 61% of the run. Files: 148k, 22% of the run. Planning: 66k, 10% of the run. Tool calls: 338k, 50% of the run. Answer: 270k, 40% of the run. Output: 146k, 22% of the run. Cached: 224k, 33% of the run. Discarded: 304k, 45% of the run.

This run
674k
Cached
33%of tokens
Discarded
304k

Installation

Copies the component into components/ui and installs its dependencies and theme tokens.

pnpm dlx shadcn@latest add https://tuffui.com/r/token-flow.json

Usage

Pass your own data and wire the callbacks to your agent runtime.

import { TokenFlow } from "@/components/ui/token-flow";

// Rendered by the agent in answer to "why did this run cost so much?"
export function RunCostAnswer({ run, hint }: Props) {
  const [unit, setUnit] = useState<"tokens" | "cost">("tokens");
  return (
    <TokenFlow
      nodes={[
        { id: "system", label: "System prompt", column: 0 },
        { id: "tools", label: "Tool results", column: 0 },
        { id: "calls", label: "Tool calls", column: 1 },
        { id: "answer", label: "Answer", column: 1 },
        // Prices in $ per 1M tokens go on the last column.
        { id: "output", label: "Output", column: 2, rate: 15 },
        { id: "cached", label: "Cached", column: 2, rate: 0.3 },
        { id: "wasted", label: "Discarded", column: 2, rate: 3, tone: "waste" },
      ]}
      links={run.flows} // [{ source: "tools", target: "calls", tokens: 260_000 }, …]
      unit={unit}
      onUnitChange={setUnit}
      summary={run.summary}
      suggestion={{
        nodeId: "tools",
        title: "Tool results are 61% — truncate search output",
        detail: "Keep the top 5 results at 2k tokens each.",
        saving: "−264k tokens · −$0.79 a run",
        applied: hint.applied,
      }}
      applying={hint.running}
      onApply={() => applyFix(hint.id)}
      onDismiss={() => dismiss(hint.id)}
    />
  );
}

Props

PropTypeDefaultDescription
nodes*TokenFlowNode[]—Inputs, stages and outcomes. `column` places each node; order within a column is the order given.
links*TokenFlowLink[]—Token flows between nodes in neighbouring columns. Flow width is tokens, or dollars in cost mode. When links change, flows and labels glide to their new size.
columnsstring[]["Inputs", "Stages", "Outcomes"]Column headings, left to right.
unit"tokens" | "cost"—Controlled measure. Cost flows back from the `rate` on last-column nodes, so every flow has a dollar value and the sankey stays balanced.
defaultUnit"tokens" | "cost""tokens"Starting measure when uncontrolled.
onUnitChange(unit) => void—Called from the Tokens / $ toggle in the header.
suggestionTokenFlowSuggestion | null—The agent's hint about the costliest node. Its node is drawn in the accent with its share, its flows lit, and the hint shows under the chart with Apply and Dismiss.
onApply(suggestion) => void—Shows the primary Apply suggestion button. Swap in new `links` and set `applied` to show the result.
onDismiss(suggestion) => void—Shows a quiet Dismiss button.
applyingbooleanfalseSpinner on Apply while the agent reruns.
pointedbooleantrueLights the suggestion's flows until someone points at another node or flow.
summaryReact.ReactNode—Agent text above the chart.
iconIconFlowArrowHeader icon, a Phosphor icon.
titleReact.ReactNode"Token flow"Header title.
subtitleReact.ReactNode"674k tokens · $3.17"Secondary line under the title.
statsbooleantrueFooter stats: the run's total, the cached share of tokens and the waste-toned outcome.
formatTokens(tokens: number) => string"412k"Formats token values.
formatCost(dollars: number) => string"$1.93"Formats dollar values.
introbooleantrueFlows sweep in left to right on mount and nodes fade in by column. Skipped with reduced motion.
classNamestring—Extra classes for the root element.
type TokenFlowNode = {
  id: string;
  label: string;           // "Tool results"
  column: number;          // 0 inputs, 1 stages, 2 outcomes
  rate?: number;           // $ per 1M tokens, on the last column
  tone?: "neutral" | "waste";
};

type TokenFlowLink = { source: string; target: string; tokens: number };

type TokenFlowSuggestion = {
  nodeId: string;
  title: React.ReactNode;
  detail?: React.ReactNode;
  saving?: React.ReactNode;    // "−264k tokens · −$0.79 a run"
  applied?: boolean;
  appliedText?: React.ReactNode;
};