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
python
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.

Diagram

History Injection

📋 Prior Messages

🧠 Build Messages

✅ LLM Request

Quick Start

  1. 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)
    )
  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:

PresetHistory LimitDescription
memory="history"10 messagesStandard history injection
memory="session"10 messagesAlias for “history”
memory="chat"20 messagesConversational (more context)
history
# Enable history via memory preset
agent = Agent(
    name="assistant",
    memory="history"  # Enables history with file backend
)
session
# Alias for history preset
agent = Agent(
    name="assistant",
    memory="session"  # Same as "history"
)
chat
# Conversational preset with higher limit
agent = Agent(
    name="assistant",
    memory="chat"  # history_limit=20
)

MemoryConfig

For full control, use MemoryConfig:

python
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
history bool

Whether history injection is enabled

history_limit int

Maximum number of messages to inject (most recent)

With auto_save

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

MemoryConfig (Recommended)

python
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)

python
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.

Diagram
SessionAgentUserSessionAgentUserstart(“What did we decide?”)get_chat_history(session_id, limit)Previous messages_build_messages() with historyContext-aware response
  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.

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

Featurememory=Truememory="history"memory="chat"
Enables memory backend✅✅✅
Injects session history❌✅ (10 msgs)✅ (20 msgs)
Long-term memory✅✅✅

Related