after the current user speech finishes how to add ...
# ask-ai
b
after the current user speech finishes how to add a certain behavior to agent. for example, after a certain time from the session start, I want I agent to say something after a user finishes speaking
t
Thanks for reaching out, mateuszkasprowicz. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to make an agent speak after the user finishes speaking, but only if a certain amount of time has passed since the session started. Below is a concise pattern you can use and adapt. Key idea: • Record session start time in
on_enter()
. • Detect end of user speech with
on_user_turn_completed()
. • In that hook, check elapsed time and trigger the agent reply (once or per your rules) with
session.generate_reply()
or
session.say()
. Example (Python — minimal):
Copy code
from livekit.agents import Agent
from livekit.agents.voice import AgentSession
import asyncio
from datetime import datetime

class TimedPostUserAgent(Agent):
    def __init__(self):
        super().__init__(instructions="You are a helpful assistant.")
        self.session_start = None
        self.triggered = False  # ensure one-time behavior

    async def on_enter(self):
        self.session_start = datetime.utcnow()

    async def on_user_turn_completed(self, turn_ctx, new_message):
        if self.triggered:
            return

        delay_seconds = 10  # time since session start required
        elapsed = (datetime.utcnow() - self.session_start).total_seconds()
        if elapsed >= delay_seconds:
            self.triggered = True
            await asyncio.sleep(0.5)  # optional slight pause
            await self.session.generate_reply(instructions="Say a brief greeting and ask how to help.")
If you want variations I can help with: • Triggering repeatedly (every N minutes) instead of once • Cancelling the behavior if user is active • Using
session.say()
directly for fixed TTS text Which variation do you want to implement? Sources: Pipeline nodes and hooks | LiveKit docs | Agent speech and audio | LiveKit docs