You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: propagate cache metrics through harness token usage (#14746)
## Summary
Propagate `cachedInputTokens` and `cacheCreationInputTokens` through the
Mastra harness token usage pipeline so downstream consumers (e.g.,
auggie-v2 print mode) can include cache hit/miss metrics in session
output.
## Problem
The AI SDK provider already extracts cache read tokens from the backend
(`cache_read_input_tokens` in TOKEN_USAGE nodes), and `normalizeUsage()`
makes `cachedInputTokens` available on the `LanguageModelUsage` type.
However, the harness `processStream()` step-finish handler only
extracted `promptTokens` and `completionTokens`, completely ignoring
cache fields. This meant cache metrics were stripped before reaching any
consumer.
## Changes
### `types.ts`
- Added `cachedInputTokens?: number` and `cacheCreationInputTokens?:
number` to `TokenUsage` interface
- Updated `defaultDisplayState()` to initialize cache fields to 0
### `harness.ts`
- Added `emptyTokenUsage()` helper for consistent zero-initialization
with all 5 fields
- Updated `processStream()` step-finish handler to extract
`cachedInputTokens` from usage
- Updated all 7 reset sites to use `emptyTokenUsage()`
- Updated `loadThreadMetadata()` to restore cache fields from saved
thread metadata
- Updated `getTokenUsage()` return type from inline 3-field type to
`TokenUsage`
- Updated `usage_update` event emission and display state handlers to
propagate cache fields
### `token-usage.test.ts`
- Added test: extracts `cachedInputTokens` from usage
- Added test: accumulates `cachedInputTokens` across multiple steps
- Added test: defaults cache fields to 0 when not present
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Token usage now includes cached-input and cache-creation-input token
metrics; these metrics are shown in the UI, included in persisted thread
metadata, emitted in lifecycle and streaming updates, and returned by
the public token-usage API.
* **Tests**
* Added tests ensuring cached token fields accumulate correctly across
streaming steps and default to zero when absent.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Tyler Barnes <tylerdbarnes@gmail.com>
Co-authored-by: Mastra Code (openai/gpt-5.5) <noreply@mastra.ai>
Propagate cache metrics (`cachedInputTokens`, `cacheCreationInputTokens`) through harness token usage. The step-finish handler now extracts `cachedInputTokens` from AI SDK usage and propagates it through `usage_update` events, `getTokenUsage()`, display state, and thread metadata persistence.
0 commit comments