```llm_model = openai.LLM.with_cerebras(model="lla...
# ask-ai
a
Copy code
llm_model = openai.LLM.with_cerebras(model="llama-3.3-70b", api_key=CEREBRAS_API_KEY)
I want to change the LLM and use openAi provider
t
Thanks for reaching out, BANDREDDY. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're switching from Cerebras to OpenAI — here's how to do that. 1) Ensure the OpenAI plugin is installed:
Copy code
uv add "livekit-agents[openai]~=1.2"
2) Set your OpenAI API key in your environment (example
.env
):
Copy code
OPENAI_API_KEY=your_openai_api_key
3) Replace the Cerebras call with the OpenAI constructor. Example Python:
Copy code
from livekit.plugins import openai

llm_model = openai.LLM(model="gpt-4o-mini")
Optional parameters:
Copy code
llm_model = openai.LLM(
  model="gpt-4o",
  temperature=0.8,
  tool_choice="auto"
)
If you want, tell me which OpenAI model and language (Python/JS/other) you’re using and I’ll give the exact code snippet for your setup. Sources: OpenAI LLM plugin guide | LiveKit Docs | OpenAI LLM models | LiveKit Docs
a
Is it possible to store the conservation in to db?
t
Yes — you can store conversations to a database. Below are the practical options and short examples so you can pick what fits your use case. Which part of storing conversation are you asking about — saving the full transcript at session end, saving each message in real time, or attaching custom metadata/state for later lookup? If helpful, I can provide a focused snippet for your stack (Python/Node), and example schemas for PostgreSQL or MongoDB. Summary of options • Save full transcript on session end: use
session.history.to_dict()
and persist when the session shuts down (good for complete transcripts and session reports). • Save messages in real time: subscribe to
conversation_item_added
events and write each message to your DB as it arrives (prevents data loss mid-session). • Use session reports: call
ctx.make_session_report()
in
on_session_end
to produce a structured JSON (includes timestamps, metadata, events) suitable for audit or analytics. • Store custom state: use
session.userdata
or
JobContext.metadata
to persist user-specific or session-specific fields you want indexed. Short Python examples 1) Save transcript on shutdown
Copy code
async def write_transcript():
    transcript_data = session.history.to_dict()
    await save_to_db(transcript_data)  # implement save_to_db for your DB

ctx.add_shutdown_callback(write_transcript)
2) Save messages in real time
Copy code
@session.on("conversation_item_added")
def on_conversation_item_added(event):
    msg = {
        "role": event.item.role,
        "content": event.item.text_content,
        "timestamp": event.item.timestamp,
        "session_id": session.id
    }
    save_message_to_db(msg)  # implement save_message_to_db
3) Create session report at end
Copy code
def on_session_end():
    report = ctx.make_session_report()
    save_report_to_db(report)
Storage considerations • Schema: store session_id, message_id, role, content, timestamp, tool_calls, and any metadata (user_id, room_id). • Security: encrypt or redact sensitive content as required. • Idempotency: design inserts so repeated events don't duplicate records (use message_id). • Performance: batch writes for high-throughput, or use a message queue for durability. Tell me which DB (Postgres, MongoDB, etc.) and language (Python/JS) you use and whether you want a full example schema or code to save tool outputs and timestamps — I’ll provide that next. Sources: Observability data hooks | LiveKit docs | Events and error handling | LiveKit docs | External data and RAG | LiveKit Docs
a
I just want to go by the real time:
Copy code
# ⭐ REAL-TIME TURN LOGGING
        @session_agent.on("conversation_item_added")
        def on_conversation_item_added(event):
            if hasattr(event.item, "text_content"):
                role = event.item.role
                message = event.item.text_content
                ts = datetime.utcnow().isoformat()

                print(f"📥 New message [{role}]: {message}")
                asyncio.create_task(
                    userdata.save_turn(role, message, ts)
                )

        # ⭐ When session closes (final safety)
        @session_agent.on("close")
        def on_close(_):
            print("🛑 Session ended.")

            async def cleanup():
                await userdata.close()

            asyncio.create_task(cleanup())
Also is it possible to store the complete Audio clips?
t
Thanks — your real-time logging code is correct and will work well for saving turns as they arrive. Regarding audio clips: yes, you can store complete audio recordings. You have two recommended options: • Use LiveKit Egress (room or track recording) to record the entire session to files (WAV/OGG/etc.) and upload to cloud storage (S3/GCS). This is the recommended production approach for full-session audio. • Enable agent observability recording by starting the session with
record=True
. That records the session audio and uploads it after the session ends. Notes and best practices • Egress /
record=True
produce full audio files available after the session ends; audio isn’t streamed as a single downloadable file during the session. • If you need audio during the session, manual buffering of audio frames is possible but not recommended for long sessions due to memory and complexity. • For real-time logging + audio: continue using
conversation_item_added
for message persistence and use Egress or
record=True
to capture audio files. Store message metadata (session_id, timestamps, message_id) alongside audio file references (file path or cloud URL) so you can link turns to audio clips. • Consider encrypting/redacting sensitive audio and using idempotent writes (message_id) to avoid duplicates. If you tell me which DB (Postgres, MongoDB, etc.) and which storage target (S3, GCS, local) you plan to use, I’ll provide a concise code example showing: 1) saving each turn in real time, and 2) recording via Egress or enabling
record=True
and storing the resulting file URL. Sources: Events and error handling | LiveKit docs | Agent session | LiveKit docs | Observability data hooks | LiveKit docs | Capturing the First Few Seconds of a Call for Processing | Agent insights in LiveKit Cloud | LiveKit docs