how do I add authentication to my tool calls which...
# ask-ai
v
how do I add authentication to my tool calls which are used by the agent in the STT/LMM/TTS configuration?
t
Thanks for reaching out, Ruchir. 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 add authentication to your tool calls in the STT/LMM/TTS configuration! Here are a few friendly approaches you might consider: • Using RunContext: You can pass authentication credentials through the RunContext when your tool runs. For instance, grabbing an API key or token from the context and including it in your API calls works really well. • Environment Variables: A handy way is to store your service API keys in environment variables and then access them within your tool functions. This is quite common for plugins like Speechmatics, LMNT, and Cartesia. • Session-Based Authentication: Another option is passing user-specific tokens during agent initialization and using those in your tool calls. • Error Handling: Don’t forget to set up proper error handling so any authentication hiccups are managed smoothly. If you'd like, I’m here to walk you through detailed examples or help with a specific setup—just let me know! Sources: examples/voice_agents/silent_function_call.py | Tool definition and use | LiveKit Docs | plugins/openai/src/llm.ts | Speechmatics STT plugin guide | LiveKit Docs | LMNT TTS plugin guide | LiveKit Docs
v
how do I access the env variables in the tool call? could you give a code sample
t
Hey there! You can easily access environment variables in your tool calls using the
os
module in Python. Here’s a little code snippet to help you get started:
Copy code
import os
from livekit.agents.llm import function_tool

class MyAgent:
    @function_tool()
    async def access_external_service(self, query: str):
        # Access environment variables
        api_key = os.getenv("EXTERNAL_SERVICE_API_KEY")
        base_url = os.getenv("EXTERNAL_SERVICE_URL", "https://default-url.com")

        if not api_key:
            return "API key not configured"

        # Use the environment variables in your tool logic
        response = await make_api_call(query, headers={"Authorization": f"Bearer {api_key}"})
        return response
Just a quick tip: be sure to set those environment variables on your system or use a
.env
file if you’re working locally. If you hit any bumps or want me to walk you through it, I’m here to help! Sources: packages/sip--um-actually/src/setup-livekit.ts | Secrets management | LiveKit Docs | External data and RAG | LiveKit Docs | Agent Deployment & Docker Configuration | livekit/livekit_composite | DeepW... | basics/tool_calling.py
v
thanks!