0

๐ŸŽฏ Tutorial 7: Sessions & Memory Management

awesome-llm-apps7_sessions

Master automatic conversation memory with Sessions! This tutorial teaches you how to use the OpenAI Agents SDK's built-in session memory to maintain conversation history across multiple agent runs without manual memory management.

Sign in to save downloads to your library and vote.

Preview

๐ŸŽฏ Tutorial 7: Sessions & Memory Management

Master automatic conversation memory with Sessions! This tutorial teaches you how to use the OpenAI Agents SDK's built-in session memory to maintain conversation history across multiple agent runs without manual memory management.

๐ŸŽฏ What You'll Learn

  • Automatic Memory: Sessions handle conversation history automatically
  • SQLite Sessions: Persistent and in-memory conversation storage
  • Memory Operations: Adding, retrieving, and managing conversation items
  • Session Management: Multiple sessions and custom implementations

๐Ÿง  Core Concept: What Are Sessions?

Sessions provide automatic conversation memory that eliminates the need to manually handle .to_input_list() between turns. Think of sessions as a smart conversation database that:

  • Automatically stores all conversation history
  • Retrieves context before each agent run
  • Maintains separate conversations for different session IDs
  • Supports persistent storage across application restarts
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    SESSION WORKFLOW                         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                             โ”‚
โ”‚  USER INPUT                                                 โ”‚
โ”‚       โ”‚                                                     โ”‚
โ”‚       โ–ผ                                                     โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    1. RETRIEVE HISTORY                     โ”‚
โ”‚  โ”‚   SESSION   โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚   MEMORY    โ”‚                                          โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    2. PREPEND TO INPUT                   โ”‚ โ”‚
โ”‚       โ”‚                                                   โ”‚ โ”‚
โ”‚       โ–ผ                                                   โ”‚ โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                          โ”‚ โ”‚
โ”‚  โ”‚    AGENT    โ”‚    3. PROCESS WITH CONTEXT               โ”‚ โ”‚
โ”‚  โ”‚   RUNNER    โ”‚                                          โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                          โ”‚ โ”‚
โ”‚       โ”‚                                                   โ”‚ โ”‚
โ”‚       โ–ผ                                                   โ”‚ โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    4. STORE NEW ITEMS                    โ”‚ โ”‚
โ”‚  โ”‚   RESPONSE  โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚  โ”‚  GENERATED  โ”‚                                            โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿš€ Tutorial Overview

This tutorial demonstrates three key session patterns:

1. Basic SQLite Sessions (basic_sessions.py)

  • In-memory and persistent session storage
  • Automatic conversation history management
  • Simple multi-turn conversations

2. Advanced Memory Operations (memory_operations.py)

  • Memory manipulation with get_items(), add_items(), pop_item()
  • Conversation corrections and modifications
  • Session management operations

3. Multiple Sessions (multi_sessions.py)

  • Managing different conversation contexts
  • Session isolation and organization
  • Custom session implementations

๐Ÿ“ Project Structure

7_sessions/
โ”œโ”€โ”€ README.md                   # This file - concept explanation
โ”œโ”€โ”€ requirements.txt            # Dependencies
โ”œโ”€โ”€ streamlit_sessions_app.py   # Interactive Streamlit demo (recommended)
โ”œโ”€โ”€ 7_1_basic_sessions/
โ”‚   โ”œโ”€โ”€ agent.py               # SQLite sessions basics
โ”‚   โ””โ”€โ”€ README.md              # Basic sessions documentation
โ”œโ”€โ”€ 7_2_memory_operations/
โ”‚   โ”œโ”€โ”€ agent.py               # Advanced memory operations
โ”‚   โ””โ”€โ”€ README.md              # Memory operations documentation
โ”œโ”€โ”€ 7_3_multi_sessions/
โ”‚   โ”œโ”€โ”€ agent.py               # Multiple session management
โ”‚   โ””โ”€โ”€ README.md              # Multi-sessions documentation
โ””โ”€โ”€ env.example                # Environment variables template

๐ŸŽฏ Learning Objectives

By the end of this tutorial, you'll understand:

  • โœ… How to use SQLiteSession for automatic memory management
  • โœ… Difference between in-memory and persistent sessions
  • โœ… Advanced memory operations for conversation management
  • โœ… Managing multiple concurrent sessions
  • โœ… When to use sessions vs manual conversation management

๐Ÿš€ Getting Started

  1. Install OpenAI Agents SDK:

    pip install openai-agents
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Set up environment variables:

    cp env.example .env
    # Edit .env and add your OpenAI API key
    
  4. Launch interactive demo (recommended):

    streamlit run streamlit_sessions_app.py
    

    OR run individual examples:

  5. Test basic sessions:

    python 7_1_basic_sessions/agent.py
    
  6. Try memory operations:

    python 7_2_memory_operations/agent.py
    
  7. Test multiple sessions:

    python 7_3_multi_sessions/agent.py
    

๐Ÿงช Sample Use Cases

Basic Sessions

  • "What city is the Golden Gate Bridge in?" โ†’ "What state is it in?"
  • "My name is Alice" โ†’ "What's my name?"
  • "I work as a developer" โ†’ "What do I do for work?"

Memory Operations

  • Correcting previous messages with pop_item()
  • Clearing conversation history with clear_session()
  • Adding custom conversation items

Multiple Sessions

  • Different users: user_123, user_456
  • Different contexts: support_ticket_789, sales_inquiry_101
  • Different applications: chatbot_session, assistant_session

๐Ÿ”ง Key Session Patterns

1. Basic Session Usage

from agents import Agent, Runner, SQLiteSession

agent = Agent(name="Assistant", instructions="Reply concisely.")
session = SQLiteSession("conversation_123")

result = await Runner.run(agent, "Hello", session=session)

2. Persistent vs In-Memory

# In-memory (lost when process ends)
session = SQLiteSession("user_123")

# Persistent file-based
session = SQLiteSession("user_123", "conversations.db")

3. Memory Operations

# Get conversation history
items = await session.get_items()

# Add custom items
await session.add_items([{"role": "user", "content": "Hello"}])

# Remove last item (for corrections)
last_item = await session.pop_item()

# Clear all history
await session.clear_session()

๐Ÿ’ก Session Design Best Practices

  1. Meaningful Session IDs: Use descriptive IDs like user_12345 or support_ticket_789
  2. Persistent Storage: Use file-based sessions for production applications
  3. Session Isolation: Keep different conversation contexts in separate sessions
  4. Memory Management: Use pop_item() for conversation corrections
  5. Cleanup: Clear sessions when conversations should start fresh

๐Ÿ”— Next Steps

After completing this tutorial, you'll be ready for:

๐Ÿšจ Troubleshooting

  • Memory Not Persisting: Ensure you're using file-based SQLiteSession with a database path
  • Session Conflicts: Use unique session IDs for different conversation contexts
  • Performance Issues: Consider implementing custom session backends for high-volume applications
  • Database Errors: Check file permissions for SQLite database files

๐Ÿ’ก Pro Tips

  • Start Simple: Begin with in-memory sessions for development and testing
  • Plan Session Architecture: Design your session ID strategy before building
  • Monitor Memory Usage: Track conversation length and implement cleanup strategies
  • Test Session Persistence: Verify that conversations survive application restarts
  • Consider Scaling: Plan for custom session implementations in production systems

Ingestion metadata

Source catalog
awesome-llm-apps
Repository
Shubhamsaboo/awesome-llm-apps ยท main
File path
ai_agent_framework_crash_course/openai_sdk_crash_course/7_sessions/README.md
Last refreshed
7/23/2026, 10:39:09 PM (4h ago)
Refresh schedule
Daily ยท 03:00 UTC
Dedupe status
Unique ยท deduped by (source, url)