How to beginner · 3 min read

How to save AutoGen conversation history

Quick answer
To save AutoGen conversation history, capture the messages exchanged during the chat session by accessing the conversation's message list or log object and serialize it to a file (e.g., JSON). This allows you to persist and reload the full dialogue for later analysis or continuation.

PREREQUISITES

  • Python 3.8+
  • CrewAI AutoGen installed (pip install crewai-autogen)
  • Basic knowledge of Python file I/O

Setup

Install the crewai-autogen package and set up your environment. Ensure you have Python 3.8 or higher.

Run the following command to install:

bash
pip install crewai-autogen

Step by step

Use the AutoGen framework to run a conversation and save the history by serializing the message list to a JSON file.

python
import json
from crewai.autogen import AutoAgent, ChatSession

# Initialize an AutoAgent (example)
agent = AutoAgent(model_name="gpt-4o")

# Start a chat session
chat = ChatSession(agent=agent)

# Send messages and receive responses
chat.send_user_message("Hello, how are you?")
response = chat.get_response()
print("Agent:", response)

# Continue conversation
chat.send_user_message("Tell me a joke.")
response = chat.get_response()
print("Agent:", response)

# Save conversation history to JSON
history = chat.get_message_history()  # returns list of dicts with role and content

with open("conversation_history.json", "w", encoding="utf-8") as f:
    json.dump(history, f, indent=2)

print("Conversation history saved to conversation_history.json")
output
Agent: I'm doing well, thank you! How can I assist you today?
Agent: Why did the scarecrow win an award? Because he was outstanding in his field!
Conversation history saved to conversation_history.json

Common variations

You can save conversation history asynchronously or use different serialization formats like CSV or plain text. Also, you can reload saved history to resume conversations.

python
import json
import asyncio
from crewai.autogen import AutoAgent, ChatSession

async def async_chat_save():
    agent = AutoAgent(model_name="gpt-4o")
    chat = ChatSession(agent=agent)

    await chat.send_user_message_async("Hello asynchronously!")
    response = await chat.get_response_async()
    print("Agent:", response)

    history = chat.get_message_history()
    with open("async_history.json", "w", encoding="utf-8") as f:
        json.dump(history, f, indent=2)

    print("Async conversation history saved.")

asyncio.run(async_chat_save())
output
Agent: Hello asynchronously! How can I help you today?
Async conversation history saved.

Troubleshooting

  • If get_message_history() returns empty, ensure messages are sent and responses received before saving.
  • Check file write permissions if saving to disk fails.
  • Use json.dumps(history, indent=2) to verify serialization before writing.

Key Takeaways

  • Use ChatSession.get_message_history() to retrieve conversation messages as a list.
  • Serialize conversation history to JSON for easy saving and reloading.
  • Support for async conversation saving is available in AutoGen.
  • Always verify messages exist before saving to avoid empty history files.
  • Reload saved history to resume or analyze past conversations.
Verified 2026-04 · gpt-4o
Verify ↗