how to set user timeout if user is not speaking mo...
# ask-ai
c
how to set user timeout if user is not speaking more than 10second in voice agent
t
Thanks for reaching out, N. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! To help you set a user timeout when someone hasn’t spoken for more than 10 seconds in a LiveKit voice agent, you can use the
user_away_timeout
parameter in the
AgentSession
configuration. This handy feature changes the user’s state to "away" after a set period of inactivity, which you can then use to gently prompt the user or disconnect them if needed. Method 1: Using
user_away_timeout
in
AgentSession
(Recommended)
Simply set
user_away_timeout=10.0
when you create your
AgentSession
. Then, listen for the
user_state_changed
event to know when the user becomes "away" and respond accordingly.
Copy code
from livekit.agents import AgentSession, UserStateChangedEvent

session = AgentSession(
    vad=silero.VAD.load(),
    llm=openai.LLM(),
    stt=deepgram.STT(),
    tts=cartesia.TTS(),
    user_away_timeout=10.0,  # Timeout after 10 seconds of inactivity
)

@session.on("user_state_changed")
async def on_user_state_changed(ev: UserStateChangedEvent):
    if ev.new_state == "away":
        # Prompt the user or end the session
        await session.generate_reply(
            instructions="Are you still there? Let me know if you need help."
        )
        # Optionally close the session after a few attempts
Method 2: Using the
InactivityHandler
Extension
LiveKit also offers a reusable
InactivityHandler
in the
agent-demos
repository. This extension automatically monitors for silence and sends a message after the timeout period, making things even easier for you.
Copy code
from agent_extensions.handlers import InactivityHandler

inactivity_handler = InactivityHandler(
    timeout_seconds=10,
    inactivity_message="Are you still there? Let me know if you need any help."
)
inactivity_handler.start(agent)
Both of these options give you the flexibility to customize how long the timeout lasts and what message to send. If you have any questions or need a hand setting this up, just let me know—I’m here to help! Sources: Events and error handling | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | agent_extensions/README.md | Events and error handling | LiveKit Docs