Can you give me an example on how disallow_interru...
# ask-ai
f
Can you give me an example on how disallow_interruptions should be used?
t
Thanks for reaching out, Abdullah. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking for an example of how to use
disallow_interruptions
. Below is a concise explanation and a practical example. What it does • Prevents user interruptions while a tool/function is executing critical or non‑interruptible work. • Applies only for the duration of that tool's execution. • Use sparingly for operations that must finish or cannot be safely rolled back. Example (Python-style function tool)
Copy code
@function_tool()
async def perform_search(context: RunContext, query: str) -> str:
    # Prevent user from interrupting this critical operation
    context.disallow_interruptions()

    # Long-running or non-interruptible work
    result = await long_running_search(query)

    return result
When to use it • Saving critical state, committing transactions, or performing external calls that must complete. • Avoid using it for routine or short operations; provide status updates instead for long-running but interruptible tasks. If you want, I can: • Show an example with status updates before/after
disallow_interruptions()
• Convert the example to your code style or language (Node/TS, etc.) Sources: External data and RAG | LiveKit Docs
f
If my function tool calls subsequent session.say() or session.generate_reply(), will I have to set allow_interruptions false here? If so, is this because the current speech handle is updated?
t
You're asking whether you must set
allow_interruptions=False
when a function tool calls
session.say()
or
session.generate_reply()
, and whether that requirement is related to the speech handle being updated. Short answer: • You only need to set
allow_interruptions=False
if you want that specific speech output to be uninterruptible by the user. • This is a per-call setting on each speech invocation, not because the speech handle is updated. Details: • Each
session.say()
/
session.generate_reply()
call creates a new SpeechHandle and evaluates
allow_interruptions
for that handle. • By default interruptions are allowed (
allow_interruptions=True
). Set
False
on the call when you want to prevent barge‑in for that segment (e.g., critical announcements). • Even with
allow_interruptions=False
, you can still programmatically cancel via
session.interrupt()
or
handle.interrupt()
. If you want, I can: • Show a short code example (Python or Node) demonstrating an uninterruptible
session.say()
followed by an interruptible one. • Explain patterns for when to use
disallow_interruptions()
at the tool level vs
allow_interruptions=False
on speech calls. Sources: Turn detection and interruptions | LiveKit Docs | Agent speech and audio | LiveKit docs
f
I just need clarification. In the original function you provided that calls disallow_interruptions(), if I were to have a session.say() would I also have to ensure that the session.say() is not interruptable with allow_interruptions=False?
t
Short answer: No — you only need to set
allow_interruptions=False
on
session.say()
if you want that specific speech to be uninterruptible.
context.disallow_interruptions()
and
allow_interruptions
control different things: •
context.disallow_interruptions()
(called inside a function/tool) prevents the tool's execution from being interrupted by user input for the duration of the tool. •
session.say()
/
session.generate_reply()
each create a new SpeechHandle and evaluate
allow_interruptions
per call. If you want the spoken output produced by that call to be uninterruptible by the user, call it with
allow_interruptions=False
. So if your function calls
context.disallow_interruptions()
and then does
session.say()
, the tool is already protected from user interruptions while running, but the speech itself will still be interruptible unless you explicitly call
session.say(..., allow_interruptions=False)
.
f
When would I use session.interrupt()?
t
You're asking when to use
session.interrupt()
. Use it when you need to forcibly stop the agent's current speech or queued responses under program control. Key cases: • To immediately stop speech in response to an external event (UI button, RPC, or signal) regardless of VAD or
allow_interruptions
. • To implement custom turn-taking (push-to-talk, explicit user start) where you programmatically end the agent's turn before enabling user input. • To cancel long-running work when the user or system indicates you should stop (e.g., cancel an HTTP request if
handle.interrupted
is set). • To override an uninterruptible speech (
allow_interruptions=False
) when you must stop anyway. Notes: • You don't need
session.interrupt()
for normal user barge-in if VAD is enabled and
allow_interruptions=True
— the framework will interrupt automatically. •
session.interrupt()
affects speech immediately and clears queued responses; tools should handle cleanup if interrupted. If you want, I can show a short code example (TS or Python) demonstrating a frontend RPC triggering
session.interrupt()
. Which language do you prefer? Sources: Turn detection and interruptions | LiveKit Docs | Agent speech and audio | LiveKit docs | Tool definition and use | LiveKit docs
f
# Prevent any new user turns self.session.update_options(_turn_detection_="manual") self.turn_detection = "manual" Which is the correct way to prevent any new user turns. I'm calling an irreversible function, that should not detect any turns afterwards?
t
Use session.update_options(turn_detection="manual"). • Call:
Copy code
session.update_options(turn_detection="manual")
This correctly updates the session runtime config and disables automatic turn detection. • Do not use:
Copy code
self.turn_detection = "manual"
Direct assignment does not reconfigure the session internals and will not reliably disable turn detection. Notes for irreversible work: • After setting
turn_detection="manual"
, no automatic user turns will be created; new turns only start if you explicitly call
session.commit_user_turn()
(or re-enable another mode). • You can also stop audio capture with
session.input.set_audio_enabled(False)
for extra safety. Would you like a short code snippet showing an irreversible action followed by disabling turn detection? Sources: Agents (Python) | livekit/livekit_composite | DeepWiki | Turn detection and interruptions | LiveKit Docs