# Practical and Useful Patterns for OpenAI Agent SDK
๐ No more manual conversation history management!
With Sessions, you get automatic history tracking, per-user isolation, and cross-agent sharing out of the box.
๐ Title: Sessions โ Overview
๐ URL:
๐งฉ Overview
The OpenAI Agent SDK's Session feature provides automatic conversation history management. No more manually calling `to_input_list()` to build history. It offers organized per-user/thread histories via session IDs, session sharing across different agents, and `pop_item()` for redoing the last question.
๐ Usage
Import `Agent`, `Runner`, and `SQLiteSession`, then create an agent with `Agent(name="assistant", instructions="Answer helpfully")`. Create a session with `SQLiteSession("conversation_123")` (omitting `db_path` keeps it in memory) and run `await "Tell me about Python", session=session)` for the first question, then `await "Tell me more", session=session)` for a follow-up โ history is automatically carried over. Call `await session.pop_item()` to remove the last exchange, then `await "Explain from a different angle", session=session)` to redo with a new question.
For persistence, pass `db_path`. Create per-user/thread sessions with `SQLiteSession(session_id="user_12345/thread_abc", db_path="sessions.db")` and per-ticket sessions with `SQLiteSession(session_id="ticket_456", db_path="sessions.db")`. Cross-agent sharing is also supported: create `shared_session = SQLiteSession(session_id="user_12345/current")`, run `await "I have a billing question", session=shared_session)`, then `await "Can you give me details?", session=shared_session)` to hand off history seamlessly from support to billing.
๐ Practical Patterns
Standardizing session ID naming conventions makes history retrieval and management easy. Using hierarchical IDs like `user_{id}/thread_{id}` lets you efficiently query all threads for a user or a specific thread's history.
`pop_item()` is perfect for implementing "redo" functionality. When users want to rephrase their question, delete the last exchange and re-run with the new input.
Sharing sessions across different agents is powerful for escalation flows like support โ billing โ engineering. Users don't have to repeat themselves, and handoffs become seamless.
Simply using the `session` parameter eliminates the need for manual `to_input_list()` history management. This alone significantly simplifies your code.
๐ก Use Cases
๐ฌ Per-user conversation history in chatbots
๐ "Redo" button for rephrasing the last question
๐ค History sharing during support โ billing โ engineering escalation
๐ Per-ticket interaction logging and reference
โ ๏ธ Caveats
- Design session IDs to be unique โ collisions will mix different users' histories
- Long-running sessions cause context bloat; combine with history control (covered separately)
- When sharing sessions, all participating agents must be able to understand each other's output
โจ Sessions automate conversation continuity, making multi-turn and multi-agent conversations simple to implement!
#
OpenAIAgentSDK# #
AIAgent#