The session module provides high-level abstractions for building stateful AI applications with persistent memory, knowledge management, and support for both local and remote agent connectivity.
# Create session with memory configurationsession = Session( session_id="assistant_session", memory_config={ "provider": "rag", "use_embedding": True })# Create agent with session contextagent = session.Agent( name="Personal Assistant", role="Helpful AI assistant", instructions="Remember user preferences and past conversations")# Memory is automatically managedresponse = agent.chat("My favorite color is blue")session.store_long_memory("User's favorite color: blue", quality_score=0.9)# Later retrievalcontext = session.get_memory_context("What's my favorite color?")
# Incremental state updatessession = Session(session_id="game_session")# Initialize statesession.save_state({ "score": 0, "level": 1, "inventory": []})# Update specific valuessession.set_state("score", session.get_state("score", 0) + 10)# Complex state updatesinventory = session.get_state("inventory", [])inventory.append("sword")session.set_state("inventory", inventory)=======title: "Session Management"sidebarTitle: "Session"description: "Build stateful applications with persistent sessions and remote agent connectivity"icon: "server"---```mermaidflowchart LR subgraph Local Session LS[Session Instance] LA[Local Agent] LM[Memory Store] LK[Knowledge Base] LS --> LA LS --> LM LS --> LK end subgraph Remote Session RS[Session Instance] RA[Remote Agent] RS -.->|HTTP/HTTPS| RA end subgraph State Management SS[Session State] ST[State Storage] SS <--> ST end LS --> SS RS --> SS style LS fill:#189AB4,color:#fff style RS fill:#189AB4,color:#fff style RA fill:#2E8B57,color:#fff style SS fill:#8B0000,color:#fff
The Session module provides a unified API for building stateful agent applications with persistent state management and remote agent connectivity. It acts as a simple wrapper around PraisonAI’s existing stateful capabilities, making it easy to:
Create persistent sessions with unique identifiers
Manage agent state across multiple interactions
Connect to remote agents running on different machines
Store and retrieve memory and knowledge within session context
Build distributed agent systems similar to Google’s ADK pattern
from praisonaiagents import Session# Create a local sessionsession = Session( session_id="chat_123", user_id="user_456")# Create an agent within the sessionagent = session.Agent( name="Assistant", role="Helpful AI", instructions="Remember our conversation history")# Chat with the agentresponse = agent.chat("Hello! Please remember I prefer Python.")print(response)# Save session statesession.save_state({ "conversation_topic": "Python programming", "user_preference": "Python"})
1
Install with memory support
Copy
pip install "praisonaiagents[memory]"
2
Create a local session
Copy
from praisonaiagents import Session# Create a local sessionsession = Session( session_id="chat_123", user_id="user_456")# Create an agent within the sessionagent = session.Agent( name="Assistant", role="Helpful AI", instructions="Remember our conversation history")# Chat with the agentresponse = agent.chat("Hello! Please remember I prefer Python.")print(response)# Save session statesession.save_state({ "conversation_topic": "Python programming", "user_preference": "Python"})
1
Install praisonaiagents
Copy
pip install praisonaiagents
2
Connect to remote agent
Copy
from praisonaiagents import Session# Connect to a remote agentsession = Session( agent_url="192.168.1.10:8000/agent", session_id="remote_chat_123", user_id="client_456")# Chat directly with the remote agentresponse = session.chat("Hello from remote client!")print(response)# Alternative method (ADK-style)response = session.send_message("What's the weather today?")print(response)
Creates an agent within the session context. Only available for local sessions.
The Session.Agent() method is the recommended way to create agents within a session. The older create_agent() method is maintained for backward compatibility.
from praisonaiagents import Session# Create persistent sessionsession = Session( session_id="customer_support_001", user_id="customer_123")# Create support agentagent = session.Agent( name="Support Bot", role="Customer Support Specialist", instructions="""You are a helpful customer support agent. Remember customer preferences and previous issues.""", memory=True)# First interactionresponse = agent.chat("I'm having trouble with my order #12345")session.save_state({"current_issue": "order_12345"})# Later interaction (session restored)session = Session(session_id="customer_support_001", user_id="customer_123")previous_state = session.restore_state()print(f"Previous issue: {previous_state.get('current_issue')}")
# Create session with knowledgesession = Session(session_id="doc_assistant_001")# Add knowledge sourcessession.add_knowledge("technical_manual.pdf")session.add_knowledge("https://docs.example.com/api")session.add_knowledge("Quick reference: API key is required for all requests")# Create documentation assistantassistant = session.Agent( name="DocBot", role="Documentation Assistant", knowledge=["technical_manual.pdf"])# Query with knowledge contextcontext = session.get_context("How do I authenticate?")response = assistant.chat(f"Based on this context: {context}\n\nHow do I authenticate?")
The Session module is designed to be a simple, intuitive wrapper around PraisonAI’s stateful capabilities. It handles the complexity of state management, allowing you to focus on building great agent applications.