How do I make the agent not respond when a certain...
# ask-ai
h
How do I make the agent not respond when a certain text is encountered at the TTS node?
t
Thanks for reaching out, James. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you want the agent to stay quiet when it sees certain text at the TTS node, you can set up some control logic to make it skip responding in those cases. For instance, you could tweak the
tts_node
to spot specific input patterns and make sure it doesn’t produce any output when those patterns show up. Need a hand with the details? Just let me know—I’m here to help! Sources: Pipeline nodes and hooks | LiveKit Docs
h
How to stop the response?
t
Hey there! If you want to stop a response in the TTS node, here are a few ways you can do it: • Using
StopResponse
Exception:
You can raise this exception in the
on_user_turn_completed
lifecycle hook to stop the response before it even starts. Here's a quick example:
Copy code
async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage) -> None:
    if not new_message.text_content:
        raise StopResponse()  # This will stop the agent from replying
• Using
session.interrupt()
Method:
This one stops any ongoing TTS playback and clears out queued responses. Check this out:
Copy code
async def tts_node(self, text: AsyncIterable[str], model_settings: ModelSettings):
    MAX_CHUNKS = 20
    chunk_count = 0

    async def process_text():
        nonlocal chunk_count
        interrupted = False
        async for chunk in text:
            chunk_count += 1
            if chunk_count > MAX_CHUNKS and not interrupted:
                self.session.interrupt()
                self.session.say("I'm sorry, that will take too long to say.")
                interrupted = True
                break

            if not interrupted:
                yield chunk

    return Agent.default.tts_node(self, process_text(), model_settings)
• Canceling User Input: Another option is to use
session.clear_user_turn()
or just disable audio input to make sure no responses get triggered. If you want, I’m here to help you with any of these methods—just let me know! Sources: livekit-agents/livekit/agents/voice/generation.py | pipeline-tts/short_replies_only.py | Pipeline nodes and hooks | LiveKit Docs