How can I restart the user away timeout?
# ask-ai
p
How can I restart the user away timeout?
t
Thanks for reaching out, Jonas. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The user away timeout in LiveKit is automatically managed by the
AgentSession
and resets based on user activity and state changes. Here's how it works: • Automatic Timer Restart: The timeout restarts automatically in the following scenarios: ◦ When the user starts speaking (state changes from "listening" to "speaking"). ◦ When the agent starts speaking (state changes). ◦ When both return to the "listening" state. • Configuration: The timeout is configured when creating an
AgentSession
using the
user_away_timeout
parameter, which defaults to 15 seconds. For example:
Copy code
session = AgentSession(
    user_away_timeout=15.0,  # Set user as "away" after 15 seconds of silence
    # other parameters...
)
• Event Handling: You can listen for the
user_state_changed
event to handle user inactivity:
Copy code
@session.on("user_state_changed")
def _user_state_changed(ev: UserStateChangedEvent):
    if ev.new_state == "away":
        # Handle user inactivity
        pass
The timeout is managed internally and cannot be manually restarted. Any user activity, such as speaking, will automatically cancel and restart the timer as needed. Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...
p
The problem is that when I generate a speech inside a tool call the timeout starts - and if the tool call is a transfer it usually takes up to 30 seconds which leads to the away timeout to trigger
t
Hey there! I totally get how frustrating it can be when a tool or transfer gets interrupted by a timeout. Let me walk you through some friendly ways to handle this. 1. Adjust the Timeout Configuration No worries - you can easily give yourself more breathing room by increasing the
user_away_timeout
when setting up your
AgentSession
. It's like giving your process a bit more time to catch its breath:
Copy code
session = AgentSession(
    user_away_timeout=30.0,  # Set timeout to 30 seconds
    # other parameters...
)
2. Provide User Feedback During Long Operations Think of this like keeping a friend in the loop during a long story. Just make sure they know what's happening: • Give a quick heads up that something's in progress. • Drop a friendly update if things are taking a bit longer. • Explain any delays so they know everything's cool and working. 3. Handle User Away Events It's like checking in with a friend to make sure they're still around. Here's how you can do that:
Copy code
@session.on("user_state_changed")
def _user_state_changed(ev: UserStateChangedEvent):
    if ev.new_state == "away":
        # Handle user inactivity
        pass
    else:
        # User is active again
        pass
4. Example Implementation Here's a friendly way to check in if someone seems to have stepped away:
Copy code
async def user_presence_task():
    # Try to re-engage the user 3 times
    for _ in range(3):
        await session.generate_reply(
            instructions=(
                "The user has been inactive. Politely check if the user is still present."
            )
        )
        await asyncio.sleep(10)

    # If no response, close the session
    await asyncio.shield(session.aclose())
    ctx.delete_room()

@session.on("user_state_changed")
def _user_state_changed(ev: UserStateChangedEvent):
    nonlocal inactivity_task
    if ev.new_state == "away":
        inactivity_task = asyncio.create_task(user_presence_task())
        return

    # User is active again - cancel re-engagement
    if inactivity_task is not None:
        inactivity_task.cancel()
By being a bit flexible with timeouts, keeping communication open, and staying attentive to user activity, you'll create a smoother, more user-friendly experience. No more unexpected interruptions! Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime... | Events and error handling | LiveKit Docs | Handling RAG Delays in Voice Agents | LiveKit Docs