Token Usage Formatter (components/span-token-usage.utils.ts)

mastra · packages/playground-ui/src/domains/traces/components/span-token-usage.utils.ts · typescript · Apache-2.0
analytics, infra, ai, lang:typescript, tag:tokens

TypeScript utility formatting AI model token usage breakdowns for observability tracing displays.

// Source: https://github.com/mastra-ai/mastra | License: Apache-2.0 | Copyright 2025 Kepler Software, Inc
// Provided via justclaw.it.com; keep this notice with the code.
import type { UsageStats } from '@mastra/core/observability';
import { SpanType } from '@mastra/core/observability';
import { getSpanDurationMs } from '../utils/span-utils';
import { formatDuration } from '@/utils/duration';

export type TokenUsageRow = {
  label: 'Input' | 'Output';
  total: number;
  /** Non-zero breakdown, e.g. `5,632 cached · 974 text`. */
  breakdown: string | undefined;
};

const BREAKDOWN_PARTS = [
  ['cacheRead', 'cached'],
  ['cacheWrite', 'cache write'],
  ['reasoning', 'reasoning'],
  ['text', 'text'],
  ['audio', 'audio'],
  ['image', 'image'],
] as const;

function formatBreakdown(details: o

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key