Is there a way to get total duration of the call i...
# ask-ai
s
Is there a way to get total duration of the call inside a tool call?
t
Thanks for reaching out, Rashika. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Are you trying to figure out the length of the call itself, the time spent on tool calls, or the total combined duration of both? Let me know so I can give you the best info! Sources: Capturing metrics | LiveKit Docs | pkg/stats/monitor.go
To figure out the total duration of a call, including any tool calls in LiveKit, you can gather metrics from
LLMMetrics
events. Each tool call creates its own metrics event, so by adding up the
duration
from these events along with the main call duration, you’ll get the full picture. Here’s a simple way to do it: • Keep an eye out for
MetricsCollectedEvent
to collect all
LLMMetrics
instances during the session. • Add up the
duration
from each
LLMMetrics
event, including those from tool calls. Here’s an example to help you get started:
Copy code
from livekit.agents import metrics, MetricsCollectedEvent

# Initialize a usage collector
usage_collector = metrics.UsageCollector()

# Listen for metrics events
@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
    usage_collector.collect(ev.metrics)

# After the session ends, get the summary
summary = usage_collector.get_summary()

# Manually sum the durations from all LLM metrics, including tool calls
total_duration = 0.0
for metric in usage_collector.metrics:
    if isinstance(metric, metrics.LLMMetrics):
        total_duration += metric.duration

print(f"Total call duration including tool calls: {total_duration} seconds")
This way, you’ll capture both the LLM response time and any extra time spent on tool calls during the session. If you have any questions or want to dive deeper, just let me know—I’m here to help! Sources: Capturing metrics | LiveKit Docs | pkg/stats/monitor.go
s
Length of the entire conversation.
How can i access metrics inside a tool call?
t
To get metrics inside a tool call in LiveKit, you can simply subscribe to the
metrics_collected
event. This event gives you a
MetricsCollectedEvent
object that holds all the metrics data you need. Here’s a quick example to show you how it works:
Copy code
from livekit.agents import AgentSession, metrics

# Initialize the session
session = AgentSession()

# Define a handler for metrics collection
@session.on("metrics_collected")
def handle_metrics(event: metrics.MetricsCollectedEvent):
    # Access the metrics object
    metrics_data = event.metrics
    # Log or process the metrics
    print(f"Received metrics: {metrics_data}")

# Use the session as needed
The metrics you'll get include things like: • Completion Tokens: Number of tokens generated by the LLM. • Prompt Tokens: Number of tokens in the input prompt. • Total Tokens: Combined tokens from completion and prompt. • Tokens per Second: Rate of token generation. • Timestamp: When the metrics were recorded. • Request ID: Unique identifier for the request. This way, you can keep an eye on and use the metrics easily during your tool calls. If you have any questions or want to dive deeper, just let me know—I’m here to help!