how can i detect when the agent is going to make a...
# ask-ai
a
how can i detect when the agent is going to make a tool call? can i listen to some event?
t
Thanks for reaching out, Sebastián. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm here to help you understand how to detect when an agent is about to make a tool call. You can do this by paying attention to specific events or using callbacks: • FunctionCallEvent: This event happens right when a function call starts. You can keep an eye on it through the
RunResult.events
class, which gathers all the events during the agent’s execution. • Custom Agent Overrides: If you override the
llm_node
method in a custom agent, you can handle
ChatChunk
instances as they come in the response stream. This way, you can spot tool calls in real-time, even before they actually run. • Post-Execution Monitoring: After a tool call finishes, you can use the
function_tools_executed
event on the
AgentSession
object to catch the completed calls and see their results. By using these approaches, you’ll be able to keep track of tool call decisions smoothly and respond to them effectively. If you have any questions or want to explore these methods further, I’m here to guide you! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | agent.py
a
how would the approach be with FunctionCallEvent and the CustomAgentOverrides
t
I'd love to guide you through how you can use FunctionCallEvent and Custom Agent Overrides to detect tool calls. Let's break it down step-by-step: • FunctionCallEvent: a. During agent execution, you can access the
RunResult.events
object. This handy feature collects all events, including
FunctionCallEvent
, which is triggered whenever a tool call starts. b. From there, you can filter these events to find
FunctionCallEvent
instances and pull out important details about the tool call. • Custom Agent Overrides: a. You’ll want to override the
llm_node
method in your custom agent implementation. b. Inside this overridden method, you can work with the
ChatChunk
instances coming through the response stream. c. This way, you can spot any
tool_calls
in the response and even trigger your own custom behavior or logging before the tool actually runs. Using these approaches, you’ll be able to keep a close eye on tool call decisions and respond to them smoothly—whether by watching events or catching decisions as they happen. If you have any questions or want to dive deeper, I’m here to help!
a
do you a code example for each in python?
t
I'm happy to share some Python code examples that show how you can use FunctionCallEvent and Custom Agent Overrides to detect tool calls. Let's walk through them together: 1. Using FunctionCallEvent:
Copy code
from livekit.agents import AgentSession

async def detect_function_call():
    async with AgentSession() as session:
        def on_function_tools_executed(event):
            print(f"Function call detected: {event.function_calls}")
        
        session.on("function_tools_executed", on_function_tools_executed)
        await session.run(user_input="What is the weather in New York?")
2. Using Custom Agent Overrides:
Copy code
from livekit.agents import Agent
from livekit.agents.llm import ChatChunk

class CustomAgent(Agent):
    async def llm_node(self, chat_ctx, tools, model_settings):
        async for chunk in super().llm_node(chat_ctx, tools, model_settings):
            if isinstance(chunk, ChatChunk) and chunk.delta and chunk.delta.tool_calls:
                print(f"Tool call detected: {chunk.delta.tool_calls[0].name}")
            yield chunk
These examples are designed to help you listen for tool calls, either by responding to events or by customizing the agent’s behavior to catch tool call decisions as they happen. If you have any questions or need further clarification, feel free to ask—I’m here to help! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | agent.py
a
the first option is too late for me ( i need to detect prior to the tool calling that we are going to make it) the second option isn't working
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!
a
so there's no way for me to know when the tool call begins executing? there's a debug log but i don't know if i have an event for that