how can i make the agent do a predetermine session...
# ask-ai
f
how can i make the agent do a predetermine session.say whenever the agent took morethan 5s to answer
t
Thanks for reaching out, Huy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It sounds like you want the agent to speak a preset message if a reply is taking longer than 5s. You can do this by starting a timer alongside the operation and calling
session.say(...)
only if the timeout is exceeded. Basic approach: 1. When you kick off the agent’s work (tool call, external request, etc.), start a background task that checks elapsed time. 2. If it reaches 5s and the work hasn’t finished, call
session.say(text="Thanks for waiting—still working on that.")
. 3. When the work completes, cancel the monitor task and optionally cancel/ignore any pending status speech via the returned
SpeechHandle
. Notes: •
session.say
triggers TTS playback and returns a
SpeechHandle
you can await or cancel. You can control interruptibility via
allow_interruptions
. Details • This follows the “verbal status updates” pattern (speak only if the task exceeds a timeout). Pattern • For realtime models, ensure a TTS plugin is available for
session.say
. More Would you like a minimal Python snippet showing the timer +
session.say
flow? Sources: Agent speech and audio | LiveKit docs | Events and error handling | LiveKit docs | External data and RAG | LiveKit Docs | How to detect and handle silence during calls
f
yes
t
Here’s a minimal Python pattern using a 5s timeout and session.say. It starts a monitor task when work begins, speaks a status update if the timeout hits, and cleans up when done.
Copy code
import asyncio
import time

STATUS_TIMEOUT_SEC = 5.0

async def do_work_and_maybe_speak_status(session, work_coro):
    # Start the long-running work
    work_task = asyncio.create_task(work_coro)

    # Monitor task that speaks if work exceeds timeout
    async def monitor():
        start = time.monotonic()
        while not work_task.done():
            if time.monotonic() - start >= STATUS_TIMEOUT_SEC:
                # Speak a predetermined status update
                speech = await session.say(
                    text="Thanks for waiting—I'm still working on that.",
                    allow_interruptions=True,  # set False if you don't want it interrupted
                )
                # Optional: await completion or keep a handle to cancel later
                await speech.wait()  # depends on your SpeechHandle API (e.g., .wait()/.done())
                break
            await asyncio.sleep(0.2)

    monitor_task = asyncio.create_task(monitor())

    try:
        result = await work_task
        return result
    finally:
        # Clean up the monitor if still running
        if not monitor_task.done():
            monitor_task.cancel()
            with contextlib.suppress(asyncio.CancelledError):
                await monitor_task
Notes: • Ensure your session has a TTS plugin for
session.say
. If not, add one before using it. • You can store the returned
SpeechHandle
to cancel it if the main work completes mid-status message. Need the same pattern adapted to your framework (tool calls, function calling, or generate_reply)?
f
do i modified this in agent.py , if yes write the whole agent.py file
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!