feat(workflows): add tool call count tracking to agent workflow

AH
4mo ago
+102-0
  • Introduced countToolCallsInMessage to track tool invocations in the agent's response message.
  • Updated runAgentWorkflow to include toolCallCount in the metadata sent to handleChatCredits.
  • Enhanced tests to verify that toolCallCount is correctly passed and handled during the workflow execution.

Summary by cubic

Track and record tool call counts per agent turn so spend reports reflect actual tool usage.

  • New Features

    • Added countToolCallsInMessage to count tool UI parts on assistant messages; defaults to 0 with tests.
    • runAgentWorkflow now passes toolCallCount to handleChatCredits (stored as usage_events.tool_call_count, source set to "api"); handleChatCredits accepts and forwards this value with tests.
  • Refactors

    • Streamlined test assertions and formatting in runAgentWorkflow.test.ts and countToolCallsInMessage.test.ts.

Summary by CodeRabbit

  • Chores
    • Enhanced credit deduction tracking to capture per-turn tool invocation counts and include them in usage/audit records.
    • Added message-level tool-call counting so credit recording can account for tool usage more accurately without changing user-facing behavior.