Use the `useChatMonitor` hook
The Here’s an example of how you would it into your Chat component.
useChatMonitor hook exposes the following functions:Vercel AI SDK Integration
Effortlessly integrate the Vercel AI SDK into your Next.js app using lunary We’ve built a custom hook that makes tracking your AI-driven chats a breeze.Other frameworks
This assumes you are using Next.js. If you are using another framework, contact us and we’ll help you integrate.Import and Initialize
Import lunary and the AI SDK helper hook, then initialize the monitor with your app ID.
Setup the monitor on the backend
We’ll need to reconcile the OpenAI calls made in the backend, with messages sent from the frontend. To do this, we’ll need to use the backend version of the lunary.
Reconcile messages with OpenAI calls
Once your openai client is monitored, you can use the
setParent method to reconcile the frontend message IDs with the backend call:Full API Function Example
Make sure you’ve enabled
sendExtraMessageFields on the useChat hook so that message IDs are also sent.