With a session over, how can I save the entire con...
# ask-ai
w
With a session over, how can I save the entire conversation between the user and the agent?
t
Thanks for reaching out, son. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to save the whole conversation between the user and the agent once the session wraps up, you can easily grab it using the
session.history
property. It holds the entire chat history. Here’s a friendly little guide on how to do it: • You can check out the
session.history
property anytime during or after the session. • Then, use the
add_shutdown_callback
method or listen for the
close
event on the session to save the conversation right when the session ends. Here’s a simple example to log the chat history:
Copy code
@session.on("close")
def on_close(ev: CloseEvent):
    print("Chat History:")
    for item in session.history.items:
        if item.type == "message":
            text = f"{item.role}: {item.text_content.replace('\n', '\\n')}"
            if item.interrupted:
                text += " (interrupted)"
            print(text)
This way, you’ll make sure the full conversation is saved and ready to be exported to a file or database whenever you need it. And if you’re also looking to record audio or video, LiveKit’s Egress feature has got you covered for recording the whole session. Just a quick tip: to avoid any extra audio after the user leaves, remember to delete the room or stop the egress when the session ends. Sources: Session recording and transcripts | LiveKit Docs | tests/test_agent_session.py