In Livekit agents on python, when I want to mute t...
# ask-ai
f
In Livekit agents on python, when I want to mute the bot I use the
session.output.set_audio_enabled(False)
command. However this only disables the audio output (i.e. the TTS), the LLM still ends up being called and generated on the background. How can I pause both LLM generation as well as TTS?
t
Thanks for reaching out, Amitrajit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to guide you through how to pause both LLM generation and TTS in LiveKit Python agents. It’s actually quite straightforward using the built-in tools for managing speech scheduling and audio output. Here’s a simple way to do it: 1. Pause LLM Generation: You can use the
pause()
method from the
AgentActivity
class. This sets the
_scheduling_paused
flag to
True
, which effectively stops new LLM generations from starting. 2. Pause TTS: Just make sure your audio output supports pausing by checking the
can_pause
property. Also, you can configure the
resume_false_interruption
parameter in
AgentSession
to manage interruptions gracefully and resume audio output when appropriate. Here’s an example of how you might set this up for smooth coordinated pausing:
Copy code
session = AgentSession(
    vad=silero.VAD.load(),
    llm=openai.LLM(model="gpt-4o-mini"),
    stt=deepgram.STT(),
    tts=cartesia.TTS(),
    false_interruption_timeout=1.0,
    resume_false_interruption=True,
)
This configuration helps pause both LLM generation and TTS during interruptions, then resumes them if no new input comes in within the timeout period. If you have any questions or want me to walk you through any part of this, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | delay-handling/README.mdoc
f
Can you give a code example of how I can pause the LLM and TTS in a workflow at the end of a function call, prior to transferring to a new workflow?
t
Here’s a helpful code example that gently pauses both LLM generation and TTS right at the end of a function call, just before moving over to a new workflow:
Copy code
from livekit.agents import function_tool, Agent, RunContext

@function_tool()
async def transfer_to_new_workflow(context: RunContext):
    """Transfer to a new workflow after ensuring current speech completes"""
    
    # Wait for the current function call's speech to complete
    await context.wait_for_playout()

    # Optionally, disable interruptions for this specific function call
    context.disallow_interruptions()

    # Access and wait for the current speech handle
    current_speech = context.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()

    # Now safely transfer to the new workflow
    return NewWorkflowAgent(), "Transferring you to the next step"
This approach makes sure that both LLM generation and TTS finish smoothly before you move on to the next workflow. If you’d like, I’m here to help with any questions or if you want me to walk you through more examples! Sources: agent.py | chain-of-thought-tts/README.mdoc | Tool definition and use | LiveKit docs | livekit-agents/livekit/agents/voice/events.py
f
I don't want LLM and TTS to finish smoothly, I want to pause it before transferring so that no generation happens. Please give the command to stop LLM and TTS generation before transferring
t
Hi there! To quickly stop both LLM and TTS generation before moving to a new workflow, you can use the
interrupt()
method on the session. This will pause all ongoing and queued speech generation tasks right away. Here’s the command you can use:
Copy code
await context.session.interrupt()
If you want to make sure the interruption happens no matter what the current speech's
allow_interruptions
setting is, just add the
force
parameter like this:
Copy code
await context.session.interrupt(force=True)
Try including this in your workflow before starting the transfer, and it should prevent any generation from continuing in the background. If you have any questions or need more help, feel free to reach out—I’m here for you! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Agent Session Management & Turn Detection | livekit/livekit_composite | Dee... | agents/src/voice/agent_session.ts
f
Thank you, can you tell me how I can resume LLM and TTS generation once I move to the new workflow? Basically how do reverse the effect which
context.session.interrupt()
has, to allow generation to start again?
t
To resume LLM and TTS generation after using
context.session.interrupt()
, the process is typically automated if you have
resume_false_interruption=True
configured in your session. This allows the agent to automatically resume generation if no new user input is detected within the
false_interruption_timeout
period. Here’s an example configuration:
Copy code
session = AgentSession(
    vad=silero.VAD.load(),
    llm=openai.LLM(model="gpt-4o-mini"),
    stt=deepgram.STT(),
    tts=cartesia.TTS(),
    false_interruption_timeout=1.0,
    resume_false_interruption=True,
)
If you want to manually trigger a new LLM and TTS generation cycle after the interruption, you can use:
Copy code
await context.session.generate_reply(instructions="Your new instructions here")
This will initiate a fresh round of LLM inference and TTS synthesis. Let me know if you need further clarification or additional examples! Sources: Turn detection and interruptions | LiveKit Docs | livekit-plugins/livekit-plugins-google/livekit/plugins/google/realtime/real...
f
You said this will pause all ongoing and queued speech generation tasks immediately. However, instead what I want to achieve is this: • If speech is ongoing, let it finish • However, any quued speech generation tasks should be flushed out (not just paused, removed completely) How can I achieve this?
t
To allow ongoing speech to finish while flushing queued speech generation tasks, you can use the following approach:
Copy code
# Allow current speech to finish playout while clearing queued speech
await context.session.interrupt(force=False)
This ensures that the currently playing audio completes, but any queued speech generation tasks are removed entirely. Let me know if you need further clarification or additional assistance! Sources: Job lifecycle | LiveKit docs | livekit-agents/livekit/agents/voice/agent_activity.py