microscopic-xylophone-84145
12/18/2025, 9:24 AMtall-airline-16756
12/18/2025, 9:25 AMtall-airline-16756
12/18/2025, 9:25 AMapi_key (if the plugin supports that parameter). For your current code, set the env var first.
Quick checks
• Confirm settings.XAI_API_KEY contains the correct key string.
• Ensure the process that runs this code actually sees the environment variable (set it early in the runtime or in the shell/service config that launches the app).
• If you recently upgraded plugins, verify compatibility between the XAI plugin and the OpenAI realtime client.
If you’d like, tell me how you start your app (local run, Docker, or service) and I’ll give the exact place to set the env var for that environment.microscopic-xylophone-84145
12/18/2025, 9:26 AMtall-airline-16756
12/18/2025, 9:26 AMimport os
os.environ["XAI_API_KEY"] = settings.XAI_API_KEY
os.environ["OPENAI_API_KEY"] = settings.XAI_API_KEY
Then create your session as you already do.
2) Confirm the key string is correct and visible to the process that runs the code (set it in the shell, Docker env, or service config that starts the app).
3) Verify plugin/SDK versions match the docs page you linked — if you recently upgraded, the plugin may require the OpenAI key to remain present.
If you want, tell me how you run the app (local, Docker, systemd, etc.) and I’ll show the exact place to set the env vars for that environment or suggest a safer way to inject the key.microscopic-xylophone-84145
12/18/2025, 9:33 AMtall-airline-16756
12/18/2025, 9:34 AMos.environ["XAI_API_KEY"] = settings.XAI_API_KEY
◦ Or pass api_key=settings.XAI_API_KEY to RealtimeModel (that param takes precedence).
1. Verify the XAI key itself:
◦ Run: curl -H "Authorization: Bearer $XAI_API_KEY" <https://api.x.ai/v1/models>
◦ 200 = valid REST access; 401 = key invalid/expired.
1. Confirm you have Realtime access:
◦ XAI’s websocket realtime API is not publicly available to everyone. If your key works for REST but the websocket returns 401, your account likely doesn’t have realtime privileges.
1. Use the supported path now (if you don’t have realtime access):
◦ Switch to the standard XAI LLM integration: LLM.with_x_ai(model=..., api_key=...) + separate STT and TTS plugins. That approach works today without websocket realtime access.
If you want, tell me:
• Does the curl test return 200 or 401?
• How are you running the app (local shell, Docker, or service)?
I’ll guide you from there (exact command to test, or how to switch to the supported LLM-with-STT/TTS setup).
Sources: xAI LLM plugin guide | LiveKit Docsmicroscopic-xylophone-84145
12/18/2025, 9:36 AMtall-airline-16756
12/18/2025, 9:36 AMimport os
from livekit.agents import AgentSession
from livekit.plugins import openai as openai_plugin
from livekit.plugins import assemblyai, cartesia, silero
# Ensure your XAI key is set
os.environ["XAI_API_KEY"] = settings.XAI_API_KEY # or set in your environment
grok_llm = openai_plugin.LLM.with_x_ai(
model="grok-3-mini", # pick a supported Grok model
api_key=settings.XAI_API_KEY
)
session = AgentSession(
llm=grok_llm,
stt=assemblyai.STT(), # streaming STT provider
tts=cartesia.TTS(), # TTS provider
vad=silero.VAD.load(), # voice activity detection
preemptive_generation=True,
max_tool_steps=1,
user_away_timeout=180,
min_interruption_duration=0.35,
min_interruption_words=2,
min_endpointing_delay=0.3,
max_endpointing_delay=0.8,
false_interruption_timeout=1.0,
turn_detection="vad",
)
What to do if you specifically need low-latency realtime WebSocket voice (Grok)
• The websocket realtime endpoint (wss://api.x.ai/v1/realtime) is restricted/not publicly available. Even with a valid XAI key you will get 401 unless xAI granted you realtime access.
• If you do have a special realtime access grant from xAI, pass the XAI API key via api_key= or XAI_API_KEY env var and the underlying client may connect — but most customers cannot use that right now.
If you want, tell me:
• The model you want to use (grok-3-mini / grok-beta / other)
• How you run the app (local, Docker, cloud)
I’ll give exact env var placement or a full agent example tuned to your stack.
Sources: xAI LLM plugin guide | LiveKit Docs