Skip to main content
1

Import

Install the package and import the necessary functions from our React export:
2

Initialize lunary

Initialize the SDK with your application’s tracking ID:
3

Use the `useChatMonitor` hook

The useChatMonitor hook exposes the following functions:
Here’s an example of how you would it into your Chat component.
4

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.
1

Import and Initialize

Import lunary and the AI SDK helper hook, then initialize the monitor with your app ID.
2

Wrap the useChat hook

3

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.
4

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.