Expose token usage that comes from the model provider response, not local context estimation. The value is only available after a model request finishes.
All model adapters normalize provider usage into ModelUsage:
inputTokens: provider-normalized total input tokens. For providers with
prompt caching this already includes ordinary input, cache creation/write, and
cache read tokens.outputTokens: provider-reported output tokens.totalTokens: provider total when available, otherwise the runtime derives a
total from normalized usage.cacheReadTokens: provider-reported cache hit tokens, kept as a breakdown of
inputTokens.cacheWriteTokens: provider-reported cache creation/write tokens, kept as a
breakdown of inputTokens.reasoningTokens: provider-reported reasoning tokens.For turn-level consumers, core exposes a ModelUsageSummary on TurnResult.
It is aggregated from model_complete.payload.usage events emitted during that
turn. A turn that never receives provider usage leaves this field undefined.
prompt_tokens,
completion_tokens, total_tokens, prompt_tokens_details.cached_tokens,
and completion_tokens_details.reasoning_tokens.input_tokens, output_tokens, total_tokens,
input_tokens_details.cached_tokens, and
output_tokens_details.reasoning_tokens.input_tokens, output_tokens,
cache_creation_input_tokens, and cache_read_input_tokens. AI SDK v6
normalizes inputTokens to ordinary input plus cache write plus cache read,
so runtime context and compact calculations must not add cache tokens again.model_complete.payload.usage is the per-model-request source of truth.turn_complete.payload.usage may include the aggregated usage for the whole
turn.turn_complete.payload.tokenCount must be derived from provider usage, and
should be 0 when no provider usage exists.The persisted assistant message already owns the normalized tokens value. Context
estimation reuses that value instead of introducing a second providerUsage field:
tokens on its internal assistant entry;input and positive output for a
non-empty assistant. Existing compatibility handling for legacy normalized usage is
retained, but new estimation behavior must not be designed around malformed usage;output: 0 without a usable total does not prove that provider usage
covered the assistant output, because legacy normalization also maps a missing
outputTokens field to zero. In that case estimation keeps the provider input as
the base and locally estimates from the assistant itself; a positive output or a
usable total advances the local suffix past the assistant;Compact 替换 provider-visible 的旧前缀后,preserved assistant 的原始 usage 不再代表当前上下文。 运行时和冷启动 projection 只在 preserved 消息的副本上将 usage 归零,Compact 之后新增的 assistant 仍保留自身 tokens;持久化 transcript 不被改写。
Microcompact 只改写旧 tool result,不清理最近 assistant 的 usage。估算继续使用该 provider baseline,并只对 assistant 之后的消息做本地估算;这是有意保留的近似,不额外增加 usage invalidation 或重算分支。
This is an in-memory ownership link for estimation, not a new protocol or persistence
schema. tokens is response metadata and must never be added to ModelInputMessage.
The TUI should display real usage only after a model completion or final turn
result. It must not display estimated context tokens as model usage. The final
status footer should include the aggregated turn usage when available. To keep
the ready state readable, final turn usage is rendered on its own footer line
below the main ready/trace line, formatted as Tokens: <usage>.
ModelUsage normalization.model_complete and final turn result,
including the final Tokens: footer detail line.