is livekit provide on the fly switch for gpt4.1 vs...
# ask-ai
e
is livekit provide on the fly switch for gpt4.1 vs gemini 2.5 flash
t
Thanks for reaching out, Guess. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes — LiveKit supports on-the-fly switching between GPT-4.1 and Gemini 2.5 Flash during a session by updating the AgentSession LLM configuration at runtime. Examples: • Python:
Copy code
# switch to GPT-4.1
session.update_llm("openai/gpt-4.1-mini")

# later switch to Gemini 2.5 Flash
session.update_llm("google/gemini-2.5-flash")
• Node.js:
Copy code
session.updateLlm("openai/gpt-4.1-mini");
session.updateLlm("google/gemini-2.5-flash");
This lets you optimize for cost, latency, or capability within the same conversation. Would you like a code sample showing how to handle state or context when switching models? Sources: Models | LiveKit Docs | OpenAI LLM models | LiveKit Docs | Google Gemini LLM | LiveKit Docs
e
ye slet meknow
t
Great — I’ll send a concise example and notes on preserving context when switching models. Do you want the example in Python or Node.js? Also, are you using LiveKit Inference/AgentSession already or should I include session setup code?
e
python
t
Below is a concise Python example showing how to set up an AgentSession, send messages, switch LLMs on the fly with
update_llm()
, and preserve conversation context (history) across model changes.
Copy code
# Example assumes LiveKit Python SDK with AgentSession-like API is available.
# Pseudocode may need minor adjustments to match your SDK import paths.

from livekit_inference import LiveKitClient, AgentSession

# 1) Create client and session (replace with your auth/config)
client = LiveKitClient(api_key="YOUR_API_KEY", url="https://your-livekit-endpoint")
session = client.create_agent_session(name="assistant-session")

# 2) Start with a default model (GPT-4.1)
session.update_llm("openai/gpt-4.1-mini")

# 3) Function to send a user message and get assistant response
def send_user_message(text):
    # Append user message to session conversation/history
    session.append_user_message(text)

    # Run the agent to get a response (sync or async depending on SDK)
    response = session.run()  # returns assistant message object/str
    # Append assistant reply to history
    session.append_assistant_message(response.text if hasattr(response, "text") else str(response))
    return response

# 4) Preserve context when switching models:
# The session stores conversation history internally. When you call update_llm(),
# history remains available so new model continues the same conversation.
def switch_model(llm_name):
    session.update_llm(llm_name)
    # Optionally adjust system prompt or model-specific params
    # session.update_system_prompt("You are now using Gemini 2.5 Flash...")

# 5) Usage example
resp1 = send_user_message("Hello — can you summarize the meeting notes?")
print("Assistant (GPT-4.1):", resp1)

# Switch to Gemini 2.5 Flash mid-session
switch_model("google/gemini-2.5-flash")

resp2 = send_user_message("Now expand that summary with action items.")
print("Assistant (Gemini 2.5):", resp2)
Notes: • Keep conversation history (user + assistant + system messages) inside the same `AgentSession`; calling
update_llm()
preserves that history so the new model has context. • You can also update system prompts or model params after switching to tune behavior. • Handle any model-specific token/response limits in your application (truncate older history if needed). Would you like a version that shows async usage, explicit history truncation, or a complete session-creation snippet with auth details?