Can I configure the agent to also to print the con...
# ask-ai
t
Can I configure the agent to also to print the connection minutes and bandwidth? How can I get the connection minutes and bandwidth details for session to bill my customers?
t
Thanks for reaching out, Renold. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! You can easily check out connection minutes and bandwidth details for a session in LiveKit using a few handy methods: • LiveKit Cloud Analytics Dashboard: This dashboard gives you a clear view of session-level metrics, like total connection minutes and bandwidth usage. Plus, you can look at data over specific time ranges to get the insights you need. • Analytics API: If you prefer, you can use the API to grab detailed session info. For example, just make a GET request to this endpoint:
<BASE_API_URL>/api/project/{PROJECT_ID}/sessions/{SESSION_ID}
. • Programmatic Access via SDKs: Another option is the
get_rtc_stats()
method in the LiveKit RTC SDKs. It helps you collect WebRTC stats, including publisher and subscriber info, which is great for figuring out bandwidth usage and connection minutes. If you want me to walk you through any of these or need more details, just let me know—I’m here to help! Sources: Say hello to improved Cloud analytics | livekit-rtc/livekit/rtc/room.py | livekit-ffi/src/server/requests.rs | packages/livekit-rtc/src/proto/room_pb.ts | Say hello to improved Cloud analytics
t
Tell me more about the below. • Programmatic Access via SDKs: Another option is the
get_rtc_stats()
method in the LiveKit RTC SDKs. It helps you collect WebRTC stats, including publisher and subscriber info, which is great for figuring out bandwidth usage and connection minutes. Can I call it from agent? or how to get this
Tell me more about the below. • Programmatic Access via SDKs: Another option is the
get_rtc_stats()
method in the LiveKit RTC SDKs. It helps you collect WebRTC stats, including publisher and subscriber info, which is great for figuring out bandwidth usage and connection minutes. Can I call it from agent? or how to get this
t
Hey there! You can use the
get_rtc_stats()
method from an agent in LiveKit to grab some useful RTC statistics, like publisher and subscriber stats. It’s available right on the Room object. Here’s a quick example to show you how it works:
Copy code
async def entrypoint(ctx: JobContext):
    session = AgentSession(llm=openai.realtime.RealtimeModel())
    await session.start(Agent(instructions="You are a helpful assistant"), room=ctx.room)

    # Log the session stats every 5 minutes
    while True:
        rtc_stats = await ctx.room.get_session_stats()

        all_stats = chain(
            (("PUBLISHER", stats) for stats in rtc_stats.publisher_stats),
            (("SUBSCRIBER", stats) for stats in rtc_stats.subscriber_stats),
        )

        for source, stats in all_stats:
            stats_kind = stats.WhichOneof("stats")
            <http://logger.info|logger.info>(f"RtcStats - {stats_kind} - {source}", extra={"stats": MessageToDict(stats)})

        await asyncio.sleep(5 * 60)
Just a heads-up: make sure the room is connected before you call this method, otherwise it’ll throw a RuntimeError. If anything’s unclear or you want a hand setting this up, just let me know—I’m here to help! Sources: livekit-rtc/livekit/rtc/_proto/stats_pb2.pyi | examples/primitives/room_stats.py
Absolutely! You can call the
get_rtc_stats()
method from an agent in LiveKit. This handy method is part of the Room object and gives you RTC statistics, including both publisher and subscriber stats. These stats are super useful for figuring out bandwidth usage and connection minutes. Here’s a quick example to show you how it works:
Copy code
async def entrypoint(ctx: JobContext):
    session = AgentSession(llm=openai.realtime.RealtimeModel())
    await session.start(Agent(instructions="You are a helpful assistant"), room=ctx.room)

    # Log the session stats every 5 minutes
    while True:
        rtc_stats = await ctx.room.get_session_stats()

        all_stats = chain(
            (("PUBLISHER", stats) for stats in rtc_stats.publisher_stats),
            (("SUBSCRIBER", stats) for stats in rtc_stats.subscriber_stats),
        )

        for source, stats in all_stats:
            stats_kind = stats.WhichOneof("stats")
            <http://logger.info|logger.info>(f"RtcStats - {stats_kind} - {source}", extra={"stats": MessageToDict(stats)})

        await asyncio.sleep(5 * 60)
Just a heads-up: make sure your room is connected before calling this method, because it will raise a RuntimeError if it’s not. If you have any questions or want me to walk you through it, just let me know—I’m here to help!