release: per-message cost/usage metadata + ai SDK upgrade (#592)

Merged
SW
5mo ago
+648-177

Summary

  • Adds messageMetadata callback to runAgentStep โ†’ emits message-metadata chunks per assistant finish-step with {modelId, lastStepUsage, totalMessageUsage, lastStepCost, totalMessageCost, stepFinishReasons}
  • Wire format byte-compatible with sandbox.recoupable.com's existing UI consumption (verified via Chrome DevTools MCP capture against open-agents production)
  • Upgrades ai@6.0.0-beta.122 โ†’ ^6.0.190 (+ @ai-sdk/anthropic/gateway/google/openai/mcp to latest stable) so api's LanguageModelUsage matches open-agents' flat shape natively
  • SRP cleanups: addTokenCounts, hasGatewayShape, GatewayProviderMetadata, AgentStepFinishMetadata each in their own file
  • Deletes normalizeUsage (-82 LOC) โ€” no longer needed after SDK upgrade

Test plan

  • 29 new unit tests across 7 helper files
  • Full suite 3106/3106 pass
  • E2E on preview verified: 2 message-metadata chunks emitted, totals accumulate correctly across steps, cost extraction from gateway providerMetadata works ($0.006191 + $0.010118 = $0.016309)
  • Cross-validated wire format against open-agents production โ€” byte-for-byte compatible

๐Ÿค– Generated with Claude Code


Summary by cubic

Adds per-message cost and token-usage metadata to assistant turns and upgrades the ai SDK so usage data matches the open-agents wire format used by sandbox.recoupable.com.

  • New Features

    • Emit message-metadata chunks from runAgentStep on each finish-step via a messageMetadata callback with { modelId, lastStepUsage, totalMessageUsage, lastStepCost, totalMessageCost, stepFinishReasons }.
    • Accumulates totals across steps and parses step cost from Vercel AI Gateway providerMetadata.
    • Extracted focused helpers (addLanguageModelUsage, addTokenCounts, extractGatewayCost, hasGatewayShape) and types; deleted normalizeUsage.
    • Updated runAgentStep and setupChatRequest to await convertToModelMessages.
  • Dependencies

    • Upgraded ai to 6.0.190 and @ai-sdk/anthropic, @ai-sdk/gateway, @ai-sdk/google, @ai-sdk/openai, @ai-sdk/mcp to latest stable, adopting the flat LanguageModelUsage shape for byte-compatible UI payloads.