agentskit.js
Hooks

useChat

High-level chat session orchestrator. Manages messages, streaming, and input state.

High-level chat session orchestrator. Manages messages, streaming, and input state.

Usage

import { useChat } from '@agentskit/react'
import { anthropic } from '@agentskit/adapters'

function Chat() {
  const chat = useChat({
    adapter: anthropic({ apiKey: 'key', model: 'claude-sonnet-4-6' }),
    onMessage: (msg) => console.log('Received:', msg.content),
  })

  return (
    <div>
      {chat.messages.map(msg => (
        <div key={msg.id}>{msg.role}: {msg.content}</div>
      ))}
      <input value={chat.input} onChange={e => chat.setInput(e.target.value)} />
      <button onClick={() => chat.send(chat.input)}>Send</button>
      {chat.status === 'streaming' && <button onClick={chat.stop}>Stop</button>}
    </div>
  )
}

API

const chat = useChat(config)

Config

ParamTypeDescription
adapterAdapterFactoryAI provider adapter
onMessage(msg: Message) => voidCalled when assistant message completes
onError(err: Error) => voidCalled on stream error
initialMessagesMessage[]Pre-populate chat history

Returns

FieldTypeDescription
messagesMessage[]All messages in the conversation
send(text: string) => voidSend a user message and stream response
stop() => voidAbort current stream
retry() => voidRetry last assistant message
statusStreamStatusCurrent streaming status
inputstringCurrent input value
setInput(value: string) => voidUpdate input value
✎ Edit this page on GitHub·Found a problem? Open an issue →·How to contribute →

On this page