Get started with AI replies in Comments using Liveblocks and Next.js
Liveblocks is a realtime collaboration infrastructure for building performant
collaborative experiences. Follow the following steps to add an AI agent that
replies in Comments when mentioned in a
thread, using @liveblocks/node, the
Vercel AI SDK, and Anthropic, in
your Next.js /app directory application.
Quickstart
Have a Comments app ready
To add AI replies to comment thread, you first need to have a Liveblocks Comments app set up with secret key authentication and resolved users. Open up your app, or set up comments if you haven’t already.
Get started with comments
Install dependencies
Install
@liveblocks/nodeto verify webhooks and write comments, along with the Vercel AI SDK and the Anthropic provider to generate AI responses.TerminalAdd your environment variables
Create a new
.env.localfile and add your Liveblocks secret key from the dashboard, your Anthropic API key from the Anthropic dashboard, and a placeholder for your Liveblocks webhook secret. You’ll create the webhook secret in the final step..env.localAdd an AI user to your database
The AI agent posts replies like a regular user, so it needs a user ID and info. Add a dedicated user for your agent next to your real users. Where you resolve user info in
resolveUsersandresolveMentionSuggestions, you should return this AI user so it can be@-mentioned and rendered in threads.Find where you fetch data in your resolver functions……and modify the return values to include an AI userCreate the webhook endpoint
Create an API route to receive Liveblocks webhooks. We’ll verify the request, only respond to
commentCreatedevents, and ignore comments that weren’t written by a human, otherwise the AI would reply to its own messages.app/api/liveblocks-webhook/route.tsCreate an AI reply
Next, create the AI response. There are a few steps to follow for a full user experience:
- Get the thread with
getThread - Check if the AI user was
@-mentioned withgetMentionsFromCommentBody. - Add a “👀” reaction to the comment that mentioned the user with
addCommentReaction. - Show AI presence in avatar stacks and
useOtherswithsetPresence. - Create a placeholder comment, “Thinking…”, with
createComment. - Convert the thread into chat messages and generate a response from Claude.
- Update the placeholder comment with the AI response using
editComment.
app/ai-comment-reply.ts- Get the thread with
Set up Liveblocks webhooks
The final step is to configure Liveblocks webhooks so the AI is notified when new comments are created.
- Follow the guide on testing webhooks locally.
- In the dashboard, create a webhook endpoint that
points to
/api/liveblocks-webhookand enables thecommentCreatedevent. - Copy your webhook secret (
whsec_...) and add it to.env.localasLIVEBLOCKS_WEBHOOK_SECRET_KEY.
Now whenever a user
@-mentions your AI in a thread, your endpoint will generate a response and post it back as a reply.Set up webhooks
Complete!
You now have an AI agent capable of replying to mentions in comment threads. When it’s mentioned in a acomment, it’ll leave a placeholder comment, and edit it after generating a response.
(Optional) Add streaming to AI replies
To stream the AI’s response into the comment in realtime, like in the AI Comments example, we can take advantage of Feeds, streaming each chunk of reasoning and writing into the comment as it arrives.
To get started, type your data in
liveblocks.config.tsso theCommentMetadata, feed, and feed message types are available across your app.Next, extend the endpoint to write streaming updates into the feed.
In React, render the streaming feed by creating an
AiCommentcomponent that reads fromuseFeedMessages. It renders live status updates from the feed, including reasoning and the final response, and then switches back to the defaultCommentonce the placeholder has been edited.Finally, import
AiCommentinto your threads UI and pass it toThreadvia thecomponents.Commentoverride. Placeholder comments created by the workflow carry afeedIdin their metadata, which is how you know when to render the streaming view instead of the default one.
What to read next
Congratulations! You’ve set up an AI agent that replies to mentions in Liveblocks Comments threads.
- @liveblocks/node API reference
- Comments overview
- Webhooks documentation
- How to test webhooks on localhost