I want the AI to automatically close the conversat...
# ask-ai
g
I want the AI to automatically close the conversation ("That's all for now, thanks!" or something like that) by itself after 3 minutes. How do I accomplish that
t
Thanks for reaching out, Syed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
There are a few supported ways to automatically close a conversation after 3 minutes of inactivity with a custom message. Are you using the LiveKit AgentSession API, working with your own agent framework, or do you have a preferred method among these? • Using the AgentSession with
user_away_timeout
parameter • Integrating a custom inactivity handler class • Utilising a function tool to let the agent close the session • Manual timeout logic in your surrounding code Which environment or method best matches your use case? Sources: livekit-agents/livekit/agents/voice/agent_session.py | agent_extensions/README.md | basics/exit_message.py | AI Agents Framework | livekit/livekit_composite | DeepWiki
g
So im not talking about inactivity. im talking about if a lesson was supposed to be 3 minutes, the user is interacting with it and then at the 2:45 second mark, it kinda just closes up the conversation. is that possible
t
Hey there! Want to set a fixed session time limit that works like clockwork? No problem! You can easily configure a timer that automatically wraps up your session after a specific duration. Let me walk you through a super simple way to do this.
Copy code
async def session_timer():
    await asyncio.sleep(180)  # 3 minutes
    await session.aclose()
    ctx.delete_room()

# Start the timer when the session starts:
timer_task = asyncio.create_task(session_timer())
This approach will gently close out the conversation at the 3-minute mark, even if you're still chatting away. Pro tip: You can also add a friendly heads-up message (like at 2:45) to give a gentle nudge that the session is about to wrap up.
Copy code
async def session_timer_with_warning():
    await asyncio.sleep(165)  # 2:45
    await session.generate_reply(instructions="The session will close in 15 seconds.")
    await asyncio.sleep(15)
    await session.aclose()
    ctx.delete_room()
Just so you know, this timer works totally independently of any inactivity tracking – it's all about that precise session duration limit. Cool, right? Sources: Job lifecycle | LiveKit Docs | .github/next-release/changeset-8a44d9bf.md