Adding Event Listeners
Register event listeners after creating the Anam client and before starting the session withstream() or streamToVideoElement().
Use the addListener method before the start call:
Available Events
Type Definitions
Message
MessageStreamEvent
DirectorNoteCueAppliedEvent
ToolCallStartedPayload
ToolCallCompletedPayload
ToolCallFailedPayload
ToolCallHandler
ClientToolEvent (Deprecated)
ConnectionClosedCode
ReasoningMessage
ReasoningStreamEvent
Example Usage
Loading States
Use connection events to manage loading states:Connection Closed Handling
Handle connection closures with reason codes:Microphone Permission Flow
Track microphone permission state:Speech detection
Know when the user starts and stops speaking, before any transcript is available:MESSAGE_STREAM_EVENT_RECEIVED. The correlationId ties the speech start/end pair to the eventual transcript.
If USER_SPEECH_ENDED doesn’t arrive within ~10 seconds of a USER_SPEECH_STARTED, treat it as a dropped detection and reset your UI. This can happen if the connection drops mid-speech.
Message History
Track conversation history:Multi-utterance Persona Turns
In a tool-driven response, a persona can speak before a tool call and continue after the tool returns. With@anam-ai/js-sdk version 4.25.0 or later, one completed persona Message can contain an ordered utterances array for those spoken parts:
message.content remains the complete concatenated turn. message.utterances is optional and appears only on persona messages when the engine provides utterance IDs. Use the content fallback shown above for user messages, older SDK behavior, and models that do not produce utterance boundaries.
For live captions, each persona MESSAGE_STREAM_EVENT_RECEIVED payload includes an optional utteranceId. Consecutive chunks with the same ID belong to the same utterance; append their content in arrival order.
Multi-utterance turns require an LLM that supports interleaved tool calling: text before and after a tool call in one turn. Anam-provided LLMs hosted by OpenAI or Azure support this behavior; LLMs hosted by Groq do not.
Real-time Transcription
Monitor speech in real-time:Director Notes Cues
Listen forDIRECTOR_NOTE_CUE_APPLIED when your application needs to react after Anam confirms that a Director Notes cue was applied to the avatar.
Director Notes cue events require
@anam-ai/js-sdk version 4.20.0 or later.correlationId to associate the cue with other events from the same user action.
Each MESSAGE_STREAM_EVENT_RECEIVED payload also includes an optional cueTag when a Director Notes cue takes effect from the start of that text chunk. If the field is absent, no new cue takes effect at that chunk boundary. Cue boundaries can split persona speech into multiple streamed text chunks, so append event.content in arrival order instead of assuming one event per response.
MESSAGE_HISTORY_UPDATED is unchanged. Its Message items do not include cue metadata.
Tool Call Handlers (Recommended)
UseregisterToolCallHandler to register handlers for specific tools before starting the session. This automatically emits completed or failed events when the handler completes.
Tool Call Events
Listen for tool call lifecycle events across all tool types — client, webhook, and knowledge tools all emit these events, so you can use them for logging, analytics, or monitoring:toolType and toolSubtype to distinguish between tool types in your listeners:
Server Warnings
Handle server-side warnings:Reasoning Events (Extended Thinking)
Track AI reasoning when using models with extended thinking:Removing Event Listeners
Remove listeners to prevent memory leaks, especially in single-page applications:React Example
Learn More
Client Tools Guide
Guide with examples for navigation, modals, UI updates, and more
Tools Overview
Learn about all tool types: client, webhook, and knowledge tools

