can we say something before tool execution starts ...
# ask-ai
b
can we say something before tool execution starts asynchronously?
t
Thanks for reaching out, Sandeep. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes, you can provide immediate feedback to users before asynchronous tool execution starts in LiveKit. Options include: • Verbal status updates from the agent to indicate an operation is starting or may take time • "Thinking" sounds to show activity • Frontend UI indicators that visualize tool execution status • Monitoring agent state changes (like switching to
thinking
) and updating your interface accordingly These methods ensure users are kept informed before and during the execution of long-running operations, improving user experience. For implementation details, use the agent's
RunContext
to send feedback immediately prior to execution, or monitor state changes in your frontend to trigger feedback automatically. Let me know if you need code examples or more details on any specific approach. Sources: External data and RAG | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Tool definition and use | LiveKit Docs | Web and mobile frontends | LiveKit Docs
b
provide more info how can I use RunContext prior to execution
t
Hey there! Let me walk you through a cool way to keep your users in the loop while your code is working its magic. With
RunContext
, you can give your participants a friendly heads-up about what's happening behind the scenes. Think of it like sending a quick text to a friend saying, "Hey, working on that thing you asked about!" • Pattern example:
Copy code
import asyncio
from livekit.agents import function_tool, RunContext

@function_tool()
async def search_knowledge_base(self, context: RunContext, query: str) -> str:
    async def _status_update(delay: float = 0.5):
        await asyncio.sleep(delay)
        await context.session.generate_reply(instructions=f"You are searching the knowledge base for '{query}' but it is taking a little while. Update the user briefly.")
    status_task = asyncio.create_task(_status_update())
    result = await _perform_search(query)
    status_task.cancel()
    return result
Pro tip: If you want to be even more direct, you can just use
context.session.say("Your message")
before diving into your main task. It's like giving your users a quick wave and saying, "I'm on it!" This way, they know something's happening and won't wonder if their request got lost in the digital void. Super helpful for those tasks that might take a bit longer than a blink! Sources: External data and RAG | LiveKit Docs | rpc/rpc_agent.py | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...
b
but this await context.session.generate_reply(instructions=f"You are searching the knowledge base for '{query}' but it is taking a little while. Update the user briefly.") will block the fucntion execution I want something async
t
Hey there! You're absolutely right about the potential blocking issue. Let me share a neat trick to keep things running smoothly. Instead of directly calling the reply generation, we can use an async helper that runs in the background – think of it like a friendly nudge to keep the user informed.
Copy code
import asyncio

async def _status_update(context, query, delay=0.5):
    await asyncio.sleep(delay)
    await context.session.generate_reply(instructions=f"You are searching the knowledge base for '{query}', but it is taking a little while. Update the user briefly.")

status_task = asyncio.create_task(_status_update(context, query))
result = await _perform_search(query)
status_task.cancel()
Pretty cool, right? This approach lets you run your main task without interruption while still keeping the user in the loop. If the search takes a bit longer than expected, they'll get a friendly update. And if it's quick, no worries – the status task gets cancelled. It's all about creating a smooth, responsive experience! Sources: External data and RAG | LiveKit Docs
b
what option or conditions can I implement on turn_change will say something in async mode
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!