The simplest way to use sessions is with the Agent class:
from praisonaiagents import Agent# Create agent with session persistenceagent = Agent( name="Assistant", instructions="You are a helpful assistant", memory={"session_id": "my-session-123"})# First conversationagent.start("My name is Alice")# Later, in a new process - history restored automatically!agent = Agent( name="Assistant", instructions="You are a helpful assistant", memory={"session_id": "my-session-123"})agent.start("What's my name?") # Remembers: "Alice"
For advanced control over memory and knowledge, use the Session class:
from praisonaiagents import Session# Create a sessionsession = Session( session_id="chat_123", user_id="user_456")# Create agent within session contextagent = session.Agent( name="Assistant", instructions="You are a helpful assistant")
# Create new session instancesession = Session(session_id="chat_123")# Restore previous statesession.restore_state()# Continue where you left offagent = session.Agent(name="Assistant")response = agent.chat("What were we discussing?")
from praisonaiagents import Session, Task, AgentTeam# Create or restore sessionsession = Session( session_id="personal_assistant", user_id="john_doe", memory={ "provider": "rag", "use_embedding": True }, knowledge={ "vector_store": { "provider": "chroma", "config": {"collection_name": "personal_docs"} } })# Try to restore previous statetry: session.restore_state() print("Restored previous session")except: print("Starting new session")# Create session agentassistant = session.Agent( name="Personal Assistant", instructions="""You are a personal assistant that remembers everything. Use your memory and knowledge to provide personalised help.""", memory=True, knowledge=True)# Add some knowledgesession.add_knowledge("calendar.pdf")session.add_memory("User prefers morning meetings")# Have a conversationresponse = assistant.chat("Schedule a meeting for tomorrow")print(response)# Save state for next timesession.save_state()
When using database-backed sessions (e.g., via praisonai.db), both messages and metadata automatically persist to your database. The clear_session() and delete_session() methods remove persisted messages and metadata respectively. set_metadata() and get_metadata() round-trip through the conversation store, ensuring metadata survives process restarts.
Use MemoryConfig.auto_save to automatically persist sessions by name after each interaction, eliminating the need for manual save_state() calls.
from praisonaiagents import Agent, MemoryConfig# Automatically save session state under the given nameagent = Agent( name="Assistant", instructions="You are a helpful assistant.", memory=MemoryConfig( auto_save="my-assistant-session", # Session name user_id="user123", ))# Each interaction is auto-persistedagent.start("Remember that I prefer dark mode")# Session saved automatically — no manual save_state() needed
Idempotent saves.Session.save_state() is idempotent against the underlying session store: calling it repeatedly does not duplicate chat history. The session store must implement set_chat_history(session_id, messages); the built-in DefaultSessionStore does. Custom stores that only implement add_message() will log a warning and may produce duplicates on repeated saves. (PraisonAI PR #1897)
The standalone auto_save parameter on Agent(...) is deprecated. Use memory=MemoryConfig(auto_save="name") instead.