Usage with React
Import
Install the package and import the necessary functions from our React export:
Initialize lunary
Initialize the SDK with your application’s tracking ID:
Use the `useChatMonitor` hook
The useChatMonitor
hook exposes the following functions:
Here’s an example of how you would it into your Chat component.
Reconcile calls on your backend
Make sure to pass the message IDs to your backend to reconcile with the backend calls and agents.
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.
Wrap the useChat hook
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.