liveBook · Manning

Session ID

As explained in Build an AI Agent (From Scratch)

A session identifier is a value used to associate multiple agent invocations with the same persistent interaction session.

A session ID associates multiple agent.run() invocations with one persistent interaction session. Reusing it preserves conversation context; omitting it or changing it starts an independent conversation. section 6.3.4 section 6.1.2

Use session IDs to control how agent state is loaded and retained:

  • Provide the same ID on successive calls to retrieve the existing Session and expose prior events through ExecutionContext. section 6.3.4 section 6.3.3
  • Use different IDs, or no ID, when each invocation should be independent, such as a one-off benchmark task. section 6.3.4
  • Persist sessions beyond process memory for stateless web services; InMemorySessionManager suits testing and single-process applications. section 6.3.4 section 6.1.2
  • Keep session continuity separate from persistent knowledge, which shares selected information across different session IDs. section 6.1.2

Continuing a conversation

result1 = await agent.run("My name is Alice", session_id="session_1") result2 = await agent.run("What's my name?", session_id="session_1") The second invocation retrieves the first invocation's session events, allowing the agent to answer that the user's name is Alice. section 6.3

Figure 6.2 Without a session (left), each run() creates a new ExecutionContext that’s discarded after completion. With a session (right), multiple run() calls share the session, maintaining conversation continuity.

Compare independent ExecutionContext instances with multiple run() calls sharing one session and maintaining conversation continuity. section 6.1.2

How Build an AI Agent (From Scratch) covers it

How deep it goes
Explains ID-based retrieval, context injection, event access, and saving, while deferring storage implementation beyond in-memory management.
Examples
Demonstrates Alice's remembered identity and a file-deletion approval resumed with the same session ID.
What it assumes
Assumes agent.run() and ExecutionContext while teaching session continuity from independent invocations.
What you can do after
Build it from scratch by adding session_id handling, loading or creating sessions, injecting context, and saving completed state.
How it connects
Connects session management with ExecutionContext, conversation history, persistent memory, and human-in-the-loop workflows.

Also explained in

Compare how 3 books explain session ID →

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.