how to disable interruptions for a tool
# ask-ai
b
how to disable interruptions for a tool
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to disable interruptions for a tool — I can help with that. To prevent a tool from being interrupted while it runs, call the RunContext method that disables interruptions at the start of the tool's execution. • Python (Agents):
Copy code
from livekit.agents import function_tool, RunContext

@function_tool()
async def my_uninterruptible_tool(context: RunContext):
    # Prevent this tool from being interrupted
    context.disallow_interruptions()

    # critical work here
    result = await long_running_external_operation()
    return result
• Node.js / TypeScript (Agents):
Copy code
const myUninterruptibleTool = llm.tool({
  description: 'A tool that cannot be interrupted',
  execute: async (_, { ctx }) => {
    // Prevent interruption
    ctx.disallowInterruptions();

    // critical work here
    const result = await longRunningExternalOperation();
    return result;
  },
});
This disables interruptions only for that tool execution; interruptions resume afterward. Use this for irreversible or critical actions (e.g., sending emails, charging payments). Do you want an example tailored to a specific tool or language? Sources: Tool definition and use | LiveKit docs
b
iam using python, i need to disallow text input also until tool completes
t
You're using Python and want to disable text input as well as audio interruptions while a tool runs — here's how to do that. Summary: call
context.disallow_interruptions()
inside the tool to block audio interruptions, and implement a custom text input handler that checks a shared lock/flag (e.g., an
asyncio.Lock
or boolean in
session.userdata
) to reject or defer incoming text while the tool holds the lock. Concise example:
Copy code
import asyncio
from livekit.agents import function_tool, RunContext, AgentSession
from livekit.agents.voice import room_io

_input_lock = asyncio.Lock()

async def handle_text_input(session: AgentSession, event: room_io.TextInputEvent):
    if _input_lock.locked():
        await session.say("Please wait — I'm processing your previous request.", allow_interruptions=False)
        return
    # normal text handling (may interrupt)
    session.interrupt()
    await session.generate_reply(user_input=event.text)

@function_tool()
async def my_tool(ctx: RunContext, query: str):
    # Block audio interruptions for this tool
    ctx.disallow_interruptions()

    # Acquire lock to block text handling while running
    async with _input_lock:
        await ctx.session.say("Working on that now — please wait.", allow_interruptions=False)
        result = await long_running_external_operation(query)
        await ctx.session.say("Done.", allow_interruptions=False)
        return result

# When starting the session, register the custom text handler:
# await session.start(..., room_options=room_io.RoomOptions(
#     text_input=room_io.TextInputOptions(text_input_cb=lambda s, e: asyncio.create_task(handle_text_input(s, e)))
# ))
Notes: • There is no built-in API to globally disable text input; you must intercept
lk.chat
messages via a custom
text_input_cb
. • Use the lock/flag to ensure consistency and to optionally notify users their text is ignored while the tool runs. • Release of the lock happens automatically when the
async with
block exits; ensure exceptions are handled so the lock is not held indefinitely. Would you like this adapted to use
session.userdata
for the flag, or a pattern that queues text inputs instead of rejecting them? Sources: Text and transcriptions | LiveKit docs | Tool definition and use | LiveKit docs