is there a way in AgentSession to enforce a hangup...
# ask-ai
m
is there a way in AgentSession to enforce a hangup when there's 30 seconds of inactivity/silence?
t
Thanks for reaching out, Anas. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking if AgentSession can enforce a hangup after 30 seconds of inactivity/silence. You can—there's no built-in automatic hangup, but you can implement it easily with a small monitoring task. What to do (high level) • Track when the user last spoke (handle the
user_started_speaking
event and update a timestamp). • Run a background task that periodically checks the timestamp. • If silence exceeds 30 seconds, call
await ctx.room.disconnect()
to hang up. Minimal pattern (Python)
Copy code
import asyncio, time

SILENCE_THRESHOLD = 30

async def entrypoint(ctx: JobContext):
    last_spoke = time.time()

    @agent.on("user_started_speaking")
    def on_user_started_speaking(_msg):
        nonlocal last_spoke
        last_spoke = time.time()

    async def monitor():
        while True:
            if time.time() - last_spoke > SILENCE_THRESHOLD:
                await ctx.room.disconnect()
                break
            await asyncio.sleep(1)

    monitor_task = asyncio.create_task(monitor())
    session = AgentSession(...) 
    await session.start(room=ctx.room, agent=agent)

    @ctx.on("shutdown")
    def on_shutdown():
        monitor_task.cancel()
Notes •
AgentSession(user_away_timeout=...)
changes when
UserState.away
is set (default 15s) but does not automatically disconnect; you still need the custom hangup logic. • This same pattern is used for voicemail/silence detection use cases. Would you like the snippet adapted to your existing agent code or to a different language? Sources: How to detect and handle silence during calls | examples/frontdesk | livekit/livekit_composite | DeepWiki | Events and error handling | LiveKit docs