Skip to documentation content

Message Usage

Show the tokens, cost, and latency of one reply or a whole thread, and open the breakdown on demand.

message-usage-demo

Overview

Message Usage puts what a reply cost under the reply: how many tokens it used, what they cost, how long it took, and which model answered. It is a quiet row of figures that opens the full breakdown: input, the part of it read from the cache, output, the part of it spent reasoning, and the total.

Take a support assistant that answers from a long policy document. Most of each prompt is the cached document, so a reply that reads "19,604 tokens" costs a fraction of a cent. The breakdown shows why: 12,288 of its 18,420 input tokens were cached.

It is the per-message companion to Context Meter. Context Meter answers "how full is the window now"; Message Usage answers "what did this reply use". They share the same token formatting.

Message Usage counts nothing. Pass the figures your provider reported. It prices nothing either, unless you pass pricing, and then it marks the cost as an estimate.

Anatomy

MessageUsage
  • SummaryThe figures chosen with show: tokens, cost, latency, and model
  • BreakdownA popover with input, cached, output, reasoning, and total tokens
  • Cost and latencyRows under the counts, when you pass them
  • RatesThe list prices an estimated cost was worked out from

MessageUsage is one component that takes data. Installing message-usage brings Popover and Context Meter along as registry dependencies.

Behavior

Counts

usage.cachedInput is part of usage.input, and usage.reasoning is part of usage.output, as most providers report them. The breakdown indents them under their parent rows, and the total is input plus output. Rows for cached and reasoning tokens appear only when they are above zero.

Cost

Pass cost when your provider bills you a figure; it is shown as it is. Pass pricing instead to estimate it from list prices with estimateCost. An estimate is marked with a tilde, such as "~$0.0042", and the breakdown names the rates it used and says the bill may differ. When you pass both, cost wins.

Costs under a cent keep two significant digits, so a cheap reply reads "$0.0042" instead of "$0.00".

Thread totals

variant="thread" totals a conversation. Add the counts with sumUsage and the costs yourself, and pass messageCount. The thread variant leaves out latency, because the latencies of separate replies do not add up to anything meaningful.

What the row shows

show picks the figures in the collapsed row and their order. The default is tokens, cost, and latency. The breakdown always shows everything you passed.

Accessibility

The row is a button. Its name starts with "Usage details" and then reads the visible figures with a pause between each, such as "Usage details: 19,604 tokens, ~$0.0042, 2.4 s". The dots between the figures are hidden from assistive technology. The details open in a popover named by its title, which takes focus and closes on Escape, returning focus to the row.

The counts are a description list, so each figure is read with its name. Every digit uses tabular figures, so columns of numbers line up.

Every string comes from labels, including the number, currency, and duration formats.

KeyAction
Enter or SpaceOpen or close the breakdown
EscapeClose the breakdown

Installation

npx honestui@latest add message-usage

Usage

import { MessageUsage } from "@/components/ai/message-usage"

<MessageUsage
  model={message.model}
  usage={{
    input: message.usage.inputTokens,
    cachedInput: message.usage.cachedInputTokens,
    output: message.usage.outputTokens,
    reasoning: message.usage.reasoningTokens,
  }}
  pricing={{ input: 0.25, cachedInput: 0.025, output: 2 }}
  latency={message.latencyMs}
/>

Examples

Per turn and thread total

Billed costs on each turn, with a thread total at the foot.

message-usage-thread

API reference

MessageUsage

Accepts native button props beyond those listed.

PropTypeDefaultDescription
usageMessageUsageTokensRequiredThe token counts your provider reported.
costnumberNoneThe billed cost. Takes precedence over pricing.
pricingModelPricingNoneList prices to estimate the cost from.
currencystringpricing.currency, then "USD"The currency of cost.
latencynumberNoneTime to the last token, in milliseconds.
modelstringNoneThe model that answered.
variant"message" or "thread""message"One reply, or a conversation's total.
messageCountnumberNoneHow many messages a thread total covers.
showMessageUsageItem[]["tokens", "cost", "latency"]The figures in the collapsed row, in order.
labelsPartial<MessageUsageLabels>EnglishEvery string and the number, currency, and duration formats.

Helpers

ExportDescription
estimateCost(usage, pricing)The cost at list prices. Cached input uses pricing.cachedInput when given.
sumUsage(usages)Adds several calls' counts.
formatUsageCost(value, currency?, locale?)Formats a cost, keeping small ones visible.
formatUsageDuration(ms)"820 ms", "2.4 s", or "1 min 12 s".

Types

ExportShape
MessageUsageTokensinput, output, and optional cachedInput and reasoning
ModelPricinginput, output, and optional cachedInput, per (default 1,000,000), and currency
MessageUsageItem"tokens", "cost", "latency", or "model"
MessageUsageLabelsTitles, row names, estimatedCost, estimatedNote, rates, number, currency, duration