feat(chat-workflow): emit per-message cost/usage metadata (cutover Bundle C)

Merged
SW
5mo ago
+648-177

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.ts
  • lib/agent/messageMetadata/addLanguageModelUsage.ts — port of open-agents' usage.ts
  • lib/agent/messageMetadata/AgentMessageMetadata.ts — type mirroring WebAgentMessageMetadata
  • lib/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

#BundleThis PRNotes
1C per-message cost/usage✅This PR
2B subagent live progress + transcriptNext
3A.7+9 real cwd + contextLimit
4A.6 Anthropic prompt cache control
5A.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-metadata chunk appears in SSE stream with non-empty modelId and usage
  • Compare chunk shape against sandbox.recoupable.com capture

🤖 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 messageMetadata into runAgentStep.toUIMessageStream(...), emitting on finish-step.
    • Added buildMessageMetadataCallback to accumulate usage/cost and record finish reasons.
    • Introduced AgentMessageMetadata and AgentStepFinishMetadata; added helpers addLanguageModelUsage, addTokenCounts, extractGatewayCost, hasGatewayShape, and GatewayProviderMetadata.
    • Added tests for helpers and runAgentStep wiring.
  • Dependencies

    • Upgraded ai to 6.0.190 and bumped @ai-sdk/anthropic, @ai-sdk/gateway, @ai-sdk/google, @ai-sdk/mcp, @ai-sdk/openai to latest stable.
    • Adjusted runAgentStep and setupChatRequest to await convertToModelMessages (now async).

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.

Review Change Stack