can i track token used by agent?
# ask-ai
h
can i track token used by agent?
t
Thanks for reaching out, Bhakti. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes, you can track token usage by an agent using the
UsageCollector
class provided by the LiveKit Agents framework. This class tracks various metrics, including: • LLM prompt tokens • LLM completion tokens • TTS (text-to-speech) character count • STT (speech-to-text) audio duration (in seconds) To use it, you can create an instance of
UsageCollector
and pass metrics to it:
Copy code
collector = UsageCollector()
collector(metrics)  # or collector.collect(metrics)
To retrieve the aggregated usage summary, you can use:
Copy code
summary = collector.get_summary()
The
UsageSummary
object will provide the total counts for each metric type tracked during the agent's operation. Sources: livekit.agents.metrics.usage_collector API documentation | livekit.agents.metrics API documentation
h
i want in new version of agent, v1.0
t
The
UsageCollector
class is available in LiveKit Agents v1.0 and is part of the
agents.metrics
module. It allows you to collect and track usage metrics across different agent components, such as: • LLM prompt tokens • LLM prompt cached tokens • LLM completion tokens • TTS (text-to-speech) character count • STT (speech-to-text) audio duration Key features include: • A constructor to initialize a new
UsageCollector
instance • A
collect(metrics)
method to process and update metrics • A
summary
accessor to retrieve a
UsageSummary
object with the collected metrics If you need further assistance with implementing this in v1.0, feel free to ask! Sources: Tree View: | dse-workbench | UsageCollector | LiveKit Agents
h
from where to import
UsageCollector
t
To import the
UsageCollector
in LiveKit Agents v1.0, you can use the following import statement:
Copy code
from livekit.agents.metrics.usage_collector import UsageCollector
This class is located in the
livekit/agents/metrics/usage_collector.py
file and is used to collect and track usage metrics for various components, such as LLM, TTS, and STT. Sources: Tree View: | dse-workbench