from livekit import api async def entrypoint(ctx:...
# ask-ai
e
from livekit import api async def entrypoint(ctx: JobContext): req = api.RoomCompositeEgressRequest( room_name=ctx.room.name, audio_only=True, file_outputs=[ api.EncodedFileOutput( file_type=api.EncodedFileType.OGG, filepath="livekit/my-room-test.ogg", s3=api.S3Upload( bucket=os.getenv("AWS_BUCKET_NAME"), region=os.getenv("AWS_REGION"), access_key=os.getenv("AWS_ACCESS_KEY_ID"), secret=os.getenv("AWS_SECRET_ACCESS_KEY"), ), ) ], ) lkapi = api.LiveKitAPI() await lkapi.egress.start_room_composite_egress(req) await lkapi.aclose() # ... continue with your agent logic why do we do await lkapi.aclose() right after starting the recording, and how do we stop recording, is it automatically stopped when the session is over ?
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking why the example calls
await lkapi.aclose()
right after starting egress, and how/when recording stops. Good questions — here's the concise answer. • Why
await lkapi.aclose()
is called: that closes the local HTTP client used by
LiveKitAPI
. Once you've started the egress with
start_room_composite_egress
, the recording runs on the LiveKit server independently, so you can safely close the local client to release resources. • When recording stops: room composite egress is tied to the room lifecycle — it stops automatically when the room ends (for example, when all participants leave). • If you need to stop earlier: call the Stop Egress API with the egress ID:
Copy code
await lkapi.egress.stop_egress(egress_id="your-egress-id")
Would you like an example showing how to capture the
egress_id
from the start response and stop it later? Sources: Observability data hooks | LiveKit docs | RoomComposite Egress | LiveKit Docs | Egress examples | LiveKit docs
e
should i explicitly call the closing of egress in the shutdown callback
t
Short answer: it's optional but recommended in some cases. If your shutdown happens while you still want the recording to continue until the room ends, you don't need to stop egress — the server-side egress will keep running and will stop automatically when the room ends. In that case you can safely close the local
LiveKitAPI
client (call
await lkapi.aclose()
) to release HTTP resources. When you should explicitly stop egress in shutdown: • Your process is shutting down and you need the recording to end immediately (rather than waiting for the room to end). • You want to finalize outputs (e.g., ensure upload completion) or transition to post-processing right away. • You need deterministic egress lifecycle for billing, orchestration, or automation reasons. How to do it: 1. Capture the start response which includes the
egress_id
. 2. In your shutdown callback call:
Copy code
await lkapi.egress.stop_egress(egress_id="the-egress-id")
await lkapi.aclose()
If you’d like, I can show a short example illustrating capturing
egress_id
and wiring it into a shutdown handler.