You can build an AI-powered bot that reads and responds to Liveblocks comment threads using Chat SDK, AI SDK's ToolLoopAgent, and the Liveblocks Chat SDK adapter. Chat SDK handles webhook verification, message routing, and the Liveblocks API. ToolLoopAgent wraps your language model with tools and runs an autonomous reasoning loop, calling tools and feeding results back until it has a complete answer. Redis tracks thread subscriptions and manages distributed locking for concurrent message handling, giving you a production-ready AI bot without managing infrastructure.
This guide walks you through wiring up a Next.js app directory project that responds to @-mentions in Liveblocks threads with streamed AI replies and tool calling.
Before you begin, make sure you have:
- Node.js 18+
- A Next.js app that uses Liveblocks Comments
- A Liveblocks account and the dashboard open
- A Redis instance (local or hosted, such as Upstash)
- A Vercel account with AI Gateway access
Liveblocks Comments is a fully featured React commenting system you can embed in any application, giving users threaded discussions, @-mentions, emoji reactions, and notifications out of the box. Threads are attached to specific rooms in your app, making them ideal for contextual conversations around documents, designs, or any shared content.
The @liveblocks/chat-sdk-adapter connects Chat SDK to Liveblocks' webhook system. You register event handlers (like onNewMention and onReaction) and the adapter routes incoming webhook payloads to them. It handles signature verification, parses comment and reaction events, and exposes a consistent thread API for posting replies or adding emoji reactions.
AI SDK's ToolLoopAgent wraps a language model with tools and runs an autonomous loop: the model generates text or calls a tool, the SDK executes the tool, feeds the result back, and repeats until the model finishes. When you pass a model string like "anthropic/claude-sonnet-4-6" and host your application on Vercel, the AI SDK routes the request through the Vercel AI Gateway automatically. Chat SDK accepts any AsyncIterable<string> as a message, so you can pass the agent's fullStream directly to thread.post() for real-time streaming in Liveblocks threads.
The Redis state adapter tracks which threads the bot has subscribed to, so follow-up messages in the same thread are handled automatically after the first mention.
Before continuing, make sure you have a React app with Liveblocks Comments up and running. If you haven't set that up yet, follow the quickstart guide first.
Install the Liveblocks adapter, Chat SDK, AI SDK, and other related packages:
The chat package is the Chat SDK core. @liveblocks/chat-sdk-adapter is the Liveblocks platform adapter. ai is the AI SDK, which includes ToolLoopAgent. zod is used to define tool input schemas. @chat-adapter/state-redis is the Redis state adapter, which handles thread subscriptions and distributed locking.
Head to the Liveblocks dashboard, open the project you'd like to use, and copy the Secret Key from the API Keys page.
Create a .env.local file in your project root:
You'll create the webhook secret further on in the guide.
Instead of an AI Gateway API key, this guide uses Vercel OIDC tokens to authenticate to the AI Gateway. Link your app to a Vercel project and pull the token into your local environment:
vercel env pull writes a VERCEL_OIDC_TOKEN into .env.local alongside any other project environment variables. The AI SDK's gateway provider reads this token automatically when no AI_GATEWAY_API_KEY is set, so no code changes are needed.
OIDC tokens expire after 12 hours, so re-run vercel env pull during longer development sessions to refresh it. In production on Vercel, the token is generated and rotated for you automatically.
Create app/database.ts to export your bot's user ID and a getUser function. Chat SDK calls resolveUsers to convert user IDs from @-mentions into display names when constructing messages.
The function must return an object matching this shape:
In production, query your own user database or auth provider:
Create app/tools.ts with the tools your agent can call. Each tool has a description that tells the model when to use it, an inputSchema that the model fills in, and an execute function that runs when the tool is called:
Create app/bot.ts with a ToolLoopAgent and a Chat instance. The agent is configured with a model, a system prompt, and your tools. The bot wires the Liveblocks adapter and Redis state to your event handlers:
Add event handlers to app/bot.ts after the bot instance. When someone @-mentions the bot, onNewMention fires. The handler acknowledges the message with a reaction, then streams the agent's response directly into the thread. fullStream is preferred over textStream because it preserves paragraph breaks between tool-calling steps:
Create the API route at app/api/webhooks/liveblocks/route.ts. This is the endpoint Liveblocks will POST webhook events to:
For production deployments on Vercel, use waitUntil from @vercel/functions so your handler finishes processing after the HTTP response has been sent. This is required on serverless platforms where the function would otherwise terminate early:
- Expose your dev server with a tunnel, for example using ngrok:
- Go to the Liveblocks dashboard and create a new webhook endpoint. Set the URL to your generated ngrok URL, add your webhook routeโs pathname to the end, and enable these events in the project dashboard:
commentCreatedcommentReactionAddedcommentReactionRemoved
- Copy the webhook secret and add it to
.env.localasLIVEBLOCKS_WEBHOOK_SECRET:
Now when users @-mention your bot in a Liveblocks thread, it will stream a response and call tools autonomously.
Chat SDK supports multiple platforms from a single codebase. The event handlers and agent logic you've already defined work identically across all of them, since the SDK normalizes messages, threads, and reactions into a consistent format.
To add Slack or Teams, install the relevant adapter packages and register them alongside the Liveblocks adapter:
Then add them to your Chat instance in app/bot.ts:
The existing webhook route already uses a [platform] parameter, so each platform gets its own endpoint automatically: /api/webhooks/slack and /api/webhooks/teams. No additional routing code is needed.
Streaming behavior varies by platform. Slack uses its native streaming API for smooth real-time updates, while Liveblocks and Teams fall back to a post-then-edit pattern that throttles updates to avoid rate limits. You can adjust the update interval with the streamingUpdateIntervalMs option when creating your Chat instance.
See the Chat SDK adapter directory for the full list of supported platforms and their configuration options.