how to invoke a tool call from on_enter
# ask-ai
w
how to invoke a tool call from on_enter
t
Thanks for reaching out, Test. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you understand how to invoke a tool call from the
on_enter
lifecycle hook in LiveKit! You can do this by using the
self.session.generate_reply()
method. This method lets you provide clear instructions that guide the LLM to pick and run a specific tool based on its reasoning. Here's a simple example:
Copy code
async def on_enter(self):
    self.session.generate_reply(instructions="Check the weather in Tokyo")
So, if your agent has a tool like
get_weather
registered, the LLM will understand the instruction and create a tool call for it, which the system will then carry out. This way, tool calls are made through the LLM’s decision-making process, keeping everything consistent with how the framework is designed. If you have any questions or want to explore this further, feel free to ask—I’m here to help! Sources: Docs::Agents | livekit-agents/livekit/agents/voice/agent.py | flows/simple_flow.py | Pipeline nodes and hooks | LiveKit Docs
w
how to avoid this insrcution from being added to chat context and also i want no reply to be said and generated as my tool call uses session say and anwers the user query
t
Hi there! To help you avoid having instructions added to the chat context and to prevent replies during
on_enter
, here are some friendly steps you can follow: 1. Prevent Instructions from Being Added to Chat Context: • When you provide instructions using
generate_reply(instructions=...)
, they won’t be added to the chat history. Only the final agent response is saved, so this method keeps things clean by design. • If you’re using
update_instructions()
to add system-level instructions, you can easily remove them with
remove_instructions(chat_ctx)
to make sure they don’t stick around. 2. Prevent Replies During `on_enter`: • If you don’t want a reply during
on_enter
, simply avoid calling
self.session.generate_reply()
or
self.session.say()
. • If you need to run a tool quietly, just make sure the tool doesn’t return a value—tools without return values won’t trigger any replies. • Another neat way is to use the
function_tools_executed
event and call
ev.cancel_tool_reply()
to stop replies after tool execution, like this:
Copy code
@session.on("function_tools_executed")
def on_function_tools_executed(ev):
    ev.cancel_tool_reply()
Following these tips will help your tool calls run smoothly without adding extra context or generating replies you don’t want. If you have any questions or need more help, feel free to ask—I’m here for you! Sources: Agent speech and audio | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py
w
@session.on("function_tools_executed") def on_function_tools_executed(ev): ev.cancel_tool_reply() if i do this will llm continue flow without user intervention?
t
When you use
ev.cancel_tool_reply()
in the
function_tools_executed
event, it stops the LLM from generating a reply right after the tool finishes its job. This is great because it lets the agent keep going smoothly without needing you to jump in, as long as your code has the right steps to handle what comes next. Once the reply is suppressed, the agent can: • Take another action, like starting a new tool call or task. • Stay idle and wait patiently for your next input. • Or even trigger another response on its own using
session.generate_reply()
or
session.say()
. This way, the agent stays active and ready, allowing the conversation to flow naturally without always needing you to step in—just make sure your code includes the logic to guide those next steps. Sources: Tool definition and use | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Tool definition and use | LiveKit Docs | Events and error handling | LiveKit Docs
w
if i intructions to do after tool call is done will those be executed via llm without user saying anything?
t
Thank you for your question! Just to clarify, instructions provided after a tool call aren’t automatically carried out by the LLM without you stepping in. The system first processes the tool call, and if the tool gives back some output, the LLM might respond based on that updated information. Whether it replies depends on if the tool’s output actually needs a response and if the reply hasn’t been canceled using
ev.cancel_tool_reply()
. If you’d like the LLM to follow specific instructions right after a tool call without needing any extra input from you, you’ll want to set that up in your code. For instance, you can use
session.generate_reply()
or other logic to smoothly guide what happens next after the tool runs. I hope this helps you move forward! Feel free to ask if you need more guidance. Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | plugins/test/src/llm.ts
w
generate_reply explae to pass tool choice
t
Thanks for reaching out! Could you please help me understand a bit more about what you mean by "pass tool choice" in
generate_reply
? Are you wondering how to guide the LLM to pick a specific tool, or are you looking to pass parameters to a tool while it’s running? I’m here to help you through this!
w
tool_choice param in generate_reply how to use that
t
I'm happy to help you understand the
tool_choice
parameter in
generate_reply
. This parameter lets you decide how tools are used when generating responses. Here’s a friendly breakdown of your options: • 'auto': The model will thoughtfully decide when to use tools based on the context (this is the default setting). • 'required': This option makes sure a tool is always used, no matter the context. • 'none': If you prefer, you can turn off all tool usage completely. • Specific tool: You can also specify a particular tool by name that you want to be used. Here’s a simple example in Python to illustrate:
Copy code
# Forcing the use of a specific tool
handle = agent_session.generate_reply(
    user_input="What's the weather in Paris?",
    tool_choice=llm.ToolChoice(type="function", name="get_weather")
)

# Disabling all tool usage
handle = agent_session.generate_reply(
    user_input="Tell me about Paris",
    tool_choice="none"
)
This parameter is really handy because it gives you the flexibility to control how tools are used, whether for a single call or across an entire session. If you have any questions or want to explore this further, I’m here to help! Sources: Azure OpenAI LLM integration guide | LiveKit Docs | Groq LLM integration guide | LiveKit Docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/llm.py | agents/src/voice/agent_activity.ts
w
how to pass tool name
t
To pass a tool name in the
tool_choice
parameter of
generate_reply
, here’s a simple way to do it: Example:
Copy code
# Force usage of a specific tool
handle = agent_session.generate_reply(
    user_input="What's the weather in Paris?",
    tool_choice=llm.ToolChoice(type="function", name="get_weather")
)
In this example,
llm.ToolChoice
lets you specify the type as
function
and the name of the tool you want to use. Just make sure the tool is registered and available in the agent’s tool list so it works smoothly. If anything isn’t clear or you have more questions, please don’t hesitate to ask—I’m here to help! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...
w
i want genrate reply to just invoke tool but does not say anything on call is it possible?
t
Absolutely! The
generate_reply
function can indeed invoke a tool without creating a reply. Let me walk you through two friendly ways to make this happen: • Silent Tool Execution: If your tool doesn’t return any value (like
None
or simply has no return statement), it will run quietly without sending back a reply. • Suppress Reply Programmatically: If your tool does return something but you’d prefer not to generate a reply, you can use the
function_tools_executed
event handler and call
ev.cancel_tool_reply()
like this:
Copy code
@session.on("function_tools_executed")
def on_function_tools_executed(ev):
    tools = (fnc.name for fnc in ev.function_calls)
    if "your_tool_name" in tools:
        ev.cancel_tool_reply()
This way, your tool runs smoothly without triggering any spoken or written response. I hope this helps clarify things for you! Sources: livekit-agents/livekit/agents/voice/agent_activity.py