future-umbrella-45124
08/20/2025, 4:18 PMtall-airline-16756
08/20/2025, 4:18 PMtall-airline-16756
08/20/2025, 4:19 PMstart_turn
method is usually an RPC method that clients call directly. But here’s a neat trick: the agent worker can actually trigger similar actions on its own by keeping an eye on the agent's state and using session methods.
For instance, you can listen for the AgentStateChangedEvent
to catch when the agent switches from "speaking" to "idle" and then turn on audio input automatically like this:
@session.on("agent_state_changed")
def on_agent_state_changed(ev: AgentStateChangedEvent):
if ev.new_state == "idle":
# Agent finished speaking and is now idle
session.input.set_audio_enabled(True)
This way, the agent worker gets ready for the next user turn right after the agent stops talking. If you want, I’m here to help you set this up or answer any questions!
Sources: Turn detection and interruptions | LiveKit Docs | livekit-agents/livekit/agents/voice/audio_recognition.py | Turn detection and interruptions | LiveKit Docsfuture-umbrella-45124
08/20/2025, 4:23 PMtall-airline-16756
08/20/2025, 4:25 PM// Client-side JavaScript example
room.localParticipant.registerRpcMethod("your_turn_to_speak", async (data) => {
// Enable microphone button or show "Your turn to speak" UI
enableMicrophoneButton();
showTurnIndicator("Your turn to speak!");
});
1. Notify the client from the agent: Once the agent finishes speaking, you can call the client’s RPC method like this:
async def notify_client_turn():
for participant in ctx.room.remote_participants.values():
try:
await participant.perform_rpc(
method="your_turn_to_speak",
payload="",
response_timeout=5.0
)
except Exception as e:
logger.warning(f"Failed to notify participant {participant.identity}: {e}")
1. Trigger the notification: Use the agent’s state changes to know when they’ve finished speaking, then notify the client:
@session.on("agent_state_changed")
def on_agent_state_changed(event: AgentStateChangedEvent):
if event.state == "listening": # Agent finished speaking
asyncio.create_task(notify_client_turn())
This way, the client will get a friendly nudge when it’s their turn to jump in. If you want me to walk you through any part of this or have questions, just let me know—I’m here to help!
Sources: Turn detection and interruptions | LiveKit Docs | livekit-agents/livekit/agents/voice/audio_recognition.pyfuture-umbrella-45124
08/20/2025, 4:31 PMtall-airline-16756
08/20/2025, 4:33 PMctx.room.local_participant.register_rpc_method(
"method_name",
handler_function
)
These methods let you handle requests from the agent and send back responses. It’s a great way to test everything, like letting the client know when it’s their turn to speak. If you want, I’m here to help you get this all set up!
Sources: rpc/rpc_agent.py | RPC | LiveKit Docs | livekit-rtc/livekit/rtc/participant.pyfuture-umbrella-45124
08/20/2025, 4:35 PMtall-airline-16756
08/20/2025, 4:36 PMasync def handle_my_method(data: RpcInvocationData) -> str:
try:
# Process the request
payload = json.loads(data.payload)
# Your logic here
return json.dumps({"success": True, "result": "some_data"})
except Exception as e:
return json.dumps({"success": False, "error": str(e)})
1. Register the RPC method: After connecting to the room, use the register_rpc_method
function like this:
ctx.room.local_participant.register_rpc_method(
"agent.myMethod",
handle_my_method
)
1. Organize your code: To keep things nice and tidy, you can create separate functions to register multiple RPC methods:
def register_rpc_handlers(room):
room.local_participant.register_rpc_method(
"get_order_state",
handle_get_order_state
)
Just remember to register your methods after you’ve connected to the room but before kicking off the agent session. If you get stuck or want to chat about any part of this, I’m here to help!
Sources: Remote method calls (RPC) | LiveKit Docs | complex-agents/teleprompter/cartesia-ink.py | livekit-rtc/livekit/rtc/participant.py