Can we update noise cancelation model in agent ses...
# ask-ai
a
Can we update noise cancelation model in agent session during an ongoing session?
t
Thanks for reaching out, Sailendra. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your question about whether the noise cancellation model can be changed during an ongoing agent session. Short answer: No — you cannot update the noise cancellation model while a session is already running. Noise cancellation is configured when the agent session is started via
RoomOptions
and cannot be changed dynamically mid-session. What you can do instead: • Choose the correct model at session start. For example, set
noise_cancellation.BVC()
for standard WebRTC participants and
noise_cancellation.BVCTelephony()
for SIP participants when you create the session. • If you must use a different noise cancellation model, end the current agent session and start a new one with the desired
RoomOptions
audio input configuration. If it would help, I can show a short code example for selecting the model at session start based on participant type or outline steps for restarting a session with new settings. Which would you prefer? Sources: Agent session | LiveKit Documentation | Using Noise Cancellation for SIP and WebRTC Participants
a
restarting
t
Thanks — you want to restart the session so a different noise-cancellation model is applied. Below are concise, step-by-step instructions to do that safely. 1) Gracefully shut down the current session • To drain any pending speech before closing, call:
Copy code
await session.shutdown(drain=True)
• To close immediately, call:
Copy code
await session.aclose()
2) Create a new AgentSession (if needed) • If you disposed the previous session object, instantiate a new
AgentSession
with the desired components (STT, LLM, TTS, VAD, etc.). 3) Start the session with the updated noise cancellation model • When starting, pass the new noise-cancellation choice in `RoomOptions`:
Copy code
await session.start(
    room=ctx.room,
    agent=Agent(instructions="You are a helpful voice AI assistant."),
    room_options=room_io.RoomOptions(
        audio_input=room_io.AudioInputOptions(
            noise_cancellation=noise_cancellation.BVCTelephony(),  # new model
        ),
    ),
)
Notes and best practices • Noise cancellation cannot be changed mid-session; it only takes effect on session start. • If participant type varies (WebRTC vs SIP), choose the appropriate model before starting (for example,
BVC()
for WebRTC,
BVCTelephony()
for SIP). • Plan for a short interruption when you stop and restart the session; use
shutdown(drain=True)
if you need to preserve in-flight speech. Would you like a ready-to-run code sample that includes session re-creation and safe handover (e.g., preserving any context or logs before restart)? I can tailor it to your language/runtime. Sources: Job lifecycle | LiveKit Documentation | Agent session | LiveKit Documentation | Build Your First AI Voice Agent in Python: Complete Tutorial | LiveKit | Voice AI quickstart | LiveKit Documentation
a
it would be great to provide for safe handover during ongoing session
t
Thanks — I’m responding to your request for a safe handover pattern so you can restart an agent session and apply a different noise-cancellation model with minimal disruption. Below is a concise, practical approach you can follow. It preserves conversation state, prevents audio races, and restarts the session with the new noise filter so users experience as little interruption as possible. High-level steps 1. Persist current session state (conversation history, userdata, linked participant identity). 2. Quiet the audio I/O so you don’t process partial audio while switching. 3. Shutdown or reconfigure the session safely (use graceful shutdown to finish in-flight speech when needed). 4. Start a new session or re-start the current session with updated RoomOptions containing the new noise cancellation model. 5. Restore preserved state (chat context, userdata) and re-enable audio. Safe-handover code pattern (Python-like pseudocode) 1) Persist state
Copy code
# Save what matters before changing audio/session
saved_userdata = session.userdata
saved_chat_ctx = session.chat_ctx.copy()
linked_participant = session.room_io.linked_participant
saved_history = session.history  # if you track full transcripts
2) Quiet I/O to avoid race conditions
Copy code
# Prevent new audio from being processed while we swap filters
session.input.set_audio_enabled(False)
3) Graceful shutdown (optional) or immediate close • To finish any in-flight speech before closing:
Copy code
await session.shutdown(drain=True)
• If you need immediate restart (no draining):
Copy code
await session.aclose()
4) Create/start a new session with updated noise cancellation
Copy code
# Choose filter based on participant type
from livekit.plugins import noise_cancellation
from livekit import rtc

if linked_participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
    new_filter = noise_cancellation.BVCTelephony()
else:
    new_filter = noise_cancellation.BVC()

# Recreate or reuse AgentSession, passing preserved state back in
new_session = AgentSession(
    stt="deepgram/nova-3:en",
    llm="openai/gpt-4.1-mini",
    tts="cartesia/sonic-3:9626c31c-...",
    vad=silero.VAD.load(),
    turn_detection=MultilingualModel(),
)

await new_session.start(
    room=ctx.room,
    agent=Agent(instructions="You are a helpful voice AI assistant.", chat_ctx=saved_chat_ctx),
    room_options=room_io.RoomOptions(
        audio_input=room_io.AudioInputOptions(
            noise_cancellation=new_filter
        ),
    ),
)
# Restore userdata if needed
new_session.userdata = saved_userdata
5) Re-enable audio and hand off control
Copy code
new_session.input.set_audio_enabled(True)
# If you need the agent instance to switch seamlessly, you can use update_agent()
# to hand off to the new agent while preserving context:
# session.update_agent(new_agent)
Notes and best practices • Noise cancellation cannot be hot-swapped mid-session; the change takes effect only when the session is started with the chosen
RoomOptions
. • Use
shutdown(drain=True)
to avoid truncating the user’s last utterance when possible. • Persist the minimal state required (chat context and userdata) to avoid replaying large histories. • If your system links specific participant identities or attributes, preserve and re-associate them when you restart so the UX remains continuous. • If you want zero perceptible disconnect, consider implementing a short overlap where you keep an existing session for playback/continuity while the new session initializes, then switch media routing to the new session once ready. This is more complex but reduces perceived interruption. Would you like a ready-to-run example tailored to your exact runtime (asyncio vs. sync) or language, or would you prefer an overlap/dual-session pattern example for near-zero interruption? Sources: Agent session | LiveKit Documentation | Agents and handoffs | LiveKit Documentation | Job lifecycle | LiveKit Documentation