is it possible to intercept tool calling response ...
# ask-ai
f
is it possible to intercept tool calling response before feeding it to llm?
t
Thanks for reaching out, Janak. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can intercept tool calling responses before they reach the LLM in LiveKit. This is done by overriding the
llm_node
within the pipeline node system. The
llm_node
manages tool invocations and their responses, giving you the flexibility to customize how these are handled. For instance, you might want to tweak the chat context or adjust the tool responses before they get sent to the LLM. Here's a friendly example to guide you:
Copy code
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
This method really puts you in the driver’s seat to manage how tools are executed and how their responses flow through. If you’d like, I’m here to help you step through the implementation or answer any questions you might have! Sources: agent.py | agents/src/inference/llm.ts | Tool definition and use | LiveKit docs | livekit-agents/livekit/agents/voice/agent_activity.py | Pipeline nodes and hooks | LiveKit docs