← All Cookbooks
Microsoft AutoGenBeginner10 min

AutoGen + HatiData: Stateful Agent Groups

Build AutoGen agent groups with persistent state. Agents remember past conversations and share context across group chat sessions.

What You'll Build

An AutoGen group chat where agents persist their state in HatiData. Conversations, decisions, and context survive across sessions.

Prerequisites

$pip install autogen-agentchat hatidata-agent

$hati init

$OpenAI API key

Architecture

┌──────────────┐    ┌──────────────┐    ┌──────────────┐
│  AutoGen     │───▶│  HatiData    │───▶│   Engine     │
│  GroupChat   │    │  Agent State │    │  + Vectors   │
└──────────────┘    └──────────────┘    └──────────────┘
     Group state persists across sessions via SQL

Key Concepts

  • Agent state persistence: set_agent_state() and get_agent_state() save and restore arbitrary JSON state across AutoGen sessions
  • Group memory: store_memory() with shared namespaces lets all agents in a group chat access the same knowledge base
  • Session continuity: agents pick up where they left off — no need to re-explain context at the start of each session
  • SQL-native state: all state operations use standard SQL, making it easy to query, audit, and migrate agent state

Step-by-Step Implementation

1

Install Dependencies

Install AutoGen and the HatiData agent SDK.

Bash
pip install autogen-agentchat hatidata-agent
hati init

Note: AutoGen provides multi-agent group chat. HatiData provides persistent state storage.

2

Create Stateful Agents

Configure AutoGen agents that save and restore state from HatiData between sessions.

Python
from hatidata_agent import HatiDataAgent

hati = HatiDataAgent(host="localhost", port=5439, agent_id="autogen-planner")

# Save agent state at the end of a session
hati.execute("""
    SELECT set_agent_state(
        'autogen-planner',
        'session_context',
        '{"last_topic": "Q3 planning", "decisions": ["expand APAC", "hire 5 engineers"]}'
    )
""")

# Restore state in a new session
state = hati.query("""
    SELECT get_agent_state('autogen-planner', 'session_context')
""")

print(f"Restored state: {state[0]}")
Expected Output
Restored state: {"last_topic": "Q3 planning", "decisions": ["expand APAC", "hire 5 engineers"]}
3

Run a Stateful Group Chat

Build an AutoGen group chat where agents share persistent context through HatiData's memory layer.

Python
import autogen
from hatidata_agent import HatiDataAgent

hati = HatiDataAgent(host="localhost", port=5439, agent_id="group-coordinator")

# Define agents with persistent memory
planner = autogen.AssistantAgent(
    name="Planner",
    system_message="You plan quarterly strategy. Check HatiData for past decisions.",
)

reviewer = autogen.AssistantAgent(
    name="Reviewer",
    system_message="You review plans against past commitments stored in HatiData.",
)

user_proxy = autogen.UserProxyAgent(name="Admin", human_input_mode="NEVER")

# Store shared context before the chat
hati.execute("""
    SELECT store_memory(
        'Q2 decision: expand APAC team by 5 engineers, budget approved',
        'quarterly-planning'
    )
""")

groupchat = autogen.GroupChat(agents=[planner, reviewer, user_proxy], messages=[])
manager = autogen.GroupChatManager(groupchat=groupchat)

user_proxy.initiate_chat(manager, message="Plan Q3 strategy based on our Q2 decisions")
Expected Output
Planner: Based on our Q2 decision to expand APAC by 5 engineers...

Ready to build?

Install HatiData locally and start building with Microsoft AutoGen in minutes.

Join Waitlist