React Hooks & Provider
React Hooks & Provider
Looking for pre-built UI components? See React UI Components. For the core JavaScript SDK, see JavaScript.
Installation
@deepgram/react lists @deepgram/agents as a dependency and re-exports the SDK types you need (AgentSessionConfig, AgentSettingsObject, MicrophoneOptions, and the rest), so a single npm install @deepgram/react is all you need for the React layer. If you want direct access to the SDK classes (AgentSession, AgentMicrophone, AgentPlayer), import them from @deepgram/agents.
Usage
Wrap your component tree in AgentProvider, then use hooks to subscribe to exactly the state slices you need. Each hook triggers re-renders only when its own values change.
Replace YOUR_AGENT_ID with a Reusable Agent Configuration UUID, or pass an inline agent config object instead. See Agent Configuration for both patterns.
AgentProvider
The provider creates and manages AgentSession, AgentMicrophone, and AgentPlayer instances. All hooks below must be called within an AgentProvider.
Props
Hooks
useAgentState
Connection state and lifecycle controls. Re-renders only when connection state changes.
useAgentConversation
Conversation history and text input. Re-renders when a new message arrives.
Each ConversationEntry contains:
useAgentMode
Tracks the agent’s speaking/listening mode with playback awareness. The mode transitions from "speaking" to "listening" only after all queued audio finishes playing in the browser, not when the server sends the AgentAudioDone event. This prevents the UI from showing “listening” while the agent’s voice is still audible.
The playback-aware transition is automatic. The provider measures AgentPlayer.getRemainingPlaybackTime() when the server signals audio-done, then delays the mode switch by that duration. No configuration needed.
useAgentMicrophone
Microphone state, mute controls, and input volume.
getInputVolume() reads the current microphone level without triggering a re-render. Call it inside requestAnimationFrame or a canvas draw loop for smooth audio visualizations.
useAgentPlayer
Audio playback state, mute controls, and output volume.
useAgentControls
Action methods only, no state. All returned functions are useCallback-wrapped refs with stable identity — they never change between renders. Components that use only this hook will never re-render due to agent state changes.
Use this for components that dispatch commands but do not display state, such as a toolbar or keyboard shortcut handler.
useAgentClientTool
Register a client-side tool handler scoped to the component lifecycle. The handler is automatically unregistered when the component unmounts, so tools only exist while the component that provides them is mounted.
Dynamic tools registered with this hook take priority over the onFunctionCall prop on AgentProvider. If no dynamic tool matches the requested function name, the provider falls back to onFunctionCall.
The handler always captures the latest closure, so referencing component state inside the handler works without stale-state issues.
useAgentSession
Raw escape hatch to the underlying AgentSession instance. Use for advanced operations not covered by other hooks, such as listening to custom events or calling lower-level session methods.
useAgentContext
Access the full context value. Prefer the focused hooks above for selective re-rendering. This hook is available when you need several unrelated values without importing multiple hooks.
Standalone Hook
For simpler apps that do not need shared state across multiple components, useDeepgramAgent manages the session, microphone, and player internally without requiring a provider.
Options
Return values
useDeepgramAgent does not support useAgentClientTool. Use the provider pattern if you need per-component tool registration.