Skip to main content
React hook for building chat interfaces with AI language models.

Parameters

string
A unique identifier for the chat. If not provided, a random ID will be generated. When provided, the useChat hook with the same id will share states across components.
Chat
An existing Chat instance. When provided, the hook will use this instance instead of creating a new one.
string
default:"/api/chat"
The API endpoint that accepts a { messages } object and returns a stream of tokens of the AI chat response.
Array<UIMessage>
Initial messages to be used in the chat.
(toolCall: ToolCall) => void
Callback function to be called when a tool call is received.
(data: JSONValue) => void
Callback function to be called when data is received from the stream.
(message: UIMessage) => void
Callback function to be called when the response is complete.
(error: Error) => void
Callback function to be called when an error occurs.
(message: UIMessage) => boolean
Function that determines whether to automatically send a message.
number
Custom throttle wait in milliseconds for the chat messages and data updates. Default is undefined, which disables throttling.
boolean
default:"false"
Whether to resume an ongoing chat generation stream.

Returns

string
The ID of the chat.
Array<UIMessage>
The current array of chat messages.
(messages: UIMessage[] | ((messages: UIMessage[]) => UIMessage[])) => void
Update the messages state locally. This is useful when you want to edit the messages on the client, and then trigger the regenerate method manually to regenerate the AI response.
(message: CreateUIMessage) => void
Send a new message to the API endpoint.
() => void
Regenerate the last AI response.
() => void
Stop the current stream.
() => void
Resume a previously stopped stream.
'idle' | 'in_progress' | 'awaiting_message'
The current status of the chat.
  • 'idle': No active generation
  • 'in_progress': Currently generating a response
  • 'awaiting_message': Waiting for user input
Error | undefined
The error object if an error occurred.
() => void
Clear the current error.
(toolResult: ToolResult) => void
Add a tool result to the chat. Deprecated: use addToolOutput instead.
(toolOutput: ToolOutput) => void
Add a tool output to the chat.
(response: ToolApprovalResponse) => void
Add a tool approval response to the chat.

Examples

Basic chat interface

With input form

With tool calls

With custom API endpoint