miniature-nail-35141
10/24/2025, 8:05 AMtall-airline-16756
10/24/2025, 8:05 AMtall-airline-16756
10/24/2025, 8:06 AM@session.on("close")
def on_close(ev: CloseEvent):
print("Session closed, reason:", ev.reason)
for item in session.history.items:
if item.type == "message":
print(f"{item.role}: {item.text_content}")
elif item.type == "function_call":
print(f"Function call: {item.name} with arguments {item.arguments}")
elif item.type == "function_call_output":
print(f"Function output: {item.name} => {item.output}")
1. Make sure automatic session closure is enabled (this is usually the default) so the history is preserved when disconnections happen.
2. Optionally, you can also process the chat history programmatically—for example, summarize it with an LLM or save it for future reference.
This approach really helps ensure you have access to the full session data, no matter how the disconnection occurs. If you have any questions or need further help setting this up, feel free to ask—I’m here for you!
Sources: examples/voice_agents/multi_agent.py | Understanding the STATE_MISMATCH Disconnect Reason | How to capture real-time agent transcriptions during conversations | Agent-assisted warm transfer | LiveKit Docsminiature-nail-35141
10/24/2025, 8:16 AMtall-airline-16756
10/24/2025, 8:16 AMclass SummaryAgent(BaseAgent):
def __init__(self, job_context: JobContext):
super().__init__(
job_context=job_context,
instructions="Summarize all responses and end the session."
)
async def on_enter(self):
summary = "Thank you for the session! Here is a summary:\n"
for item in self.session.history.items:
if item.type == "message":
summary += f"- {item.role}: {item.text_content}\n"
await self.session.say(summary)
await self.session.aclose()
1. Use the on_exit Callback: Another option is to override the on_exit method in your agent to generate and deliver the summary just before the session closes.
2. Access Chat History: You can use session.history.items to gather all messages and interactions for a complete summary.
3. Close the Session: Once the summary is delivered, simply call await session.aclose() to end the session smoothly.
Following these steps helps ensure your user feels well-informed and appreciated with a clear summary before the session wraps up. If you have any questions or need further help, feel free to ask!
Sources: flows/multi_stage_flow.py | README.md