important-judge-78504
03/27/2025, 1:15 PMtall-airline-16756
03/27/2025, 1:15 PMtall-airline-16756
03/27/2025, 1:15 PMctx.shutdown()
method. For example:
async def entrypoint(ctx: JobContext):
# do some work
...
# disconnect from the room
ctx.shutdown(reason="Session ended")
• Disconnecting everyone (ending the entire session): If you want to end the session for all participants, you can use the server API's deleteRoom
method. For example:
from livekit import api
async def entrypoint(ctx: JobContext):
# do some work
...
api_client = api.LiveKitAPI(
os.getenv("LIVEKIT_URL"),
os.getenv("LIVEKIT_API_KEY"),
os.getenv("LIVEKIT_API_SECRET"),
)
await api_client.room.delete_room(api.DeleteRoomRequest(
room=ctx.job.room.name,
))
The deleteRoom
method will send a "Disconnected" room event to all participants and remove the room from the server.
Sources: Inside a session | LiveKit Docsimportant-judge-78504
03/27/2025, 1:17 PMtall-airline-16756
03/27/2025, 1:17 PMctx.shutdown()
method to disconnect the agent from the room while allowing other participants to continue. For example:
async def entrypoint(ctx: JobContext):
# do some work
...
# disconnect from the room
ctx.shutdown(reason="Session ended")
• Removing a specific participant: If you want to remove the agent (or any specific participant) from the room, you can use the remove_participant
API method. For example:
async def hangup(self):
try:
await self.api.room.remove_participant(api.RoomParticipantIdentity(
room=self.room.name,
identity=self.participant.identity,
))
except Exception as e:
# it's possible that the user has already hung up, this error can be ignored
<http://logger.info|logger.info>(f"received error while ending call: {e}")
• Ending the call for everyone: If the task is complete and you want to terminate the entire session, you can use the server API's deleteRoom
method. For example:
from livekit import api
async def entrypoint(ctx: JobContext):
# do some work
...
api_client = api.LiveKitAPI(
os.getenv("LIVEKIT_URL"),
os.getenv("LIVEKIT_API_KEY"),
os.getenv("LIVEKIT_API_SECRET"),
)
await api_client.room.delete_room(api.DeleteRoomRequest(
room=ctx.job.room.name,
))
For a SIP call, the remove_participant
method is particularly useful if you want to hang up the phone call for the agent while keeping the room active for other participants.
Sources: Inside a session | LiveKit Docs | Making calls using SIP | LiveKit Docsimportant-judge-78504
03/27/2025, 1:24 PMtall-airline-16756
03/27/2025, 1:24 PMappend
method on a ChatContext
object to add messages to the conversation context. For example:
chatContext.append({
role: ChatRole,
text: "Your message text here",
images: [optional image objects]
})
• Accessing and Modifying an Existing Agent Session: You can retrieve and modify the chat context of an existing session using the following methods:
# Get a copy of the current chat context
def chat_ctx_copy(self) -> llm.ChatContext:
return self._session.chat_ctx_copy()
# Modify the context (e.g., append a message)
chat_ctx = agent.chat_ctx_copy()
chat_ctx.append({
"role": "user",
"text": "Manually added message"
})
# Update the session with the modified context
await agent.set_chat_ctx(chat_ctx)
This approach allows you to programmatically add messages or other context to the LLM session, enabling the agent to act on the new information. Let me know if you need further clarification!
Sources: Vision Agent Quickstart | LiveKit Docs | ChatContext | LiveKit Agents | livekit.agents.multimodal API documentationimportant-judge-78504
03/27/2025, 2:26 PMimportant-judge-78504
03/27/2025, 2:26 PMtall-airline-16756
03/27/2025, 2:26 PMcli.run_app
. This might require a deeper understanding of your specific implementation or additional system-level access to manage the agent's session dynamically.
If this is a critical requirement, I recommend consulting with a LiveKit specialist or reviewing the LiveKit API documentation for advanced session management options. Let me know if there's anything else I can assist you with!tall-airline-16756
03/27/2025, 2:26 PMimportant-judge-78504
03/27/2025, 2:58 PM