bulky-car-15738
12/09/2025, 6:18 AMtall-airline-16756
12/09/2025, 6:18 AMtall-airline-16756
12/09/2025, 6:18 AMfrom 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):
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 docsbulky-car-15738
12/09/2025, 6:27 AMtall-airline-16756
12/09/2025, 6:28 AMcontext.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:
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