from praisonaiagents import Agent# Create agent with session persistenceagent = Agent( name="Assistant", instructions="You are a helpful assistant", memory={"session_id": "my-session-123"} # Enable persistence!)# First conversationagent.start("My name is Alice and I love pizza")
3
Resume Later
Copy
# In a new Python process - history is automatically restored!from praisonaiagents import Agentagent = Agent( name="Assistant", instructions="You are a helpful assistant", memory={"session_id": "my-session-123"} # Same session_id)agent.start("What's my name?") # Agent remembers: "Alice"
For apps with multiple users, include user_id to isolate conversations:
Copy
from praisonaiagents import Agent, MemoryConfigdef create_agent_for_user(user_id: str): return Agent( name="Assistant", instructions="You are a helpful assistant", memory=MemoryConfig( session_id=f"user-{user_id}-main", user_id=user_id, ) )# Each user gets isolated sessionsalice_agent = create_agent_for_user("alice")bob_agent = create_agent_for_user("bob")# Alice's conversations are separate from Bob'salice_agent.start("My favorite color is blue")bob_agent.start("My favorite color is red")
For advanced control over memory and knowledge, use the Session class:
Copy
from praisonaiagents import Session# Create a sessionsession = Session( session_id="research-project", user_id="researcher-1")# Create agent within session contextagent = session.Agent( name="Research Assistant", instructions="Help with research tasks", memory=True)# Chat with the agentresponse = agent.start("Find papers about machine learning")# Save custom statesession.save_state({ "project": "ML Research", "papers_found": 5})
For low-level control, access the session store directly:
Copy
from praisonaiagents.session import get_default_session_storestore = get_default_session_store()# Add messages manuallystore.add_user_message("session-123", "Hello!")store.add_assistant_message("session-123", "Hi there!")# Get chat historyhistory = store.get_chat_history("session-123")# List all sessionssessions = store.list_sessions(limit=50)# Delete a sessionstore.delete_session("session-123")
# List all sessionspraisonai session list# Start interactive sessionpraisonai session start my-session# Resume a sessionpraisonai session resume my-session# Show session detailspraisonai session show my-session# Delete a sessionpraisonai session delete my-session