Documentation
Getting Started
Integrations
JavaScript
Python
LangChain
API
Others
Features
Observability
Prompts
Threads
Evaluations
Radars
Users
Feedback
Tags
More
Security
Concepts
Self-hosting
User Tracking
Identify your users, track their cost, conversations and more.
The strict minimum to enable user tracking is to report a userId
, however you can report any property you'd like such as an email or name using an userProps
object.
Tracking users with the backend SDK
Identify OpenAI calls
The easiest way to get started tracking users is to send user data with every OpenAI API call.
chat_completion = client.chat.completions.create(model="gpt-4o",messages=[{"role": "user", "content": "Hello"}],user_id="user123",user_props={ "name": "John" })
If you're using LangChain, you can similarly pass user data as metadata.
handler = LunaryCallbackHandler()chat = ChatOpenAI(callbacks=[handler],metadata={"user_id": "user123"}, # Assigning user ids to models in the metadata)
Advanced: Inject user into context
When tracking traces, you can inject user data into the context using the identify
methods. This will cascade down to all the child runs.
import lunarydef my_agent():# Some AI queries# Everything done in this context will be tracked with the userdef main():# Using identify to inject the user into the contextwith lunary.identify('user123', user_props={"email": "email@example.org"}):my_agent()
Identifying users on the frontend
If you are tracking chat messages or feedback on the frontend, you can use the identify
method to identify the user there.
lunary.identify("user123", {email: 'test@example.org'})
Identifying Threads
If you are using threads to track conversations, you can pass userId
and userProps
to the openThread
method.
const thread = await lunary.openThread({userId: "user123",userProps: { name: "John" },})
User Properties
While you can track any property you'd like, we recommend using the following ones:
Property | Description |
---|---|
name | Name of the user |
email | Email of the user |
avatar | URL to an avatar |
group | Group or company ID the user belongs to |