Skip to main content
Sessions enable agents to remember conversations across restarts and connect to remote agents.
The user sends follow-up messages; the session keeps context across process restarts and remote agents.

Quick Start

1

Start with session_id

2

Resume in a new process

Using the Session Class

For advanced control over memory and knowledge, use the Session 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 single close() call.
ParameterTypeDefaultDescription
session_ttlOptional[int]NoneLifetime in seconds. None = never expire. Must be >= 0; negative raises ValueError.
MethodReturnsDescription
is_expired()boolTrue 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()NoneFlushes agent chat histories, closes memory connections, clears knowledge and agents. No-op on remote sessions.
Use 30–60 min for chat assistants, 24 h for long-running bots, None for batch jobs.
is_expired() measures elapsed runtime; it does not reflect calendar time. Do not compare internal timestamps against time.time().
Even without TTL, close() releases memory-adapter connections and flushes pending chat history.
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.
For database-backed persistence without manual session management, see the HostedAgent persistence guide.

Best Practices

Prefer stable IDs such as support_{ticket_id} or assistant_{user_id} so resume and cleanup stay predictable.
Call session.save_state() or use MemoryConfig(auto_save=...) so long-running workflows survive restarts.
Wrap session.chat() in try/except for TimeoutError and ConnectionError; set explicit timeout= on remote sessions.
Treat session IDs as opaque handles — authenticate remote agent URLs with headers or tokens, not shared IDs alone.

Auto-Save Sessions

Use MemoryConfig.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

ApproachCodeWhen to Use
auto_saveMemoryConfig(auto_save="name")Most applications — zero boilerplate, per-turn persistence stays in sync
Manualsession.save_state()Save-point control; idempotent as of PR #1897
History injectionMemoryConfig(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

Choose a storage backend so sessions survive restarts.

Persist and recall information across sessions and agents.