ๆณจๅ†Œๅนถๅˆ†ไบซ้‚€่ฏท้“พๆŽฅ๏ผŒๅฏ่Žทๅพ—่ง†้ข‘ๆ’ญๆ”พไธŽ้‚€่ฏทๅฅ–ๅŠฑใ€‚

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture ๆŠ•็จฟใฏๅ€‹ไบบใฎๆ„่ฆ‹ใงใ™ใ€‚
ๅŠ ๅ…ฅ May 2026
282 ๆญฃๅœจๅ…ณๆณจ    431 ็ฒ‰ไธ
# 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#
ๆ˜พ็คบๆ›ดๅคš