how do i record a room?
# ask-ai
r
how do i record a room?
t
Thanks for reaching out, Trevor. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to record a room — I can help. Do you want to: • Start a recording on demand via the Egress API/SDK or CLI, • Have the room automatically recorded when it's created (auto egress), or • Capture audio-only (mixed audio) or full video (MP4/HLS/RTMP)? Tell me which method and the output destination you want (S3 filepath, MP4, HLS, RTMP, etc.), and I’ll give the exact steps or example code. Sources: Composite and web recordings | LiveKit docs | Egress API | LiveKit docs | RoomComposite Egress | LiveKit Docs | Auto egress | LiveKit docs
r
I want audio -- I guess 2? I want to save the recording in S3
t
Thanks — you're looking for an audio-only room recording saved to S3 using the Egress API/SDK. Quick clarifying questions so I give the exact code/config: 1. Do you want to start recordings manually (call Egress to start/stop) or have them start automatically when the room is created? 2. What's the room name (or pattern) you'll record? 3. Which SDK/language do you want the example in (JavaScript/TypeScript, Go, or CLI)? 4. Do you have S3 credentials and the target bucket/path ready, and do you want a single mixed audio file (e.g., MP3/MP4 audio) or segmented outputs? Answer those and I’ll provide the exact Egress call example and required permission (
roomRecord
token) setup.
r
1. automatic 2. uuid 3. python 4. yes, mixed
t
Thanks — I’ll give a concise Python example and the exact fields you need to create a room (UUID name) with automatic, mixed audio-only recording to S3. 1) Required permissions • Use a token with
room_create
. Admin API key/secret includes egress permissions so auto egress will run. 2) Minimal Python example (CreateRoom with auto egress to S3)
Copy code
import os
import time
import jwt
import requests
import json

LIVEKIT_HOST = "https://your-livekit-host.com"  # Replace
API_KEY = os.getenv("LIVEKIT_API_KEY")
API_SECRET = os.getenv("LIVEKIT_API_SECRET")

ROOM_UUID = "a1b2c3d4-e5f6-7890-g1h2-i3j4k5l6m7n8"  # use your UUID
S3_BUCKET = os.getenv("AWS_BUCKET_NAME")
S3_REGION = os.getenv("AWS_REGION")
S3_ACCESS = os.getenv("AWS_ACCESS_KEY_ID")
S3_SECRET = os.getenv("AWS_SECRET_ACCESS_KEY")

def create_jwt(api_key, api_secret, ttl=600):
    now = int(time.time())
    payload = {"iss": api_key, "sub": api_key, "nbf": now, "exp": now + ttl, "room_create": True}
    return jwt.encode(payload, api_secret, algorithm="HS256")

token = create_jwt(API_KEY, API_SECRET)

payload = {
    "name": ROOM_UUID,
    "egress": {
        "room": {
            "audio_only": True,
            "file": {
                "filepath": f"recordings/{ROOM_UUID}.ogg",
                "s3": {
                    "access_key": S3_ACCESS,
                    "secret": S3_SECRET,
                    "bucket": S3_BUCKET,
                    "region": S3_REGION
                }
            }
        }
    }
}

resp = requests.post(
    f"{LIVEKIT_HOST}/twirp/livekit.RoomService/CreateRoom",
    headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"},
    data=json.dumps(payload),
    timeout=15
)

if resp.status_code == 200:
    print(f"Room {ROOM_UUID} created with auto audio recording to S3.")
else:
    print("CreateRoom failed:", resp.status_code, resp.text)
Notes: •
audio_only: true
produces a single mixed audio file (typically OGG/Opus) saved to the specified S3 path. • Filename uses the room UUID in the example; change
filepath
if you prefer a different path/extension. • Ensure your LiveKit host URL and AWS creds/bucket are correct and that the LiveKit key/secret used can create rooms. • If you prefer a different audio format or filename extension, adjust
filepath
and confirm LiveKit supports the desired format for audio-only egress. Would you like this example adapted to use the LiveKit Python client library instead of the HTTP/Twirp call, or help generating the JWT token with a specific library? Sources: RoomComposite Egress | LiveKit Docs | Auto egress | LiveKit docs | Egress API | LiveKit docs
r
it's not a client -- it's the livekit CLI / agent runner that wants to do this
t
Got it — you want automatic, mixed audio-only recordings saved to S3 using the LiveKit CLI / agent runner. Below are the exact options and a ready-to-use CLI approach plus the CreateRoom auto-egress payload you can use from an agent. 1) Quick CLI (start recording for an existing room UUID) • Create a JSON file (audio-record.json):
Copy code
{
  "room_name": "your-room-uuid",
  "audio_only": true,
  "file_outputs": [
    {
      "filepath": "recordings/{room_name}-{time}.ogg",
      "s3": {
        "access_key": "YOUR_AWS_ACCESS_KEY_ID",
        "secret": "YOUR_AWS_SECRET_ACCESS_KEY",
        "region": "YOUR_AWS_REGION",
        "bucket": "YOUR_S3_BUCKET"
      }
    }
  ]
}
• Start egress:
Copy code
lk egress start --type room-composite audio-record.json
This will mix all participant audio into one OGG file and upload to S3. The egress stops automatically when the room ends. 2) Auto-start on room creation (agent or CLI can POST CreateRoom) • Use this CreateRoom payload so recording begins as soon as the room is created:
Copy code
{
  "name": "your-room-uuid",
  "egress": {
    "room": {
      "audio_only": true,
      "file_outputs": [
        {
          "filepath": "recordings/{room_name}.ogg",
          "s3": {
            "access_key": "YOUR_AWS_ACCESS_KEY_ID",
            "secret": "YOUR_AWS_SECRET_ACCESS_KEY",
            "region": "YOUR_AWS_REGION",
            "bucket": "YOUR_S3_BUCKET"
          }
        }
      ]
    }
  }
}
• POST to the RoomService CreateRoom endpoint with an admin token:
Copy code
curl -X POST https://<LIVEKIT_HOST>/twirp/livekit.RoomService/CreateRoom \
  -H "Authorization: Bearer <ADMIN_TOKEN>" \
  -H "Content-Type: application/json" \
  --data-binary @create-room.json
This creates the room and immediately starts the audio-only composite egress that uploads to S3. Notes and reminders • Use an admin API key/secret (or a token that has
room_create
and egress privileges) for CreateRoom/auto-egress. •
audio_only: true
produces a single mixed audio track (commonly OGG/Opus); change
filepath
extension if you need a different container. • If you want this entirely driven by your agent runner, have the agent call CreateRoom with the payload above when provisioning the room. Would you like: • a templated shell script that your agent runner can call, or • the same example converted to a small agent-runner Python snippet that posts CreateRoom? Sources: Egress output types | LiveKit docs | Composite and web recordings | LiveKit docs | Egress API | LiveKit docs | server-sdk-js | livekit/livekit_composite | DeepWiki | Observability data hooks | LiveKit docs
r
I'm running:
Copy code
lkapi = api.LiveKitAPI()
    await lkapi.agent_dispatch.create_dispatch(
        api.CreateAgentDispatchRequest(
            agent_name=call_id,
            room=call_id,
            metadata=f'{{"phone_numbers": {json.dumps(phone_numbers)}}}',
        )
    )
    await lkapi.aclose()
t
Thanks — since your dispatch auto-creates the room, you should create the room with Auto Egress before calling
create_dispatch
. Below is a minimal Python example that integrates into your agent flow: it first creates the room with audio-only auto egress to S3, then creates the dispatch.
Copy code
import json
import asyncio
from livekit import api

async def create_room_and_dispatch(call_id: str, phone_numbers: list):
    lkapi = api.LiveKitAPI(url="https://your-livekit-host", api_key="API_KEY", api_secret="API_SECRET")

    s3 = api.S3Upload(
        bucket="YOUR_S3_BUCKET",
        region="YOUR_AWS_REGION",
        access_key="YOUR_AWS_ACCESS_KEY_ID",
        secret="YOUR_AWS_SECRET_ACCESS_KEY",
    )

    # Auto egress: mixed audio-only file to S3
    room_egress = api.RoomEgress(
        audio_only=True,
        file_outputs=[
            api.EncodedFileOutput(
                filepath=f"recordings/{call_id}.m4a",
                s3=s3,
            )
        ],
    )

    # Create room with auto egress so recording starts immediately
    await lkapi.room.create_room(name=call_id, egress=room_egress)

    # Now create the agent dispatch (room already exists / egress started)
    await lkapi.agent_dispatch.create_dispatch(
        api.CreateAgentDispatchRequest(
            agent_name=call_id,
            room=call_id,
            metadata=json.dumps({"phone_numbers": phone_numbers}),
        )
    )

    await lkapi.aclose()

# usage
# asyncio.run(create_room_and_dispatch("your-room-uuid", ["+1555..."]))
If you prefer to keep your current dispatch-first flow, instead call the Egress API after dispatch to start recording: • Use
lkapi.egress.start_room_composite_egress(...)
with
audio_only=True
and an S3
EncodedFileOutput
. • That approach works but can race with room creation/joins — creating the room with
egress
avoids that. Do you want the post-dispatch example (start egress after create_dispatch) or a shell/CLI variant? Sources: Auto egress | LiveKit docs | RoomComposite Egress | LiveKit Docs | Composite and web recordings | LiveKit docs | Egress API | LiveKit docs
r
I'm starting the room with def run_cli(self, with_agent_name: bool = True): cli.run_app( WorkerOptions( entrypoint_fnc=self.entrypoint, job_memory_warn_mb=2048, job_memory_limit_mb=4096, prewarm_fnc=prewarm, initialize_process_timeout=100, agent_name=self.call_id if with_agent_name else "", ) )
t
Thanks — since you start the agent via
cli.run_app(...)
, the room is usually created by dispatch. To ensure audio-only recording to S3 starts immediately, create the room with Auto Egress before you call
create_dispatch
(so recording begins at room creation). Minimal change to your flow: 1) Create room with audio-only auto egress to S3, then 2) Create the dispatch (agent joins the already-created room). Example (matches your agent usage):
Copy code
from livekit import api
import json
import asyncio

async def prepare_and_dispatch(call_id: str, phone_numbers: list):
    lkapi = api.LiveKitAPI(url="https://your-livekit-host", api_key="API_KEY", api_secret="API_SECRET")

    s3 = api.S3Upload(
        bucket="YOUR_S3_BUCKET",
        region="YOUR_AWS_REGION",
        access_key="YOUR_AWS_ACCESS_KEY_ID",
        secret="YOUR_AWS_SECRET_ACCESS_KEY",
    )

    # Auto egress: mixed audio-only to S3
    room_egress = api.RoomEgress(
        audio_only=True,
        file_outputs=[
            api.EncodedFileOutput(
                filepath=f"recordings/{call_id}.m4a",
                s3=s3,
            )
        ],
    )

    # 1) Create room with auto egress so recording starts immediately
    await lkapi.room.create_room(name=call_id, egress=room_egress)

    # 2) Create the agent dispatch (agent will join the room)
    await lkapi.agent_dispatch.create_dispatch(
        api.CreateAgentDispatchRequest(
            agent_name=call_id,
            room=call_id,
            metadata=json.dumps({"phone_numbers": phone_numbers}),
        )
    )

    await lkapi.aclose()
If you prefer to keep dispatch-first, start egress after dispatch with: •
lkapi.egress.start_room_composite_egress(..., audio_only=True, file_outputs=[...s3...])
but expect a small gap before recording starts. Do you want the post-dispatch egress snippet or a ready-to-run shell/CLI command for your pipeline? Sources: Auto egress | LiveKit docs