feat(chat-workflow): emit per-message cost/usage metadata (cutover Bundle C)
Summary
First step in the open-agents → api cutover sequence. Adds a messageMetadata callback to runAgentStep's toUIMessageStream so the UI receives {modelId, lastStepUsage, totalMessageUsage, lastStepCost, totalMessageCost, stepFinishReasons} on every assistant turn — matching open-agents' WebAgentMessageMetadata shape byte-for-byte so sandbox.recoupable.com's existing model/cost badges keep working when cut over to /api/chat/workflow.
Was called out as a known gap in the original workflow port (PR 4 / PROGRESS notes).
Changes (SRP — one function per file)
lib/agent/messageMetadata/extractGatewayCost.ts— port of open-agents'gateway-metadata.tslib/agent/messageMetadata/addLanguageModelUsage.ts— port of open-agents'usage.tslib/agent/messageMetadata/AgentMessageMetadata.ts— type mirroringWebAgentMessageMetadatalib/agent/messageMetadata/buildMessageMetadataCallback.ts— stateful factory (fresh closure per turn) that accumulates usage + cost across finish-step parts- Wired into
app/lib/workflows/runAgentStep.ts
Cutover roadmap
| # | Bundle | This PR | Notes |
|---|---|---|---|
| 1 | C per-message cost/usage | ✅ | This PR |
| 2 | B subagent live progress + transcript | Next | |
| 3 | A.7+9 real cwd + contextLimit | ||
| 4 | A.6 Anthropic prompt cache control | ||
| 5 | A.4 forward Privy JWT as recoupAccessToken |
Test plan
- 19 new unit tests (6 + 4 + 6 + 3 across the 4 new files)
- Full suite 3096/3096 pass; lint clean
- E2E on preview: confirm
message-metadatachunk appears in SSE stream with non-emptymodelIdandusage - Compare chunk shape against
sandbox.recoupable.comcapture
🤖 Generated with Claude Code
Summary by cubic
Emit per-message model, usage, and cost metadata in the chat workflow so the UI can show accurate model/cost badges. Also upgraded ai and @ai-sdk/* so the usage shape matches open-agents natively for a clean cutover to /api/chat/workflow.
-
New Features
- Wired
messageMetadataintorunAgentStep.toUIMessageStream(...), emitting onfinish-step. - Added
buildMessageMetadataCallbackto accumulate usage/cost and record finish reasons. - Introduced
AgentMessageMetadataandAgentStepFinishMetadata; added helpersaddLanguageModelUsage,addTokenCounts,extractGatewayCost,hasGatewayShape, andGatewayProviderMetadata. - Added tests for helpers and
runAgentStepwiring.
- Wired
-
Dependencies
- Upgraded
aito6.0.190and bumped@ai-sdk/anthropic,@ai-sdk/gateway,@ai-sdk/google,@ai-sdk/mcp,@ai-sdk/openaito latest stable. - Adjusted
runAgentStepandsetupChatRequesttoawait convertToModelMessages(now async).
- Upgraded
Summary by CodeRabbit
- New Features
- Agent steps now surface detailed metadata to the interface, including model identifiers, token usage metrics (per-step and cumulative), calculated operational costs, and step completion details to enable enhanced monitoring and transparency of agent behavior and resource consumption.