how can I reply something to the user if the agent...
# ask-ai
a
how can I reply something to the user if the agent's state is in thinking for more than 3 seconds
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can reply to a user if the agent's state is
thinking
for more than 3 seconds by listening for the
agent_state_changed
event and starting a timeout when the state becomes
thinking
. If the state is still
thinking
after 3 seconds, use
session.generate_reply()
to send a brief update message to the user. Cancel the timeout when the agent leaves the
thinking
state to prevent duplicate notifications. This method ensures your users get a natural, reassuring update during any long processing period. For example:
Copy code
async def on_agent_state_changed(event):
    if event.new_state == "thinking":
        # Start your timeout here (3 seconds)
        ...
    elif event.old_state == "thinking":
        # Cancel the timeout if agent stops thinking
        ...
Then, in your timeout handler, call:
Copy code
await session.generate_reply(instructions=
    "You are taking a little while to process the request. Give the user a very brief update that you're still working on their request. Keep it short and natural."
)
This pattern is fully described in the LiveKit agent documentation and code examples. Sources: livekit-agents/livekit/agents/llm/utils.py | Events and error handling | LiveKit Docs | External data and RAG | LiveKit Docs | Handling RAG Delays in Voice Agents | LiveKit Docs