is it correct to use in livekit_agent.py token_pro...
# ask-ai
c
is it correct to use in livekit_agent.py token_provider = get_bearer_token_provider( DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default") # Use LLMOptions for chat parameters like max_tokens and temperature. llm_options = LLMOptions( max_tokens=32768, temperature=0.3, top_p=0.95, # The system prompt from your original code is now handled by instructions ) entra_token = get_entra_token() azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME"), azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), api_key=os.getenv("AZURE_OPENAI_API_KEY"), api_version=os.getenv("OPENAI_API_VERSION"), llm = openai.LLM.with_azure( azure_deployment=azure_deployment, azure_endpoint=azure_endpoint, api_version=api_version, entra_token=entra_token, options=llm_options, )
t
Thanks for reaching out, rajesh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you please confirm if the code snippet you shared is complete? Also, if you could share a bit more about the context or any specific errors you're seeing, that would be really helpful. I want to make sure we get the setup for your token_provider and LLM with Azure OpenAI just right for you. Sources: Azure OpenAI LLM plugin guide | LiveKit docs | examples/voice_agents/langfuse_trace.py
c
[7/7] RUN python livekit_agent.py download-files:
2.499 Traceback (most recent call last): 2.499 File "/app/livekit_agent.py", line 73, in <module> 2.499 from livekit.plugins.openai import LLMOptions 2.499 ImportError: cannot import name 'LLMOptions' from 'livekit.plugins.openai' (/usr/local/lib/python3.11/site-packages/livekit/plugins/openai/__init__.py) ------ Dockerfile.agent:36 -------------------- from openai import AzureOpenAI from azure.identity import DefaultAzureCredential, get_bearer_token_provider from livekit.plugins import openai from livekit.plugins.openai import LLMOptions # Load environment variables load_dotenv()
t
I understand how confusing this can be! The error happens because LLMOptions isn’t a publicly exported class in the LiveKit OpenAI plugin. Instead, you can create an instance of the LLM class directly with the parameters you need. Here’s a simple example to help you out:
Copy code
from livekit.plugins import openai

llm_instance = openai.LLM(
    model="gpt-4o-mini",
    temperature=0.3,
    api_key="your-api-key"
)
If you want to organize your configuration data, using a dictionary or making your own configuration class works great. Please feel free to reach out if you have any more questions or need a hand with anything else! Sources: examples/voice_agents/speedup_output_audio.py | examples/src/comprehensive_test.ts | OpenAI LLM plugin guide | LiveKit Docs | agent-examples/agent_examples/pipeline_agent.py | Template System & Project Scaffolding | livekit/livekit_composite | DeepWik...
c
i am using gpt4.1 from azure with certain parameters like max_tokens=32768, temperature=0.3,
.
t
To help you configure Azure OpenAI GPT-4.1 with parameters like max_tokens and temperature, here’s a friendly Python example you can follow:
Copy code
from livekit.plugins.openai import RealtimeModel

model = RealtimeModel.with_azure(
    azure_deployment="your-gpt-41-deployment-name",
    azure_endpoint="https://your-azure-endpoint.openai.azure.com",
    api_version="2024-10-01-preview",  # Required
    api_key="your-azure-api-key",
    model="gpt-4.1",  # Specify model if needed
    temperature=0.3,
    max_response_output_tokens=32768
)
Just make sure your Azure deployment name matches a GPT-4.1 model that you’ve set up in your Azure account. If you have any questions or need a hand with anything else, please don’t hesitate to ask—I’m here to help! Sources: Azure OpenAI LLM plugin guide | LiveKit docs | agents/src/inference/llm.ts | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | agent/main.py | Build voice, video, and physical AI | LiveKit
To help you configure Azure OpenAI GPT-4.1 with parameters like max_tokens and temperature, here’s a friendly Python example you can follow:
Copy code
from livekit.plugins.openai import RealtimeModel

model = RealtimeModel.with_azure(
    azure_deployment="your-gpt-41-deployment-name",
    azure_endpoint="https://your-azure-endpoint.openai.azure.com",
    api_version="2024-10-01-preview",  # Required
    api_key="your-azure-api-key",
    model="gpt-4.1",  # Specify model if needed
    temperature=0.3,
    max_response_output_tokens=32768
)
Just make sure that your Azure deployment name matches a GPT-4.1 model you’ve set up in your Azure account. If you run into any questions or need a hand with anything else, I’m here to help!
c
------ > [7/7] RUN python livekit_agent.py download-files: 2.518 Traceback (most recent call last): 2.518 File "/app/livekit_agent.py", line 73, in <module> 2.518 from livekit.plugins.openai import RealtimeModel 2.518 ImportError: cannot import name 'RealtimeModel' from 'livekit.plugins.openai' (/usr/local/lib/python3.11/site-packages/livekit/plugins/openai/__init__.py)
t
I understand how frustrating this error can be, but don’t worry—we’ll get it sorted out together! The issue happens because RealtimeModel isn’t directly exported from livekit.plugins.openai. Instead, it should be imported from the realtime submodule. You can update your import statement like this:
Copy code
from livekit.plugins.openai.realtime import RealtimeModel
Also, please double-check that the livekit-plugins-openai package is installed by running:
Copy code
pip install livekit-plugins-openai
If you’re still running into trouble after that, it might be helpful to verify your environment and dependency versions. Feel free to reach out anytime—I’m here to help! Sources: examples/voice_agents/realtime_load_chat_history.py | OpenAI Realtime API plugin guide | LiveKit docs
c
this is alredy done
t
If the RealtimeModel import isn’t working even though the path and package seem correct, don’t worry! Let’s walk through some steps together to get this sorted out: 1. Verify Dependencies: First, let’s make sure you have all the necessary packages like aiohttp, pydantic, and openai installed. You can do this by running:
Copy code
pip install aiohttp pydantic openai
2. Check Python Version: It’s important to be on Python 3.8 or higher. You can check your version with:
Copy code
python --version
3. Test Core Imports: Try importing the base class to see if that works:
Copy code
from livekit.agents import llm
print(llm.__file__)
4. Reinstall Plugin: Sometimes a fresh install helps! You can uninstall and reinstall the plugin like this:
Copy code
pip uninstall livekit-plugins-openai
pip install livekit-plugins-openai
5. Avoid Naming Conflicts: Double-check that your script isn’t named livekit.py or openai.py, since that can cause confusion with the actual packages. 6. Environment Isolation: If you’re using a virtual environment, please make sure the package is installed in the one you’re currently working in. 7. Minimal Example: Finally, try running a simple script to confirm the import works:
Copy code
from livekit.plugins.openai.realtime import RealtimeModel
print("Import successful")
If you’re still seeing issues or get any specific error messages, just let me know—I’m here to help you through it! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | OpenAI and LiveKit partner to turn Advanced Voice into an API
c
i want to use below code for llm configuration in livekit_agent.py file import os import base64 from openai import AzureOpenAI from azure.identity import DefaultAzureCredential, get_bearer_token_provider endpoint = os.getenv("ENDPOINT_URL", "https://neuravoice.openai.azure.com/") deployment = os.getenv("DEPLOYMENT_NAME", "gpt-4.1") # Initialize Azure OpenAI client with Entra ID authentication token_provider = get_bearer_token_provider( DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default" ) client = AzureOpenAI( azure_endpoint=endpoint, azure_ad_token_provider=token_provider, api_version="2025-01-01-preview", ) # IMAGE_PATH = "YOUR_IMAGE_PATH" # encoded_image = base64.b64encode(open(IMAGE_PATH, 'rb').read()).decode('ascii') chat_prompt = [ { "role": "system", "content": [ { "type": "text", "text": "You are an AI assistant that helps people find information." } ] }, { "role": "user", "content": [ { "type": "text", "text": "Hi" } ] }, { "role": "assistant", "content": [ { "type": "text", "text": "Hello! How can I help you today?" } ] } ] # Include speech result if speech is enabled messages = chat_prompt completion = client.chat.completions.create( model=deployment, messages=messages, max_tokens=13107, temperature=0.7, top_p=0.95, frequency_penalty=0, presence_penalty=0, stop=None, stream=False ) print(completion.to_json())
t
To configure Azure OpenAI with Entra ID authentication and use chat completions in LiveKit, let's walk through these steps together: 1. Authentication Setup: - You'll want to use Microsoft Entra ID tokens for authentication. - Make sure to set environment variables such as AZURE_OPENAI_ENTRA_TOKEN, AZURE_OPENAI_ENDPOINT, and OPENAI_API_VERSION in your .env file. - Alternatively, you can pass the entra_token directly when creating your client instance. 2. Client Configuration: - Here's how you can initialize the client with Azure-specific parameters:
Copy code
from livekit.plugins.openai import LLM

llm_instance = LLM.with_azure(
    azure_deployment="your-deployment-name",
    azure_endpoint="https://your-endpoint.azure.com",
    api_version="2024-xx-xx",
    entra_token="your-entra-token"  # or use AZURE_OPENAI_ENTRA_TOKEN env var
)
3. Using Chat Completions: - Once your client is set up, you can use it to handle chat completions by specifying parameters like max_tokens, temperature, and messages. If you have any questions or need more detailed examples tailored to your setup, please don't hesitate to ask—I'm here to help! Sources: Azure OpenAI LLM plugin guide | LiveKit docs | plugins/openai/src/realtime/realtime_model.ts | livekit-plugins/livekit-plugins-azure/livekit/plugins/azure/stt.py | Azure OpenAI Realtime API and LiveKit | LiveKit Docs
c
main parameter is this how to use it ? max_tokens=32768,
t
The max_tokens parameter can definitely be set when you're using Azure OpenAI GPT-4.1 with LiveKit. Just keep in mind that the maximum value for max_tokens (like 32768) really depends on your specific Azure OpenAI deployment and the model configuration you have. To use this parameter, make sure it’s included in your model options or API call. If you run into any trouble or have questions, please don’t hesitate to reach out—I’m here to help! Sources: tests/test_config.py | plugins/openai/src/realtime/realtime_model.ts | OpenAI LLM models | LiveKit Docs
c
where to configure it?
t
I'm glad you asked about the max_tokens parameter! You’ll want to set it as part of your API call when creating a chat completion. For instance, in your Azure OpenAI GPT-4.1 setup, you can include it like this:
Copy code
completion = client.chat.completions.create(
    model="gpt-4.1",
    messages=messages,
    max_tokens=32768,
    temperature=0.7,
    top_p=0.95,
    frequency_penalty=0,
    presence_penalty=0,
    stop=None,
    stream=False
)
This way, the max_tokens value is applied directly to your specific request. If you have any questions or need a hand with anything else, just let me know—I’m here to help!
c
how to use it in llm = openai.LLM.with_azure( azure_deployment=azure_deployment, azure_endpoint=azure_endpoint, api_version=api_version, entra_token=entra_token )
t
The max_tokens parameter isn’t directly supported in the openai.LLM.with_azure method, but don’t worry—I’m here to help you navigate this! Depending on what you’re working on, you can use the equivalent parameter that fits your needs: 1. For Standard LLM (Non-Realtime): You’ll want to use max_completion_tokens to set the maximum number of output tokens. Here’s a quick example:
Copy code
from livekit.plugins import openai

llm_instance = openai.LLM.with_azure(
    model="gpt-4.1",
    azure_endpoint="https://your-azure-endpoint",
    azure_deployment="your-deployment-name",
    api_version="your-api-version",
    api_key="your-api-key",
    max_completion_tokens=32768
)
2. For Realtime API: In this case, max_response_output_tokens is what you’ll use for real-time interactions. Check out this example:
Copy code
from livekit.plugins.openai.realtime import RealtimeModel

model = RealtimeModel.with_azure(
    azure_deployment="gpt-4.1-deployment",
    azure_endpoint="https://your-endpoint.azure.com",
    api_version="2024-10-01-preview",
    api_key="your-api-key",
    max_response_output_tokens=32768
)
If anything feels unclear or you want to dive deeper into this, just let me know—I’m happy to help you every step of the way! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | agents/src/inference/llm.ts
c
unexpected keyword argument 'max_completion_tokens'\nTraceback (most recent call last):\n File \"/app/livekit_agent.py\", line 403, in entrypoint\n bot = NeuraVoiceAgent(metrics_collector=metrics_collector)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/app/livekit_agent.py\", line 238, in __init__\n llm = openai.LLM.with_azure(\n ^^^^^^^^^^^^^^^^^^^^^^\nTypeError: LLM.with_azure() got an unexpected keyword argument 'max_completion_tokens'", "level": "ERROR", "name": "__mp_main__", "room": "sbx-1rh8j8-NY9JFCtnKPTttTczkcydDH", "pid": 108, "job_id": "AJ_stcShCv3V4by", "timestamp": "2025-11-01T121727.813365+00:00"} {"message": "unhandled exception while running the job task\nTraceback (most recent call last):\n File \"/usr/local/lib/python3.11/site-packages/opentelemetry/util/_decorator.py\", line 71, in async_wrapper\n return await func(*args, **kwargs) # type: ignore\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.11/site-packages/livekit/agents/ipc/job_proc_lazy_main.py\", line 240, in _traceable_entrypoint\n await self._job_entrypoint_fnc(job_ctx)\n File \"/app/livekit_agent.py\", line 403, in entrypoint\n bot = NeuraVoiceAgent(metrics_collector=metrics_collector)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/app/livekit_agent.py\", line 238, in __init__\n llm = openai.LLM.with_azure(\n ^^^^^^^^^^^^^^^^^^^^^^\nTypeError: LLM.with_azure() got an unexpected keyword argument 'max_completion_tokens'", "level": "ERROR", "name": "livekit.agents", "room": "sbx-1rh8j8-NY9JFCtnKPTttTczkcydDH", "pid": 108, "job_id": "AJ_stcShCv3V4by", "timestamp": "2025-11-01T121727.814337+00:00"}
t
The error happens because the LLM.with_azure() method doesn’t currently support the max_completion_tokens parameter. But don’t worry! You can still control the maximum number of tokens by trying one of these friendly approaches: 1. Use the Standard LLM Constructor:
Copy code
from livekit.plugins.openai import LLM
import openai

# Create Azure client manually
azure_client = openai.AsyncAzureOpenAI(
    azure_endpoint="https://your-resource.openai.azure.com/",
    azure_deployment="your-deployment-name",
    api_version="2024-02-01",
    api_key="your-api-key"
)

# Use the main LLM constructor with max_completion_tokens
llm = LLM(
    model="gpt-4o",
    client=azure_client,
    max_completion_tokens=1000,  # This parameter is supported here
    temperature=0.7
)
2. Use LiveKit Inference:
Copy code
from livekit.agents.inference import LLM

llm = LLM(
    model="openai/gpt-4o",
    provider="azureopenai",
    extra_kwargs={
        "max_completion_tokens": 1000,  # Token limit control
        "temperature": 0.7
    }
)
Both of these options let you set the token limit while staying compatible with Azure OpenAI. If you have any questions or want me to walk you through the steps, just let me know—I’m here to help! Sources: OpenAI LLM models | LiveKit Docs | livekit-agents/livekit/agents/llm/llm.py