State And Session

Examples for session state management, chat history, and session persistence.

ExampleDescription
Agentic Session StateLet the agent update its own session_state shopping list with enable_agentic_state.
Chat HistoryPersist chat history in Postgres and read it back with get_chat_history() between turns.
Dynamic Session StateUpdate session state inside a tool hook during an agent run.
Last N Session MessagesSearch a user's previous sessions with search_past_sessions, capped at the last two by num_past_sessions_to_search.
Persistent SessionPersist agent sessions in Postgres with a fixed session_id so history carries across runs.
Session OptionsSimple example demonstrating store_history_messages option.
Session State AdvancedManage a shopping list in session_state with tools that add, remove, and list items through RunContext.
Session State BasicMaintain a shopping list in session state and update it from a tool through RunContext.
Session State EventsStream an agent run and read the final session state from RunCompletedEvent.
Session State Manual UpdateModify session state between runs with get_session_state() and update_session_state().
Session State Multiple UsersSeparate process-local shopping lists by user and session.
Session SummaryUse the session summary to store the conversation summary.
Metadata ResolutionDemonstrates the three-layer metadata resolution for Agents: agent.metadata < session.metadata < call-site metadata.
Search Past SessionsDemonstrates the two-step list-then-read pattern for accessing previous sessions.