creamy-judge-56458
08/25/2025, 6:03 PMtall-airline-16756
08/25/2025, 6:03 PMtall-airline-16756
08/25/2025, 6:04 PM@function_tool annotation, a great way to do this is by using the await self.session.say() method inside the function tool itself. Let me show you how:
Example Implementation:
from livekit.agents import function_tool, Agent, RunContext
class MyAgent(Agent):
@function_tool()
async def get_orders(self, context: RunContext):
# Say something before executing the function
await self.session.say("Let me check the orders.")
# Perform the actual function logic
orders = self.fetch_orders() # Replace with your logic
return orders
Key Points:
• Use await self.session.say() to have the agent say something before running the main function logic.
• You can return None or a specific value depending on whether you want the function to complete silently or send a response back to the LLM.
• Make sure the function logic comes after the speech so everything feels smooth and consistent.
This way, your agent will always say that friendly phrase before diving into the function’s work.
Sources: Tool definition and use | LiveKit Docs | basics/function_calling.pycreamy-judge-56458
08/25/2025, 6:07 PMtall-airline-16756
08/25/2025, 6:08 PMcreamy-judge-56458
08/25/2025, 6:08 PMtall-airline-16756
08/25/2025, 6:09 PMllm_node method in your Agent class. This way, you’ll catch when a tool call is about to happen and can pop in a friendly message before the tool starts working. Here’s a simple example to show you how:
from livekit.agents.llm import ChatChunk
class FunctionAgent(Agent):
async def llm_node(self, chat_ctx, tools, model_settings):
"""Override the llm_node to say a message when a tool call is detected."""
activity = self._activity
tool_call_detected = False
# Get the original response from the parent class
async for chunk in super().llm_node(chat_ctx, tools, model_settings):
# Check if this chunk contains a tool call
if isinstance(chunk, ChatChunk) and chunk.delta and chunk.delta.tool_calls and not tool_call_detected:
# Say the checking message only once when we detect the first tool call
tool_call_detected = True
activity.agent.say("Sure, I'll check that for you.")
yield chunk
Here’s what’s happening:
• The method keeps an eye on the LLM response stream for any tool calls.
• Once it spots a tool call, it uses activity.agent.say() to share a quick message before the tool kicks in.
• It makes sure to say this message just once for each tool call sequence, so it’s not overwhelming.
This way, your agent will always give a little heads-up before running any function tool, making the interaction feel more natural and friendly.
Sources: agent.py | Handling RAG Delays in Voice Agents | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...creamy-judge-56458
08/25/2025, 6:11 PMtall-airline-16756
08/25/2025, 6:12 PMctx.session.generate_reply(). Here’s a simple example to show you how:
from livekit.agents import function_tool, RunContext
class MyAgent(Agent):
@function_tool()
async def get_orders(self, ctx: RunContext):
# Add a friendly message before running the main function
await ctx.session.generate_reply(
instructions="Let me check the orders for you."
)
# Now, perform the actual function logic
orders = self.fetch_orders() # Replace this with your own logic
return orders
Here’s what’s happening:
• The ctx.session.generate_reply() method makes sure the agent shares that message before diving into the function’s main work.
• This way, only the "get_orders" function gets this message—other tools won’t be affected.
If you have any questions or want me to walk you through it, just let me know—I’m here to help!
Sources: Handling RAG Delays in Voice Agents | LiveKit Docs | examples/voice_agents/restaurant_agent.py | External data and RAG | LiveKit Docs | agent.py | Agents telephony integration | LiveKit Docs