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
-
Install OpenAI Agents SDK:
pip install openai-agents -
Install dependencies:
pip install -r requirements.txt -
Set up environment variables:
cp env.example .env # Edit .env and add your OpenAI API key -
Launch interactive demo (recommended):
streamlit run streamlit_sessions_app.pyOR run individual examples:
-
Test basic sessions:
python 7_1_basic_sessions/agent.py -
Try memory operations:
python 7_2_memory_operations/agent.py -
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
- Meaningful Session IDs: Use descriptive IDs like
user_12345orsupport_ticket_789 - Persistent Storage: Use file-based sessions for production applications
- Session Isolation: Keep different conversation contexts in separate sessions
- Memory Management: Use
pop_item()for conversation corrections - Cleanup: Clear sessions when conversations should start fresh
๐ Next Steps
After completing this tutorial, you'll be ready for:
- Tutorial 8: Handoffs & Delegation - Agent handoffs and task delegation
- Tutorial 9: Multi-Agent Orchestration - Complex multi-agent workflows
- Tutorial 10: Production Patterns - Real-world deployment strategies
๐จ 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)