liveBook · Manning

SessionManager

As explained in Build an AI Agent (From Scratch)

SessionManager is a component that persists, retrieves, and manages sessions across multiple agent interactions.

A new agent run normally creates a new ExecutionContext, losing the previous conversation when that run ends. SessionManager provides continuity: it creates, retrieves, and saves a Session so later runs with the same session_id can reuse its events and state. section 6.3 section 6.3.2

The manager’s interface has four operations:

  • create(session_id, user_id) makes and stores a Session; an existing ID must not be overwritten. section 6.3.2
  • get(session_id) returns the existing Session, or None when it is absent. section 6.3.2
  • get_or_create(session_id, user_id) reuses get and create rather than requiring another implementation in each subclass. section 6.3.2
  • save(session) persists changes. All methods use an async interface so storage implementations can be swapped without changing callers. section 6.3.2

InMemorySessionManager implements the interface with a Python dictionary. It suits development, testing, and single-process applications, but its sessions disappear when the program terminates; durable production storage calls for another implementation. An Agent accepts a supplied manager or defaults to the in-memory one. section 6.3.2 section 6.3.3

Listing 6.13 run() method with session support
async def run(
    self, 
    user_input: str,
    session_id: str | None = None,
    context: ExecutionContext | None = None,
) -> AgentResult:
    """Execute the agent with optional session support."""

    # Load or create session if session_id is provided
    session = None
    if session_id:
        session = await self.session_manager.get_or_create(session_id)

    # Create context with session support
    if context is None:
        context = ExecutionContext(
            session=session,
            session_manager=self.session_manager,
        )

    # Existing run() logic...

    # Save session if available
    if session:
        await self.session_manager.save(session)

    return AgentResult(output=context.final_result, context=context)

Follow the run() flow: use session_id to get or create a Session, attach it and the manager to ExecutionContext, then save the Session after execution. section 6.3.3

Two runs, one conversation

Run the agent once with an introduction, then ask it to recall that introduction. Without a shared session_id, the second run lacks the first run’s context. With the same ID, get_or_create returns the saved Session and ExecutionContext exposes its earlier events to the next run. section 6.3.4 section 6.3.3

A Session can also hold intermediate execution state. That makes it possible to pause a tool call awaiting human approval, save the state, and resume when approval arrives. This is continuity within one session, not long-term knowledge retrieval across different session IDs; that separate job belongs to MemoryManager. section 6.3 section 6.4.1

How Build an AI Agent (From Scratch) covers it

How deep it goes
Implements an async interface, dictionary-backed manager, and agent integration; defers durable database storage to a production implementation.
Examples
Contrasts two agent runs without a shared session_id against runs that recall an earlier introduction using the same ID.
What it assumes
Assumes a Python agent with run() and ExecutionContext, while defining the session-management interface from its operations.
What you can do after
Build it from scratch with an in-memory manager, then wire session loading and saving into an agent’s run() method.
How it connects
Connects Session and ExecutionContext to multiturn conversations and asynchronous human-in-the-loop approval, while distinguishing MemoryManager’s cross-session retrieval.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.