Quick Start
Using the Session Class
For advanced control over memory and knowledge, use theSession class:
Memory Integration
Sessions can maintain memory across conversations:Knowledge Integration
Attach knowledge bases to sessions:Remote Agents
Connecting to Remote Agents
Remote agents follow the Google ADK (Agent Development Kit) pattern for standardised communication.
Remote Agent Server
Create an agent server for remote access:Session Configuration
Local Session Options
Remote Session Options
State Management
Saving Session State
Restoring Session State
State Persistence Location
By default, session state is saved to:.sessions/{session_id}/state.json- Session metadata.sessions/{session_id}/memory/- Memory databases.sessions/{session_id}/knowledge/- Knowledge databases
Advanced Features
Session Context
Access session context programmatically:Multi-Agent Sessions
Use multiple agents within a session:Session Middleware
Add custom middleware to sessions:Session Expiry & Cleanup
Sessions can auto-expire after a fixed lifetime and release their resources with a singleclose() call.
| Parameter | Type | Default | Description |
|---|---|---|---|
session_ttl | Optional[int] | None | Lifetime in seconds. None = never expire. Must be >= 0; negative raises ValueError. |
| Method | Returns | Description |
|---|---|---|
is_expired() | bool | True once time_to_expiry() == 0. Always False when session_ttl is None. |
time_to_expiry() | Optional[float] | Seconds left before expiry (clamped to 0). None if no TTL set. |
close() | None | Flushes agent chat histories, closes memory connections, clears knowledge and agents. No-op on remote sessions. |
- Poll before each turn
- Show remaining time
- Cleanup with close()
Pick a TTL that matches your UX
Pick a TTL that matches your UX
Use 30–60 min for chat assistants, 24 h for long-running bots,
None for batch jobs.Use monotonic semantics, not wall-clock
Use monotonic semantics, not wall-clock
is_expired() measures elapsed runtime; it does not reflect calendar time. Do not compare internal timestamps against time.time().Always close() in a finally block
Always close() in a finally block
Even without TTL,
close() releases memory-adapter connections and flushes pending chat history.Negative TTL is a programming error
Negative TTL is a programming error
The constructor raises
ValueError immediately — surface this to the developer, do not catch silently.Use Cases
Customer Support
Maintain conversation history across support interactions
Personal Assistants
Remember user preferences and past interactions
Collaborative Work
Share context between team members
Distributed Systems
Deploy agents across multiple servers
Complete Example
Database Session Persistence
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.Best Practices
Use meaningful session IDs
Use meaningful session IDs
Prefer stable IDs such as
support_{ticket_id} or assistant_{user_id} so resume and cleanup stay predictable.Save state after important interactions
Save state after important interactions
Call
session.save_state() or use MemoryConfig(auto_save=...) so long-running workflows survive restarts.Handle remote failures gracefully
Handle remote failures gracefully
Wrap
session.chat() in try/except for TimeoutError and ConnectionError; set explicit timeout= on remote sessions.Validate session IDs and authenticate remotes
Validate session IDs and authenticate remotes
Treat session IDs as opaque handles — authenticate remote agent URLs with headers or tokens, not shared IDs alone.
Auto-Save Sessions
UseMemoryConfig.auto_save to automatically persist sessions by name after each interaction, eliminating the need for manual save_state() calls.
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.Auto-Save vs Manual Save
| Approach | Code | When to Use |
|---|---|---|
auto_save | MemoryConfig(auto_save="name") | Most applications — zero boilerplate, per-turn persistence stays in sync |
| Manual | session.save_state() | Save-point control; idempotent as of PR #1897 |
| History injection | MemoryConfig(history=True) | Auto-inject past messages into context |
Next Steps
Bot session reset policies (idle / daily)
Deep dive into memory capabilities
Deploy agents for remote access
Related
Choose a storage backend so sessions survive restarts.
Persist and recall information across sessions and agents.

