Skip to content
+

Chat - Tool call events

Observe tool invocation state changes with onToolCall and drive side effects outside the message list.

This demo focuses on the onToolCall callback and how to use it for side effects that live outside the message store:

  • observing tool input and output state changes
  • building a local audit log from tool invocations
  • reacting to specific tool names with app-level logic

Key concepts

The onToolCall callback

Register onToolCall on ChatProvider to observe every tool invocation state change during streaming:

<ChatProvider
  adapter={adapter}
  onToolCall={({ toolCall }) => {
    console.log(`Tool "${toolCall.toolName}" is now ${toolCall.state}`);

    if (toolCall.state === 'output-available') {
      // Drive side effects — update dashboards, trigger notifications, etc.
    }
  }}
>
  <MyChat />
</ChatProvider>

Tool invocation states

The toolCall.state field tracks the tool lifecycle:

State Description
input-streaming Tool input is being streamed
input-available Tool input is fully available
approval-requested User approval is needed
approval-responded User has responded to the approval
output-available Tool output is ready
output-error Tool execution failed
output-denied User denied the tool call

The ChatOnToolCallPayload

interface ChatOnToolCallPayload {
  toolCall: ChatToolInvocation | ChatDynamicToolInvocation;
}

The toolCall object includes toolCallId, toolName, state, input, output, errorText, and approval fields — all typed based on your ChatToolDefinitionMap augmentation.

Tool call callback log

Send a message to stream a tool invocation and watch the callback log update.

Tool

none

Latest state

idle

Events

0

Tool events will appear here.

Key takeaways

  • onToolCall fires on every tool state change — not just when output is available
  • Use it for side effects outside the store: logging, analytics, external API calls
  • Tool invocation state progresses through a well-defined lifecycle from input to output
  • For approval flows, see the Tool approval and renderers demo

See also

API