How to control when the agent speaks again after a...
# ask-ai
f
How to control when the agent speaks again after an interruption
t
Thanks for reaching out, Amjad. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you want to control when the agent speaks again after being interrupted, here’s a simple way to do it: • Use the
session.interrupt()
Method:
This will pause the agent’s current speech so the user can jump in. • Resume Speaking with `session.generate_reply()`: Once the interruption is over, you can have the agent pick up by generating a new reply. This usually happens inside an event handler that listens for interruptions. • Adjust Interruption Settings: You can tweak parameters like
min_interruption_duration
and
min_interruption_words
in
AgentSession
to decide what counts as an interruption. Here’s a little example showing how to get the agent talking again after an interruption:
Copy code
# Inside your agent's event handler
@session.on("agent_state_changed")
async def handle_agent_state(ev: AgentStateChangedEvent):
    if ev.new_state == "listening" and ev.old_state == "speaking":
        # Generate a new reply to resume speaking
        session.generate_reply(instructions="Thank you for interrupting. Please continue.")
Also, you can fine-tune how interruptions work by setting these parameters when you create the `AgentSession`:
Copy code
session = AgentSession(
    agent_false_interruption_timeout=4.0,
    min_interruption_duration=0.5,
    min_interruption_words=3,
)
This way, the agent knows exactly when to pause and when to jump back in, making the conversation flow smoothly based on what your app needs.