History Injection
Automatically inject session history into agent context for multi-turn conversations
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
from praisonaiagents import Agent
agent = Agent(
name="assistant",
instructions="Continue our conversation with prior turns in context",
memory="history",
)
agent.start("What did we decide yesterday?")The user returns to a session; prior messages are injected before the model runs.
Quick Start
- Simple Usage
Preset (Simple)
python from praisonaiagents import Agent # Enable history with defaults (last 10 messages) agent = Agent( name="assistant", instructions="Be helpful", memory="history" )Chat Preset
python from praisonaiagents import Agent # Conversational preset (last 20 messages) agent = Agent( name="assistant", instructions="Be helpful", memory="chat" )Custom Limit
python from praisonaiagents import Agent from praisonaiagents import MemoryConfig # Custom history limit agent = Agent( name="assistant", instructions="Be helpful", memory=MemoryConfig(history=True, history_limit=5) ) - 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:
| Preset | History Limit | Description |
|---|---|---|
memory="history" | 10 messages | Standard history injection |
memory="session" | 10 messages | Alias for “history” |
memory="chat" | 20 messages | Conversational (more context) |
# Enable history via memory preset
agent = Agent(
name="assistant",
memory="history" # Enables history with file backend
)# Alias for history preset
agent = Agent(
name="assistant",
memory="session" # Same as "history"
)# Conversational preset with higher limit
agent = Agent(
name="assistant",
memory="chat" # history_limit=20
)MemoryConfig
For full control, use MemoryConfig:
from praisonaiagents import Agent
from praisonaiagents import MemoryConfig
agent = Agent(
name="assistant",
instructions="Be helpful",
memory=MemoryConfig(
history=True, # Enable history injection
history_limit=15 # Last 15 messages
)
)MemoryConfig History Fields
boolWhether history injection is enabled
intMaximum number of messages to inject (most recent)
With auto_save
When using auto_save, the session ID is automatically used for history:
MemoryConfig (Recommended)
from praisonaiagents import Agent
from praisonaiagents import MemoryConfig
agent = Agent(
name="assistant",
memory=MemoryConfig(auto_save="my-session", history=True)
)
agent.start("Hello!")
# Later, same session continues
agent2 = Agent(
name="assistant",
memory=MemoryConfig(auto_save="my-session", history=True)
)
agent2.start("What did I say before?") # Has context!Legacy (Deprecated)
from praisonaiagents import Agent
agent = Agent(
name="assistant",
auto_save="my-session", # Deprecated standalone param
memory="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.
- Agent Initialization
History settings are resolved from
memoryparameter: –memory="history"→ enabled with limit=10 –memory="chat"→ enabled with limit=20 –memory=MemoryConfig(history=True, history_limit=N)→ custom limit - 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 - 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 onlyget_chat_historyfall back to raw history automatically. See Compacted Session Resume.
Best Practices
Set reasonable limits
Use memory="history" (10 messages) to avoid token bloat. More history means more tokens and higher cost.
Use meaningful session IDs
Always use session IDs like user-{user_id} for multi-user apps via auto_save.
Combine with auto_save
Use auto_save to persist conversations and memory="history" to reload them. Repeated turns persist incrementally.
Monitor token usage
Monitor token usage when enabling history to ensure costs stay reasonable.
Comparison
| Feature | memory=True | memory="history" | memory="chat" |
|---|---|---|---|
| Enables memory backend | ✅ | ✅ | ✅ |
| Injects session history | ❌ | ✅ (10 msgs) | ✅ (20 msgs) |
| Long-term memory | ✅ | ✅ | ✅ |