i have a user and an agent connected to a room and...
# ask-ai
a
i have a user and an agent connected to a room and the user is detected as away, i want a way to trigger the agent to start talking, how could i do that? can it be via API?
t
Thanks for reaching out, Sebastián. 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 your agent to start chatting when it notices a user has gone away, you can set this up right in the agent’s code using the
user_state_changed
event along with the
user_away_timeout
parameter in
AgentSession
. Just a heads-up, this isn’t something you can control through the external LiveKit API. Here’s a friendly step-by-step on how to get this working: • Set the
user_away_timeout
parameter in
AgentSession
to decide how long the user should be inactive before being marked as away. • Add a listener for the
user_state_changed
event so you know exactly when the user is marked away. • Inside that event handler, call
session.generate_reply()
or
session.say()
to have your agent speak up. Here’s a quick example to illustrate:
Copy code
@session.on("user_state_changed")
def on_user_state_changed(ev: UserStateChangedEvent):
    if ev.new_state == "away":
        asyncio.create_task(session.generate_reply(
            instructions="The user has been inactive. Politely check if they're still there."
        ))
Or, if you’d prefer something simpler, you can try out the
InactivityHandler
from the
agent_extensions
package. For a full working example, feel free to check out the inactive_user.py example. It’s a great resource to get you started! Sources: Events and error handling | LiveKit Docs | examples/avatar_agents/audio_wave/agent_worker.py | agent_extensions/README.md | Dispatching agents | LiveKit Docs
a
so i cannot from the LiveKit API to notify the user agent to start a conversation?
t
You're right, the LiveKit API doesn't have a built-in way to notify the user agent to start a conversation directly. You'll need to handle this in your agent's code using event handling, like the
user_state_changed
event we talked about earlier. If you'd like a hand with setting this up or have any other questions, just let me know—I'm here to help!