My tool call is being interrupted, but the agent i...
# ask-ai
g
My tool call is being interrupted, but the agent is not able to pickoff from where it was interrupted
t
Thanks for reaching out, Aumesh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I understand your tool call is being interrupted and the agent can't pick up where it left off. I can help troubleshoot and suggest workarounds. Quick facts: • LiveKit Agents do not automatically resume a tool call after an interruption. When interrupted, the running tool is cancelled and its result is removed from history. • You can detect interruptions inside a tool via
context.speech_handle.interrupted
, but that only lets you clean up, not resume mid-execution. Options to resolve or mitigate: • Prevent interruptions during critical work: call
context.disallow_interruptions()
at the start of the tool to keep it from being cancelled. • Implement external state persistence: save progress periodically (database or in-memory store) so the tool can restart and continue from the last saved checkpoint after an interruption. • Re-initiate on resume: when a session restarts, detect the incomplete operation (session state) and prompt the agent/user to restart or continue from saved progress. To give targeted guidance, can you tell me: 1. What kind of tool is being interrupted (API request, file processing, long computation, etc.)? 2. Are you currently using
context.disallow_interruptions()
or
allow_interruptions=False
anywhere? 3. Do you observe any error messages or logs when the interruption happens? 4. Would you prefer preventing interruptions entirely, or adding resumable checkpoints so the tool can continue after a restart? Answering those will let me recommend concrete code patterns or storage approaches. Sources: Tool definition and use | LiveKit docs | Turns overview | LiveKit docs
g
but after the interruption shouldn't the agent be able to decide whether to call the function tool again or not?
t
You're right to expect the agent to be able to decide — but it won't do that automatically. When a tool call is interrupted, the call is cancelled and removed from history; the agent itself must detect that and choose whether to call the tool again. What you can do • Detect the interruption inside the tool using
context.speech_handle.interrupted
. That lets you clean up and signal the agent that the operation ended prematurely. • Persist partial progress (database, cache, session state) so on next turn the agent can inspect state and decide whether to retry, continue from a checkpoint, or abandon. • Use explicit retry logic in the agent: after an interruption, have the agent ask the user whether to retry or automatically re-invoke the tool based on session/state rules. • Prevent interruptions for critical sections with
context.disallow_interruptions()
when you need the operation to run to completion. Minimal code example for detection and cleanup
Copy code
@function_tool()
async def my_long_running_task(self, context: RunContext):
    wait_for_result = asyncio.ensure_future(self._long_operation())
    await context.speech_handle.wait_if_not_interrupted([wait_for_result])

    if context.speech_handle.interrupted:
        wait_for_result.cancel()
        # persist any partial state here if useful
        return None
    else:
        return await wait_for_result
Suggested flow to let the agent decide 1. Tool runs and writes checkpoints/partial results to storage. 2. If interrupted, tool returns/cleans up (and optionally notes interruption in session state). 3. On the next agent turn, check session/state: ◦ If state shows an incomplete operation, either prompt the user (“Resume previous task?”) or automatically re-invoke the tool with resume parameters. ◦ If no resume desired, clear the incomplete state. If you tell me what type of tool and what state you can store (DB, Redis, in-memory), I can suggest a concrete retry/resume pattern or sample code tailored to your case.