How can I make all user messages to run the same f...
# ask-ai
n
How can I make all user messages to run the same function tool?
t
Thanks for reaching out, Hwuiwon. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! Could you help me understand a bit more so I can assist you better? • What exactly do you want the function to do with all user messages? Is it for logging, processing, or something else? • Would you like this function to run alongside the usual conversation, or should it take over and replace the normal responses? • After the function runs, should the agent keep chatting as usual, or should the function's result be the final reply? Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/utils.py
n
I would like the function to process all user incoming user messages, call an endpoint, get response from that endpoint, and use it to generate a response to user This function should take over and replace the normal responses After the function runs, it should be the final reply and it becomes the user turn
t
Hey there! If you want every user message to trigger a function that processes the input, calls an endpoint, and then generates a response, here’s a simple way to do it: 1. Start by creating a function with the
@llm.ai_callable()
decorator. This function will take care of the user input, reach out to your external endpoint, and return the response. 2. Next, plug this function into your agent by passing it as the function context (
fnc_ctx
) when you set up your agent. 3. Make sure your function’s description clearly explains what it does, so the LLM knows to call it for every user message. Here’s a quick example to get you going:
Copy code
import aiohttp
from livekit.agents import llm

class AssistantFnc(llm.FunctionContext):
    @llm.ai_callable()
    async def process_user_message(self, user_input: str):
        """Process user message by calling external endpoint and generating reply."""
        url = "https://your-api-endpoint.com/process"
        async with aiohttp.ClientSession() as session:
            async with session.post(url, json={"message": user_input}) as response:
                if response.status == 200:
                    api_data = await response.json()
                    return api_data['result']
                else:
                    return f"Error: {response.status}"
Then, just pass this function context to your agent when you initialize it:
Copy code
fnc_ctx = AssistantFnc()
agent = YourAgentClass(..., fnc_ctx=fnc_ctx)
With this setup, your function will take charge and replace the usual responses, making its output the final reply to your users. If you have any questions or want to tweak it further, just let me know! Sources: Function calling with Voice Agents | LiveKit Docs | Function calling with Voice Agents | LiveKit Docs | tool_calling/call_function_tool.py | Agent speech and audio | LiveKit Docs | Tool definition and use | LiveKit Docs
n
Is it possible to use ctx in AssistantFnc?
t
Absolutely, you can use
ctx
in
AssistantFnc
! It’s your gateway to the room context, letting you interact with the local participant, make RPC calls, and tap into other room-related features. Here’s a quick example of how you might use
ctx
to perform an RPC call:
Copy code
class AssistantFnc(llm.FunctionContext):
    @llm.ai_callable()
    async def get_user_location(self, high_accuracy: bool = False):
        """Retrieve the user's current geolocation as lat/lng."""
        try:
            return await ctx.room.local_participant.perform_rpc(
                destination_identity=participant.identity,
                method="getUserLocation",
                payload=json.dumps({"highAccuracy": high_accuracy}),
                response_timeout=10.0 if high_accuracy else 5.0,
            )
        except Exception:
            return "Unable to retrieve user location"
This way, you can easily forward function calls to frontend apps or grab data that’s only available on the client side. If you have any questions or want to explore more, just let me know! Sources: Function calling with Voice Agents | LiveKit Docs
n
I get
Any
for fnc_ctx
t
Hey! Could you help me understand exactly where you’re seeing the
Any
type for
fnc_ctx
? For example: • Is it when you’re accessing
agent.fncCtx
? • Or maybe when you’re creating a function context? • Also, are you using TypeScript? If yes, is your TypeScript setup using strict type checking? Once I know a bit more, I’d be happy to guide you through fixing this! Sources: RealtimeSession | LiveKit Agents | MultimodalAgent | LiveKit Agents | VoicePipelineAgent | LiveKit Agents | LLMStream | LiveKit Agents | agents/src/llm/function_context.ts
n
can i override llm completely
t
Absolutely, you can fully override the LLM in LiveKit! Here are some friendly ways to do it: • Override the `llm_node`: If you’re working with voice agents, you can customize the
llm_node
method to tailor how the LLM behaves—whether that’s tweaking the chat context, managing tool calls, or even plugging in your own LLM provider. • Create a Custom LLM: Feel free to build your own LLM by extending the base
LLM
class. LiveKit has handy examples with OpenAI, Anthropic, and AWS Bedrock integrations that you can check out for inspiration. • Use Fallback and Multiple LLMs: The
FallbackAdapter
is great if you want to run multiple LLMs and have automatic fallback options to keep things smooth. • Agent Configuration: When you set up an agent, you can easily specify your custom LLM using the
llm
parameter. These options give you lots of flexibility to shape how the LLM works in your LiveKit setup. If you want, I’m here to help you get started with any of these—just let me know! Sources: Voice pipeline nodes | LiveKit Docs | livekit-plugins/livekit-plugins-google/livekit/plugins/google/llm.py