How to authenticate my custom LLM requests from li...
# ask-ai
n
How to authenticate my custom LLM requests from livekit agents on my server
t
Thanks for reaching out, Hari. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you authenticate custom LLM requests from LiveKit agents on your server, here’s a friendly guide based on your setup: • Using LiveKit Inference Service: You’ll want to generate a JWT access token using your
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
. Then, include this token as the
api_key
in your HTTP requests. Don’t worry, we have example code ready for both Python and JavaScript to make this easier for you. • Using Third-Party LLMs: If you’re working with providers like OpenRouter, Letta, or Azure OpenAI, you’ll need to set up API keys accordingly. Each provider has its own way of handling credentials, whether that’s through environment variables or passing them directly as parameters. • Server-Side Token Validation: To keep everything secure, you can validate JWT tokens for incoming requests using the
AccessToken.verify()
method along with your API secret. This step helps ensure safe connections to your LiveKit room. If you’d like, I’m here to provide detailed examples or walk you through any part of this setup. Just let me know how I can assist! Sources: Core Platform Architecture & Authentication | livekit/livekit_composite | D... | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/llm.py | agents/src/inference/llm.ts | OpenRouter LLM plugin guide | LiveKit docs
n
Provide python example using livekit inference service
t
Here is a Python example for using the LiveKit Inference Service:
Copy code
from livekit.agents.inference import LLM

# Initialize the LLM with your LiveKit API credentials
llm = LLM(
    model="openai/gpt-4o",  # Specify the model you want to use
    api_key="your_livekit_api_key",  # Replace with your LiveKit API key
    api_secret="your_livekit_api_secret"  # Replace with your LiveKit API secret
)

# Example usage: Generate a response from the model
response = llm.generate(prompt="What is LiveKit?")
print(response)
Just be sure to replace
your_livekit_api_key
and
your_livekit_api_secret
with your actual credentials. If you have any questions or would like me to walk you through more examples, I’m here to help—just let me know!
n
Im using custom LLM. I want to authenticate the chat completion requests coming to my dev server for the custom LLM.
t
To help you authenticate chat completion requests for your custom LLM on a development server, you’ll want to include an API key in the
Authorization
header as a Bearer token. Don’t worry, I’ll walk you through it: Using Python:
Copy code
import openai

client = openai.OpenAI(
    base_url="http://localhost:8080/v1",  # Replace with your server's base URL
    api_key="sk-this-is-the-secret-key"  # Replace with your actual API key
)

completion = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the meaning of life?"}
    ]
)
print(completion.choices[0].message)
Using cURL:
Copy code
curl http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-this-is-the-secret-key" \
  -d '{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "What is the meaning of life?"
    }
  ]
}'
Just make sure your server is set up to validate the API key — you can usually do this by setting the key in your server configuration (like
server.api_key
). If anything feels unclear or you want a hand with the setup, please don’t hesitate to ask. I’m here to help! Sources: tools/server/public_simplechat/readme.md