Skip to main content
History injection is enabled via memory presets like memory="history" or memory=MemoryConfig(history=True).
Session history is preserved even across long conversations — see Retention Policies for how overflow is summarised and archived.

Overview

History injection automatically loads previous conversation messages from a session and includes them in the agent’s context. This enables:
  • Multi-turn conversations across sessions
  • Context continuity when users return
  • Persistent memory without complex setup
The user returns to a session; prior messages are injected before the model runs.

Quick Start

1

Simple Usage

2

With Configuration

Switch presets (memory=“chat”) or use a MemoryConfig for token limits and injection rules — see the remaining tabs.

Memory Presets

Enable history via memory presets:

MemoryConfig

For full control, use MemoryConfig:
Default sessions are workspace-scoped — the same agent name in a different project is a different session. See Where sessions live for details and the PRAISONAI_GLOBAL_SESSIONS opt-out.

With auto_save

When using auto_save, the session ID is automatically used for history:

How It Works

The user sends a message; the agent loads prior turns from the session store and prepends them to the messages array before calling the model.
1

Agent Initialization

History settings are resolved from memory parameter:
  • memory="history" → enabled with limit=10
  • memory="chat" → enabled with limit=20
  • memory=MemoryConfig(history=True, history_limit=N) → custom limit
2

Message Building

When _build_messages() is called:
  1. System prompt is added
  2. Session history is injected (if enabled)
  3. In-memory chat history is added
  4. User prompt is added
3

Session Store

History is loaded from the session store using get_working_history(session_id, max_messages=limit) when the store supports it — this replays a compacted summary + retained tail if a compaction checkpoint exists. Stores that implement only get_chat_history fall back to raw history automatically. See Compacted Session Resume.
Per-turn persistence is exact. When memory="history" (or MemoryConfig(history=True)) is combined with auto_save, each agent turn persists exactly the user message and the assistant response — no duplicates, even on repeated save_state() calls. Fixed in PraisonAI PR #1897.
Auto-derived session ids are workspace-scoped. When history is enabled without an explicit session_id, the id now folds in a workspace identity so same-named agents in different projects don’t share history. Opt out with PRAISONAI_GLOBAL_SESSIONS=true. See Session Resume.

Best Practices

Use memory="history" (10 messages) to avoid token bloat. More history means more tokens and higher cost.
Always use session IDs like user-{user_id} for multi-user apps via auto_save.
Use auto_save to persist conversations and memory="history" to reload them. Repeated turns persist incrementally.
Monitor token usage when enabling history to ensure costs stay reasonable.

Comparison

Use memory="history" for conversation continuity with reasonable context. Use memory="chat" for longer conversational context.

Memory

Full memory system with backends

Sessions

Session management and persistence