clever-apartment-50437
09/21/2025, 5:44 PMtall-airline-16756
09/21/2025, 5:44 PMtall-airline-16756
09/21/2025, 5:44 PMclever-apartment-50437
09/21/2025, 5:45 PMtall-airline-16756
09/21/2025, 5:46 PMdelete_room API. This handy API will end the room and disconnect everyone at once, making it super convenient.
Here’s a Python example to guide you through it:
import asyncio
from livekit import api
from livekit.agents import get_job_context, JobContext
async def hangup_after_15_seconds():
# Wait for 15 seconds
await asyncio.sleep(15)
# Get the current job context
ctx = get_job_context()
if ctx is None:
print("No job context available.")
return
# Initialize the LiveKit API client
api_client = api.LiveKitAPI()
try:
# Delete the room to hang up for all participants
await api_client.room.delete_room(
api.DeleteRoomRequest(
room=ctx.room.name,
)
)
print(f"Room {ctx.room.name} has been deleted.")
except Exception as e:
print(f"Failed to delete room: {e}")
finally:
api_client.close() # Clean up the API client
async def entrypoint(ctx: JobContext):
# Connect the agent to the room
await ctx.connect(auto_subscribe=api.AutoSubscribe.AUDIO_ONLY)
# Start the 15-second countdown to hang up
asyncio.create_task(hangup_after_15_seconds())
print("Call in progress. Will end in 15 seconds for all participants.")
Just a quick reminder to make sure your environment has these credentials set up:
• `LIVEKIT_URL`: Your LiveKit server WebSocket URL
• `LIVEKIT_API_KEY`: API key with room management permissions
• `LIVEKIT_API_SECRET`: Corresponding API secret
If you'd like, I’d be more than happy to help you adapt this example for another programming language or a different use case. Just let me know!
Sources: Inside a session | LiveKit Docs | Inside a session | LiveKit Docs | SIP Integration | livekit/livekit_composite | DeepWiki | Connecting to LiveKit | LiveKit Docs | Remote method calls (RPC) | LiveKit Docsclever-apartment-50437
09/21/2025, 5:53 PMtall-airline-16756
09/21/2025, 5:53 PMapi.LiveKitAPI() to help you implement the auto hang-up feature smoothly. Here’s a quick rundown of the steps:
1. Wait for 15 seconds using asyncio.sleep(15).
2. Initialize the LiveKit API client with api.LiveKitAPI().
3. Call the delete_room method using the room name to disconnect all participants.
Just a friendly reminder to double-check that your environment variables (LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET) are set up correctly so the API client works seamlessly.
If you’d like, I’m here to help with any part of the setup or to clarify anything further—just let me know!clever-apartment-50437
09/21/2025, 5:54 PMtall-airline-16756
09/21/2025, 5:55 PMapi.LiveKitAPI. You can achieve this by using the ctx.api.room.delete_room() method available through the agent's job context. This is a convenient way to end the room session and disconnect everyone without having to set up an API client manually.
Here’s a simple example for you to try:
from livekit import api
from livekit.agents import get_job_context
async def auto_hangup_all():
# Get current job context
ctx = get_job_context()
if ctx is None:
return # Not running inside an agent job
# Delete the room to disconnect all participants
await ctx.api.room.delete_room(
api.DeleteRoomRequest(
room=ctx.room.name,
)
)
This approach makes sure everyone is disconnected right away, using the built-in API client that’s already part of the agent runtime. So, you won’t need any extra credentials or setup.
If you’d like, I’m here to help you integrate this smoothly into your workflow or answer any questions you might have!
Sources: Connecting to LiveKit | LiveKit Docs | examples/warm-transfer/warm_transfer.py | Agents telephony integration | LiveKit Docs | Agents telephony integration | LiveKit Docs | Job lifecycle | LiveKit Docsclever-apartment-50437
09/21/2025, 5:57 PMtall-airline-16756
09/21/2025, 5:58 PMuser_away_timeout parameter in the AgentSession class. This parameter helps detect when a user has been inactive for 15 seconds and triggers a user_state_changed event. From there, you can handle this event to disconnect the room smoothly.
Here’s a simple example to guide you:
from livekit.agents import AgentSession, JobContext
async def entrypoint(ctx: JobContext):
session = AgentSession(user_away_timeout=15.0)
@session.on("user_state_changed")
def _user_state_changed(event):
if event.new_state == "away":
# Auto hang-up after inactivity
ctx.room.disconnect()
await session.start()
This setup ensures the room disconnects after 15 seconds of inactivity. If you'd like, you can also notify the user before disconnecting by sending a friendly message or prompt.
Would you like me to help you with adding user notifications or any other customization? I'm here to support you!
Sources: agent_extensions/README.md | Connecting to LiveKit | LiveKit Docs | Events and error handling | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | Agent Testing & Evaluation | livekit/livekit_composite | DeepWiki