we need to implement feacture of saving recodrding...
# ask-ai
b
we need to implement feacture of saving recodrdings and transcripts of the conversation
t
Thanks for reaching out, Aleh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I can help you implement saving recordings and transcripts. A few quick questions so I can give the right steps: 1. Are you using LiveKit Cloud or a self-hosted LiveKit deployment? 2. Do you need audio-only recordings or video as well? 3. Where do you want to save files (S3, GCP, local storage)? 4. Do you want transcripts saved after the session ends, or streamed/processed in real time? What I can provide next once you answer: • Example code to start Room Composite Egress to save audio/video to S3 (or other targets). • Example of persisting agent/user transcriptions (listening to
lk.transcription
/
user_input_transcribed
and saving
session.history
). • Guidance on Auto Egress or Agent Observability if you’re on LiveKit Cloud. Which options do you want to proceed with? Sources: Text and transcriptions | LiveKit docs | Observability data hooks | LiveKit docs | Events and error handling | LiveKit docs | Auto egress | LiveKit docs | Agent insights in LiveKit Cloud | LiveKit docs
b
1 azure 2 audio only 3 azure 4 on session closed(in any cases)
t
Great — thanks for the details. Below are concise, actionable steps and example snippets to: • start an audio-only recording (Room Composite Egress) that writes to Azure Blob Storage, and • save the session transcript to Azure when the session closes (in any case). Please adapt the snippets to your app structure and language/runtime. 1) Environment variables (set these in your runtime) • LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET • AZURE_ACCOUNT_NAME, AZURE_ACCOUNT_KEY (or AZURE_STORAGE_CONNECTION_STRING) • AZURE_CONTAINER_NAME 2) Start audio-only Room Composite Egress (start when session begins) • Use
RoomCompositeEgressRequest
with
audio_only=True
and an
AzureBlobUpload
file output. • Example (Python async):
Copy code
from livekit import api
import os

async def start_recording(room_name):
    lkapi = api.LiveKitAPI(os.getenv("LIVEKIT_URL"),
                          os.getenv("LIVEKIT_API_KEY"),
                          os.getenv("LIVEKIT_API_SECRET"))

    req = api.RoomCompositeEgressRequest(
        room_name=room_name,
        audio_only=True,
        file_outputs=[
            api.EncodedFileOutput(
                filepath=f"{room_name}/recording_{room_name}.ogg",
                file_type=api.EncodedFileType.OGG,
                azure=api.AzureBlobUpload(
                    account_name=os.getenv("AZURE_ACCOUNT_NAME"),
                    account_key=os.getenv("AZURE_ACCOUNT_KEY"),
                    container_name=os.getenv("AZURE_CONTAINER_NAME"),
                ),
            )
        ],
    )

    info = await lkapi.egress.start_room_composite_egress(req)
    await lkapi.aclose()
    return info.egress_id
• Store the returned
egress_id
so you can stop it later if needed. 3) Persist transcripts on session close (always run on shutdown) • Use
session.history.to_dict()
(session.history is populated via transcription/chat events) and upload to Azure Blob Storage during your session shutdown callback. • Example using Azure Blob SDK:
Copy code
import json
from datetime import datetime
import os
from azure.storage.blob import BlobClient

def upload_transcript_to_azure(transcript_dict, room_name):
    ts = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
    blob_name = f"{room_name}/transcript_{room_name}_{ts}.json"
    connect_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
    blob = BlobClient.from_connection_string(conn_str=connect_str,
                                            container_name=os.getenv("AZURE_CONTAINER_NAME"),
                                            blob_name=blob_name)
    blob.upload_blob(json.dumps(transcript_dict, indent=2), overwrite=True)

# In your shutdown handler / session end callback:
async def on_session_end(ctx):
    # ensure you capture session.history at shutdown
    if session.history:
        transcript = session.history.to_dict()
        upload_transcript_to_azure(transcript, ctx.room.name)

    # optional: stop egress if you started it manually
    if hasattr(ctx, "egress_id"):
        lkapi = api.LiveKitAPI(os.getenv("LIVEKIT_URL"),
                              os.getenv("LIVEKIT_API_KEY"),
                              os.getenv("LIVEKIT_API_SECRET"))
        await lkapi.egress.stop_egress(ctx.egress_id)
        await lkapi.aclose()
4) Ensure coverage for all session-end cases • Register your transcript upload code in the same shutdown/job lifecycle hook that always runs on session end (graceful close, error, participant disconnect). Use your platform’s shutdown/ job lifecycle event to guarantee execution. 5) Optional/fine points • Filename paths include the room name and timestamp to avoid collisions. • You can let LiveKit auto-stop egress when the room ends; otherwise call
stop_egress(egress_id)
in shutdown. • If you prefer streaming transcripts as they arrive, subscribe to
user_input_transcribed
events and append to a buffer; still flush and upload on shutdown. Would you like: • the code adapted to another language (Node/TS), or • a sample that stores transcripts incrementally during the session (for very long sessions)? Sources: Events and error handling | LiveKit docs | Observability data hooks | LiveKit docs | Job lifecycle | LiveKit docs | Egress examples | LiveKit docs
b
do i need to deploy egress in this case?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
b
is there session id in AgentSession?