Can you please help me understand difference betwe...
# ask-ai
a
Can you please help me understand difference between on_session_end and add_shutdown_callback?
✅ 1
t
Thanks for reaching out, Manish. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’ll explain the difference between
on_session_end
and
add_shutdown_callback
, and when you’d use each. Acknowledging your question • You asked how
on_session_end
differs from
add_shutdown_callback
. I’ll compare their purpose, where you register them, when they run, and common use cases. Short summary •
on_session_end
: a decorator-level callback you supply to
@server.rtc_session
. It runs when an AgentSession ends and is ideal for final session data handling (exporting/redacting transcripts, analytics). •
add_shutdown_callback
: a runtime method on the
JobContext
(
ctx.add_shutdown_callback(...)
) that registers one or more coroutine callbacks to run during the session shutdown process. It’s ideal for cleanup or teardown tasks that depend on runtime state. Key differences • Where you register ◦ `on_session_end`: supplied as the
on_session_end
parameter on the
@server.rtc_session
decorator (set up ahead of handling sessions). ◦ `add_shutdown_callback`: called inside your session code using
ctx.add_shutdown_callback(...)
so you can register callbacks dynamically. • When they execute ◦ `on_session_end`: invoked when an
AgentSession
ends for post-session processing. ◦ `add_shutdown_callback`: executed during the shutdown sequence (after
session.shutdown()
/
session.aclose()
), before the agent process terminates. • Multiplicity ◦ `on_session_end`: typically a single function configured per agent server. ◦ `add_shutdown_callback`: you can add multiple callbacks per session. • Common uses ◦ `on_session_end`: exporting redacted session transcripts, exporting reports, session-level analytics or compliance-oriented finalization. ◦ `add_shutdown_callback`: final cleanup tasks, saving incremental user state that depends on runtime conditions, triggering webhooks or downstream workflows at shutdown. How to choose • If you need a consistent, single final handler for session export/redaction or agent-level session post-processing, use
on_session_end
. • If you need to register conditional or multiple teardown tasks that depend on what happened during the session, use
ctx.add_shutdown_callback(...)
. • You can use both together:
add_shutdown_callback
for runtime-specific cleanup and
on_session_end
for central session export/processing. If you’d like, I can: • Show a short example of each pattern in code. • Help design where to put transcript redaction vs. resource cleanup in your current agent flow. Which would you prefer? Sources: Redacting PII from agent logs and transcripts | LiveKit | Agent session | LiveKit Documentation | Job lifecycle | LiveKit Documentation | Collect recording consent with tasks | LiveKit Documentation, with interactive elements
a
i need to generate the call summary when call is ended and call a api to store it in DB
t
Thanks — this is a great use case. Based on what you said (generate a call summary when the call ends and call an API to store it), the simplest, most reliable place to do that is in
on_session_end
. That callback is designed for final session processing such as exporting/redacting transcripts and sending summaries to storage. How to implement this (high-level) 1. Use
on_session_end
on your
@server.rtc_session
decorator to run logic after the AgentSession ends. 2. In that callback, generate the call summary (e.g., from the session transcript, events, or precomputed summary data). 3. Call your storage API (HTTP request) with the summary payload to save it in your DB. 4. Optionally: use
ctx.add_shutdown_callback()
inside the session if you need any runtime-specific cleanup or additional async tasks to run during shutdown (but keep summary export in
on_session_end
for a single centralized handler). Example pattern (pseudo-code) • Register a single
on_session_end
function where you: ◦ Load or accept the session transcript / summary source. ◦ Perform any redaction or formatting needed for compliance. ◦ Make an HTTP POST to your API with the summary payload. ◦ Handle retries/errors (log failure or enqueue for retry). If you’d like, I can provide a concrete example in your preferred language (Python or Node). To make the example match your setup, please tell me: • Which language you’re using (Python or Node)? • Where the summary is produced today (full transcript, partial events, or do you need help creating the summary from raw transcript)? • Any requirements for redaction, retries, or response handling from your storage API? If you want a quick starter, tell me which language and I’ll give a compact code example showing
@server.rtc_session(on_session_end=...)
, summary generation, and an HTTP POST to store it. , with interactive elements
a
Can we access : llm = self.agent_instance.llm chat_ctx = self.agent_instance.chat_ctx in session_end
t
Thanks — that’s a clear question. You want to know if you can access
self.agent_instance.llm
and
self.agent_instance.chat_ctx
inside
on_session_end
so you can generate the call summary and call your API. Short answer • You cannot rely on direct access to
self.agent_instance
(and its `llm`/`chat_ctx`) inside
on_session_end
. That pattern is not documented as guaranteed. • Instead, use the
JobContext
passed into
on_session_end
to get the session data you need and generate/store the summary. Why and what to use instead •
on_session_end
receives a
JobContext
(
ctx
) and is intended for final session processing such as exporting/redacting transcripts or creating session reports. • Use
ctx.make_session_report()
or the session report data from the context to build your call summary. The session report contains the conversation history and session artifacts you need for summarization and storage. • The active session exposes useful runtime properties while the session is running (e.g.,
session.chat_ctx
,
session.llm
), but those are not documented as accessible via
self.agent_instance
inside the
on_session_end
callback. Recommended implementation options 1. Generate summary in
on_session_end
◦ Call
ctx.make_session_report()
inside your
on_session_end
handler to get the transcript/history. ◦ Create the summary (redact if required). ◦ POST the summary to your storage API and handle retries/errors. 1. If you need to run LLM calls that depend on in-memory runtime state ◦ Register runtime callbacks during the session using
ctx.add_shutdown_callback(my_coroutine)
so you can run async cleanup or finalization while runtime state is still available. ◦ Use
add_shutdown_callback
from inside the session code where
self.agent_instance
is available; those callbacks run during shutdown and can access runtime state you registered/closed over. Which approach to pick • Use
on_session_end
for a centralized, single final handler that uses session report data to create a summary and persist it. • Use
ctx.add_shutdown_callback
if your summary generation requires direct access to the live
llm
instance or in-memory chat context that isn’t available from the session report. If you want, I can: • Provide a compact Python example showing
on_session_end
that calls
ctx.make_session_report()
and POSTs the summary to your API. • Or show how to register an
add_shutdown_callback
from inside the session that has access to
self.agent_instance
and then sends the summary. Which example would you prefer and do you want redaction or retry logic included? Sources: Agent session | LiveKit Documentation | Redacting PII from agent logs and transcripts | LiveKit | Agents and handoffs | LiveKit Documentation | LLM-Powered Content Filter | LiveKit Documentation, with interactive elements