Can tool calls in agent be interrupted by user?
# ask-ai
p
Can tool calls in agent be interrupted by user?
t
Thanks for reaching out, harish. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely! In LiveKit Agents, tool calls can be interrupted by the user by default. This means if a user speaks during a tool call, the tool is considered interrupted, and any partial results are ignored. But don’t worry—developers have the option to manage these interruptions using the
speech_handle
utilities, which can help cancel long-running tasks when needed. If you want to prevent interruptions for certain tool calls, you can do so by using
run_ctx.disallow_interruptions()
right at the start of the tool execution. Plus, there are session-level settings like
allow_interruptions
and
min_interruption_duration
that give you even more control over how interruptions behave. If you’d like, I’m here to provide more details or examples—just let me know! Sources: Tool definition and use | LiveKit docs | examples/voice_agents/README.md
p
how to use this run_ctx.disallow_interruptions, give an example
t
To make sure your tool function runs smoothly without interruptions, you can use
run_ctx.disallow_interruptions()
right at the start of your function. Here’s a little example to help you see how it works:
Copy code
from livekit.agents import Agent, RunContext
from livekit.plugins import openai

class MyAgent(Agent):
    @openai.function_tool
    async def critical_operation(self, run_ctx: RunContext) -> str:
        # Disable interruptions for this function call
        run_ctx.disallow_interruptions()

        # Simulate a long-running, non-interruptible task
        await asyncio.sleep(10)
        return "Operation completed successfully"
In this example, by calling
run_ctx.disallow_interruptions()
inside the
critical_operation
tool, we ensure the task runs all the way through without any interruptions. This is especially handy for important tasks that need to finish completely. If you’d like me to walk you through more examples or if anything isn’t clear, just let me know—I’m here to help! Sources: Tool definition and use | LiveKit docs | Turn detection and interruptions | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py
p
so give a dry run on how this works within an users conversation. lets say llm thought to trigger the tool, but in that moment right before triggereing, if the user speaks, what happens?
t
I understand it can be a bit tricky when a user speaks right before a tool is triggered. Here’s a friendly breakdown of how it works, depending on timing and interruption settings: • Default Behavior: When a user starts speaking, the tool gets interrupted, removed from the history, and its result is ignored. The agent then focuses on responding to the user’s new input instead of running the tool. • Interruption Detection: The system uses certain thresholds, like a minimum speech duration (default: 0.5 seconds) and word count, to decide if the interruption is valid. • Preventing Interruptions: If you want to avoid interruptions, you can disable them using
run_ctx.disallow_interruptions()
, so the tool will run without being stopped by user speech. • False Interruptions: If no real user speech is detected, the agent will simply continue its task after a short timeout (default: 2 seconds). For example, if a tool is about to run but the user speaks, the system checks if that speech qualifies as an interruption. If it does, the tool execution is canceled. But if interruptions are turned off, the tool will carry on no matter what. Please feel free to ask if you’d like me to explain anything further or provide some examples—I’m here to help! Sources: Tool definition and use | LiveKit docs | agents/src/voice/agent_activity.ts | livekit-agents/livekit/agents/voice/agent_activity.py | Turn detection and interruptions | LiveKit Docs | Agent Session Management & Turn Detection | livekit/livekit_composite | Dee...
p
Still not clear await self.update_chat_ctx(_chat_ctx_) async for chunk in Agent.default.llm_node(self, _chat_ctx_, tools, _model_settings_): yield chunk this is my code in llm_node, lets say this llm_node is called, at this movement if the user speaks, is this cancelled?
t
I wanted to share a little insight about how things work when a user speaks during the execution of
llm_node
. By default, the ongoing task gets canceled. This happens because the system uses the
speech_handle
utility to keep an eye out for any interruptions. If it detects one, it gently stops the
llm_node
execution and discards any partial results to keep everything clean. If you'd prefer to keep things running without interruptions during
llm_node
execution, you have the option to set the agent to disallow interruptions. You can do this by adjusting the
allow_interruptions
parameter or by explicitly calling
run_ctx.disallow_interruptions()
in your tool logic. Please feel free to reach out if you'd like me to walk you through it further or provide some examples—I'm here to help! Sources: Tool definition and use | LiveKit docs | livekit-agents/livekit/agents/voice/agent.py
p
so if run_ctx.disallow_interruptions() is added to the tool definition, then even if the user speaks, will it still be called ?
t
Absolutely! When you add
run_ctx.disallow_interruptions()
to the tool definition, it means that any user speech won’t interrupt the tool while it’s running. This is a great way to make sure your task finishes smoothly without any interruptions. If you’d like, I’m here to help with more details or examples—just let me know! Sources: livekit-agents/livekit/agents/voice/agent_activity.py
p
look at async for chunk in Agent.default.llm_node(self, _chat_ctx_, tools, _model_settings_): yield chunk logger.info(f"✅ [GENERATE_RESPONSE] User: {user_id} - Response generation completed") Example, lets say, if the user interrupted while llm_node was calling. will the next log line gets executed atlease?
t
I completely understand how interruptions during
llm_node
execution can be a bit tricky! When a user interrupts, the following log line (or any code after the
llm_node
call) doesn’t run by default because the interruption cancels the ongoing task, and that cancellation skips the rest of the code. To make sure your log line still executes even if there’s an interruption, a great approach is to use a
try/finally
block. This way, you can handle any cleanup or logging smoothly:
Copy code
try:
    async for chunk in Agent.default.llm_node(self, chat_ctx, tools, model_settings):
        yield chunk
except asyncio.CancelledError:
    logger.info("LLM node was interrupted")
    raise
finally:
    logger.info(f"✅  [GENERATE_RESPONSE] User: {user_id} - Response generation completed")
With this setup, the log line inside the
finally
block will always run, even if the task gets interrupted. If you’d like, I’m here to help clarify anything further—just let me know! Sources: Tool definition and use | LiveKit docs | livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/realtime/...