I have a livekit agent worker server, where agent ...
# ask-ai
f
I have a livekit agent worker server, where agent is instructed to interview users, We are doing a timebound interview. What is the best way of keeping the agent aware of how much time is left in the interview, so that it incorporates that detail while interviewing?
t
Thanks for reaching out, Tushar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit doesn’t have a built-in feature to keep the agent worker server updated on the remaining session or interview time, but no worries—you can totally set this up yourself! Here’s how: • Keep track of when the session starts, right when the interview kicks off. • Set up a background task that regularly checks how much time is left based on the total session length you decide on. • Use that task to gently remind the agent or user about the time left, maybe with verbal cues or other friendly prompts. For instance, you could have a timer send messages like "You have 5 minutes remaining" or trigger something when the session wraps up. If you want, I’m here to help you get this going—just let me know! Sources: Handling RAG Delays in Voice Agents | LiveKit Docs | External data and RAG | LiveKit Docs | observability/sessiontimer.go | livekit-agents/livekit/agents/voice/agent_session.py | External data and RAG | LiveKit Docs
f
I have to push this "You have 5 minutes remaining" thing as a context in the agent's context history, If yes, what role should I use. Can you give the python code snippet of how exactly to do this in a python agent worker.
t
Hey there! To get that "You have 5 minutes remaining" notification into the agent's context history, the best way is to use the "assistant" role. This way, the message shows up as if the agent said it, keeping the conversation flow nice and clear. Here’s a simple way to do it: • Use the
on_user_turn_completed
node or something similar to add the notification into the context. • Call
turn_ctx.add_message(role="assistant", content="You have 5 minutes remaining.")
to insert the message. • Make sure to save the changes by calling
update_chat_ctx(turn_ctx)
right after you update the context. If you want, I’m happy to help you set this up or even create a timer-based system to estimate the time. Just let me know! Sources: Voice pipeline nodes | LiveKit Docs | External data and RAG | LiveKit Docs | complex-agents/nutrition-assistant/nutrition-assistant-frontend/components/... | Pipeline nodes and hooks | LiveKit Docs
f
What if i give role as system or developer, will that be more effective?
t
Hey there! Just to clarify, the "system" role is usually meant for high-level instructions or dynamic context that helps guide how the LLM behaves—think operational settings or game states. It’s not really designed for user-facing notifications like letting someone know how much time is left. The "developer" role is valid but less commonly used or documented for specific tasks, and how it works can vary depending on how the LLM interprets roles. When it comes to time-based notifications, the "assistant" role is your best bet. It fits perfectly with updates meant for users and makes sure the message feels like it’s coming from the agent within the conversation. Using "system" or "developer" roles for these kinds of notifications probably won’t make them more effective and might even cause the LLM to handle the context in unexpected ways. If you want, I’m here to help with any more questions or to walk you through setting this up! Sources: ChatRole | LiveKit Agents | Handling RAG Delays in Voice Agents | LiveKit Docs | External data and RAG | LiveKit Docs | External data and RAG | LiveKit Docs | complex-agents/role-playing/agents/base_agent.py
f
alright agreed. Does using the
add_message()
as suggested by you will trigger the assistant to speak the provided message?
t
Just a heads-up: using
add_message()
won’t make the assistant actually say the message out loud. It simply adds the message to the chat history for internal tracking. If you want the assistant to speak the message aloud, you’ll want to use
session.say()
or
generate_reply()
, as these are built to trigger the Text-to-Speech (TTS) system and get that audio going. Feel free to ask if you'd like me to show you an example of how to do this—I’m happy to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Agent speech and audio | LiveKit Docs
f
Then how will it let the user know about it?
t
To make sure the user actually hears the message, you can use
session.say()
or
generate_reply()
. These handy methods kick off the assistant's Text-to-Speech (TTS) system, so the message gets spoken out loud. For example:
Copy code
await session.say("You have 5 minutes remaining.")
This way, the user won’t miss the notification. If you want, I’m here to help you set this up or answer any questions!