Track Users in Traces
Tracking user info in your traces for observability
Overview
You can track user interactions with your LLM app by setting the user ID in a trace. This allows you to track things such as how much tokens each user is costing you, who interacted with your LLM app the most, etc.
Set Users At Runtime
You can use update_current_trace to set the user_id within traces:
from deepeval.tracing import observe, update_current_trace
from openai import OpenAI
client = OpenAI()
@observe()
def llm_app(query: str):
res = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": query}]
).choices[0].message.content
update_current_trace(user_id="your-user-id")
return res
llm_app("Write me a poem.")The user_id can be any string, including the actual IDs of customers in your own database, or even their email addresses. Everything will be viewable and searched in the UI.
You can use updateCurrentTrace to set the userId within traces:
import { observe, updateCurrentTrace } from 'deepeval/tracing';
import OpenAI from 'openai';
const llmApp = async (query: string) => {
const openai = new OpenAI();
const res = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: query }]
})
updateCurrentTrace({ userId: "your-user-id" });
return res.choices[0].message.content;
};
const observedLlmApp = observe({fn: llmApp});
observedLlmApp("Write me a poem.");The userId can be any string, including the actual IDs of customers in your own database, or even their email addresses. Everything will be viewable and searched in the UI.
Last updated on