What I want is to capture metrics the start time a...
# ask-ai
b
What I want is to capture metrics the start time and end time for each conversation item. How can I do that?
t
Thanks for reaching out, Harsh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to track the start and end times of each conversation item in LiveKit, you can use the
EventMetric
structure from LiveKit’s metrics system. It’s got these handy
start_timestamp_ms
and
end_timestamp_ms
fields that log the times in milliseconds. Here’s a simple way to do it: 1. When a conversation item kicks off, grab the current time in milliseconds and save it as
start_timestamp_ms
. 2. Once the conversation item wraps up, record the current time again as
end_timestamp_ms
. 3. Then, create an
EventMetric
object with those timestamps and add it to your metrics batch. Check out this example code snippet:
Copy code
from livekit.agents import metrics
import time

# When a conversation item starts
start_time = int(time.time() * 1000)  # Current time in milliseconds

# When the conversation item ends
end_time = int(time.time() * 1000)

# Create the EventMetric for this conversation item
conversation_metric = metrics.EventMetric(
    label=your_label,
    participant_identity=your_participant_id,
    track_sid=your_track_id,
    start_timestamp_ms=start_time,
    end_timestamp_ms=end_time,
    metadata="conversation_item"
)

# Add it to your metrics batch
metrics_batch.events.append(conversation_metric)
If you want to dive deeper, the LiveKit Metrics Documentation has all the details you might need. Feel free to reach out if you have any questions!