useChat
useChat(
options?:object):UseChatResult
Defined in: src/react/useChat.ts:189
A React hook for managing chat completions with authentication.
This hook provides a convenient way to send chat messages to the LLM API with automatic token management and loading state handling. Streaming is enabled by default for better user experience.
Parameters
| Parameter | Type | Description |
|---|---|---|
|
|
|
Optional configuration object |
|
|
|
Which API endpoint to use. Default: “auto”
|
|
|
|
Keep a resumable generation running server-side when the request fails or
the hook unmounts; only |
|
|
|
Optional base URL for the API requests. |
|
|
() => |
An async function that returns an authentication token.
This token will be used as a Bearer token in the Authorization header.
If not provided, |
|
|
|
Run lifecycle hooks for telemetry and tracing, for example from |
|
|
( |
Callback function to be called when a new data chunk is received. |
|
|
( |
Callback function to be called when an unexpected error is encountered. Note: This callback is NOT called for aborted requests (via |
|
|
( |
Callback function to be called when the chat completion finishes successfully. Receives raw API response - either Responses API or Completions API format. |
|
|
( |
Called with the PII matches found whenever outbound messages are redacted.
Useful for surfacing what was redacted to the user. Only fired when
|
|
|
( |
Callback function to be called when a server-side tool (MCP) is invoked during streaming. Use this to show activity indicators like “Searching…” in the UI. |
|
|
( |
Called after each tool execution round completes. Receives the round index, model content, tool calls, results, and token usage. Useful for progress indicators, cost tracking, and custom early-exit logic. |
|
|
( |
Stream handle for each HTTP round, including client-tool continuations. |
|
|
( |
Callback function to be called when thinking/reasoning content is received. This is called with delta chunks as the model “thinks” through a problem. |
|
|
( |
Callback function to be called when a tool call is requested by the LLM but no executor is registered for it (e.g. server-side tools). |
|
|
( |
Called with partial tool call arguments as they stream in. Use for live preview of artifacts (HTML, slides) being generated. |
|
|
|
Enable best-effort, client-side PII obfuscation (NOT a compliance guarantee). Outbound message text is scanned for personally identifiable information (emails, phone numbers, SSNs, credit cards, API keys, addresses) and matches are replaced with tagged placeholders before reaching the LLM provider; both streamed and final responses are de-anonymized automatically. Detection is regex-based and does not cover names, non-text content (images/files/attachments), or model-generated tool-call arguments.
|
|
|
Pre-processors run after the last user message is received but before the
first LLM request. Each receives the prompt text and a shared embedding
(computed once per request) and may return messages to enrich the
conversation. See | |
|
|
|
Buffer streamed rounds so a service can verify their canonical output. |
|
|
|
Controls adaptive output smoothing for streaming responses. Fast models can return text faster than is comfortable to read — smoothing buffers incoming chunks and releases them at a consistent, adaptive pace.
Default |
Returns
UseChatResult
Example
// Basic usage with API
const { isLoading, sendMessage, stop } = useChat({
getToken: async () => await getAuthToken(),
onFinish: (response) => console.log("Chat finished:", response),
onError: (error) => console.error("Chat error:", error)
});
const handleSend = async () => {
const result = await sendMessage({
messages: [{ role: 'user', content: [{ type: 'text', text: 'Hello!' }] }],
model: 'your-provider/your-model'
});
};
// Using extended thinking
const result = await sendMessage({
messages: [{ role: 'user', content: [{ type: 'text', text: 'Solve this complex problem...' }] }],
model: 'your-provider/your-model',
thinking: { type: 'enabled', budget_tokens: 10000 },
onThinking: (chunk) => console.log('Thinking:', chunk)
});
// Using reasoning
const result = await sendMessage({
messages: [{ role: 'user', content: [{ type: 'text', text: 'Reason through this...' }] }],
model: 'your-provider/your-model',
reasoning: { effort: 'high', summary: 'detailed' }
});