useChat hook. Messages persist across turns using Vibes’ built-in message history.
What you’ll learn
agent.stream()for streaming responsestoDataStreamResponse()to convert streams to Vercel AI data protocoluseChatReact hook for real-time streaming UIs- Multi-turn conversation with
messageHistory
Prerequisites
ANTHROPIC_API_KEYset in your environment- For the Deno server: Vibes installed (
deno add npm:@vibesjs/sdk) - For the Next.js route:
npm install @vibesjs/sdk @ai-sdk/anthropic ai
Complete example
Run it
How it works
agent.stream(): Returns a result with textStream (async iterable of string chunks), partialOutput, and accumulated messages.
toDataStreamResponse(): Converts the text stream to the Vercel AI data stream protocol - the format useChat expects. Imported from the "ai" package (already a dependency in your project).
messageHistory: Pass previous messages to maintain conversation context across turns. Vibes accumulates new messages in result.messages - or use result.newMessages for only the new messages added in that run.
useChat: Manages message state, sends POST requests to your API route, and streams tokens into the UI automatically. No manual state management needed.
Next steps
- Vercel AI UI integration - structured output streaming,
useCompletion - Messages concept page -
serializeMessages, history processors