Interruptions The user can interrupt the agent at ...
# ask-ai
r
Interruptions The user can interrupt the agent at any time, either by speaking with automatic turn detection or via the
session.interrupt()
method. When an interruption occurs, the agent stops speaking and automatically truncates its conversation history to reflect only the speech that the user actually heard before interruption. Example?
t
Thanks for reaching out, Phanos. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to share some helpful examples of using the
session.interrupt()
method in LiveKit to make your experience smoother: • Manual Turn Control: Imagine a push-to-talk setup—here, you can use
session.interrupt()
to gently pause the agent's speech whenever the user starts speaking:
Copy code
session = AgentSession(turn_detection="manual")
session.input.set_audio_enabled(False)

@ctx.room.local_participant.register_rpc_method("start_turn")
async def start_turn(data: rtc.RpcInvocationData):
    session.interrupt()  # Stop agent if speaking
    session.clear_user_turn()
    session.input.set_audio_enabled(True)  # Start listening to user
• Interrupting Before Non-Verbal Tasks: Before diving into a non-verbal task, you can smoothly stop the agent's speech and listening like this:
Copy code
session.interrupt()  # Stop any ongoing speech
session.input.set_audio_enabled(False)  # Stop listening
try:
    await do_job()  # Perform non-verbal job
finally:
    session.input.set_audio_enabled(True)  # Resume listening
• Explicit Interruption: Even if interruptions are usually off, you can still gently interrupt the agent’s speech when needed:
Copy code
handle = session.say("Hello world")
session.interrupt()  # Can interrupt even if allow_interruptions=False
• Agent Handoff: When it’s time to transition to another agent, you can kindly stop the current agent’s speech first:
Copy code
@function_tool
async def story_finished(self, context: RunContext[StoryData]):
    self.session.interrupt()  # Stop ongoing speech
    await self.session.generate_reply(instructions=f"say goodbye to {context.userdata.name}")
    # Proceed to end the call or switch agents
If you have any questions or want me to walk you through more examples, just let me know—I’m here to help! Sources: Turn detection and interruptions | LiveKit Docs | Text and transcriptions | LiveKit Docs | Agent speech and audio | LiveKit Docs | main.py | livekit-agents/livekit/agents/voice/speech_handle.py