how do i make my agent ask are you there or any co...
# ask-ai
a
how do i make my agent ask are you there or any confirmation message if there is no response from the user. should i add it in the prompt or
t
Thanks for reaching out, Aryan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're on the right track asking whether to put it in the prompt — short answer: don't rely on the prompt alone. Use event/timer logic to detect silence and programmatically trigger the agent to ask a confirmation. What to do (concise): 1. Listen for user state changes (use the
user_state_changed
event). 2. Track last-spoken time and run a background timer/loop with your chosen threshold (e.g., 5s). 3. When the threshold is exceeded, call
session.generate_reply()
(or your agent's reply API) with instructions like "Ask politely if the user is still there." 4. Reset the timer when you detect
speaking
again. Why not the prompt: • Prompts set behavior/voice but cannot observe real-time events or measure silence duration. If you want, I can: • Provide a minimal runnable example in your language (Python/Node). • Suggest a polite message template and retry/backoff logic. Which would you prefer? Sources: Events and error handling | LiveKit docs | How to detect and handle silence during calls
a
give me a minimal example code in python
t
Below is a minimal Python example that listens for user state changes, tracks silence, and prompts the agent to ask "Are you there?" after a silence threshold. Adapt the
AgentSession
, event names, and imports to match your project setup.
Copy code
import asyncio
import time
from livekit.agents import AgentSession  # adjust import to your SDK layout

SILENCE_THRESHOLD = 5  # seconds

async def run_agent(session: AgentSession, room_id: str):
    await session.start(room=room_id)  # initialize/join room as your setup requires

    user_last_spoke = time.time()

    @session.on("user_state_changed")
    def on_user_state_changed(event):
        nonlocal user_last_spoke
        # adjust event field names as needed (example: event.new_state)
        if event.new_state == "speaking":
            user_last_spoke = time.time()

    async def monitor_silence():
        nonlocal user_last_spoke
        while True:
            await asyncio.sleep(1)
            if time.time() - user_last_spoke > SILENCE_THRESHOLD:
                # instruct agent to ask a confirmation question
                await session.generate_reply(instructions="Ask: 'Are you there?'")
                user_last_spoke = time.time()  # reset after prompting

    asyncio.create_task(monitor_silence())

# Example usage
async def main():
    session = AgentSession(...)  # provide credentials/config
    await run_agent(session, room_id="room123")
    # keep process alive (or integrate with your service lifecycle)
    await asyncio.Event().wait()

if __name__ == "__main__":
    asyncio.run(main())
Notes: • Put your actual
AgentSession
initialization and room/agent config where indicated. • Tune
SILENCE_THRESHOLD
, retry/backoff, and the prompt message as needed. • If you want, I can adapt this to your exact SDK method signatures or provide a Node.js version.