Is it possible to have the agent say something spe...
# ask-ai
c
Is it possible to have the agent say something specific every time a specific function with @function_tool is called. Let’s say that I have a function called “get_orders” with the @function_tool() annotation. Before the agent calls this function I want it to always say “let me check the orders”, and to do this consistently and to always do this once. How can I achieve that?
t
Thanks for reaching out, Vahid. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you'd like the agent to consistently say something specific right before calling a function with the
@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:
Copy code
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.py
c
With this approach the agent is already in the function, this needs to happen prior. So this wouldn’t work would it?
t
It sounds like you'd like the agent to say something before the function even starts running, instead of right at the beginning. Could you help me understand a bit more? Are you looking for a way to add a message before any function tool gets called, or is this for a particular setup you're working on? Sources: tool_calling/update_tools.py
c
Yes, I would like to add a message before any function tool get’s called, how can I achieve that?
t
If you want to add a little message right before any function tool gets called, you can do that by overriding the
llm_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:
Copy code
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...
c
Let’s say I have a function called “get_orders” and I want add a little message only right before this specific function tool is called, how would that work?
t
If you want to add a message just before a specific function tool, like "get_orders," you can do so by including a message right at the start of the function using
ctx.session.generate_reply()
. Here’s a simple example to show you how:
Copy code
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