some-grass-64312
10/29/2025, 9:52 PMtall-airline-16756
10/29/2025, 9:52 PMtall-airline-16756
10/29/2025, 9:52 PMAgentSession. You have two friendly options to choose from:
1. Using an OpenAI-Compatible Endpoint:
• If your API supports OpenAI’s /chat/completions format, you can easily set it up using LiveKit’s built-in inference module. Just set the base_url to your API endpoint, add your API key, and specify the model ID. This approach is straightforward—no need for subclassing—and it even supports streaming responses.
2. Overriding the llm_node Method:
• If your API uses a custom format, you can override the llm_node method in your Agent subclass. This gives you the flexibility to handle custom request and response structures, implement streaming, and manage authentication smoothly. Just keep in mind that this method should be an async generator that yields text chunks or structured ChatChunk objects.
Please feel free to reach out if you'd like detailed examples or any further guidance with either approach—I’m here to support you every step of the way!
Sources: agents/src/inference/llm.ts | Google Gemini LLM | LiveKit Docs | OpenAI LLM models | LiveKit Docs | examples/voice_agents/llamaindex-rag/chat_engine.py | tools/server/public_simplechat/readme.mdsome-grass-64312
10/29/2025, 9:54 PMtall-airline-16756
10/29/2025, 9:54 PMAgentSession. Here it is:
from livekit.plugins.openai import LLM
# Configure the LLM instance
llm_instance = LLM(
model="your-model-id", # Replace with your model ID
api_key="your-api-key", # Replace with your API key
base_url="<BASE_API_URL>", # Replace with your API endpoint
extra_headers={"X-Custom-Header": "value"}, # Optional custom headers
temperature=0.7, # Adjust as needed
)
# Use the LLM instance in your agent
from livekit.agents import Agent
from livekit.plugins import deepgram, silero
agent = Agent(
instructions="You are a helpful assistant.",
vad=silero.VAD.load(),
stt=deepgram.STT(),
llm=llm_instance, # Pass the configured LLM instance
tts=None, # Replace with your TTS instance if needed
)
# Start the AgentSession
from livekit.agents import AgentSession
session = AgentSession()
await session.start(agent=agent, room="your-room-id") # Replace with your room ID
Just be sure to swap out placeholders like your-model-id, your-api-key, <BASE_API_URL>, and your-room-id with your actual details. If anything feels unclear or if you'd like me to walk you through more examples, please don’t hesitate to ask—I’m here to help!some-grass-64312
10/30/2025, 12:06 PMtall-airline-16756
10/30/2025, 12:07 PMAgentSession by setting up the LLM instance with that model string. Here’s a simple example to guide you:
from livekit.plugins.openai import LLM
from livekit.agents import Agent
from livekit.plugins import deepgram, silero
from livekit.agents import AgentSession
# Configure the LLM instance with the model string
llm_instance = LLM(
model="openai/gpt-5", # Pass your model string here
api_key="your-api-key", # Replace with your API key
base_url="<BASE_API_URL>", # Replace with your API endpoint
temperature=0.7, # Adjust as needed
)
# Create the agent with the configured LLM
agent = Agent(
instructions="You are a helpful assistant.",
vad=silero.VAD.load(),
stt=deepgram.STT(),
llm=llm_instance, # Pass the configured LLM instance
tts=None, # Replace with your TTS instance if needed
)
# Start the AgentSession
session = AgentSession()
await session.start(agent=agent, room="your-room-id") # Replace with your room ID
Just be sure to replace placeholders like your-api-key, <BASE_API_URL>, and your-room-id with your actual details. If you have any questions or need a hand with anything else, feel free to ask—I’m here to help!some-grass-64312
10/30/2025, 12:08 PMtall-airline-16756
10/30/2025, 12:08 PMbase_url parameter when setting up your LLM instance. Here’s a friendly example to guide you:
from livekit.plugins.openai import LLM
from livekit.agents import Agent
from livekit.plugins import deepgram, silero
from livekit.agents import AgentSession
# Configure the LLM instance with the model string
llm_instance = LLM(
model="openai/gpt-5", # Pass your model string here
api_key="your-api-key", # Replace with your OpenAI API key
temperature=0.7, # Adjust as needed
)
# Create the agent with the configured LLM
agent = Agent(
instructions="You are a helpful assistant.",
vad=silero.VAD.load(),
stt=deepgram.STT(),
llm=llm_instance, # Pass the configured LLM instance
tts=None, # Replace with your TTS instance if needed
)
# Start the AgentSession
session = AgentSession()
await session.start(agent=agent, room="your-room-id") # Replace with your room ID
With this setup, your LLM will connect to the default OpenAI API endpoint. Just be sure to swap out your-api-key and your-room-id with your actual details. If anything feels unclear or you want to dive deeper, please don’t hesitate to ask—I’m here to help!some-grass-64312
10/30/2025, 12:09 PMtall-airline-16756
10/30/2025, 12:09 PMOPENAI_API_KEY set up in your .env.local file, the LLM instance will automatically pick it up, as long as your environment variables are properly loaded. To make sure everything runs smoothly, it's a good idea to use a library like python-dotenv to load those variables from your .env.local file into your application.
Here’s a friendly example to help you load the environment variables:
from dotenv import load_dotenv
import os
from livekit.plugins.openai import LLM
from livekit.agents import Agent
from livekit.plugins import deepgram, silero
from livekit.agents import AgentSession
# Load environment variables from .env.local
load_dotenv(".env.local")
# Configure the LLM instance
llm_instance = LLM(
model="openai/gpt-5", # Pass your model string here
temperature=0.7, # Adjust as needed
)
# Create the agent with the configured LLM
agent = Agent(
instructions="You are a helpful assistant.",
vad=silero.VAD.load(),
stt=deepgram.STT(),
llm=llm_instance, # Pass the configured LLM instance
tts=None, # Replace with your TTS instance if needed
)
# Start the AgentSession
session = AgentSession()
await session.start(agent=agent, room="your-room-id") # Replace with your room ID
Just double-check that your OPENAI_API_KEY is correctly set in your .env.local file. If you run into any bumps or have questions along the way, please don’t hesitate to reach out—I’m here to help!