Manage conversation history, session summaries, and chat continuity across interactions
Session state in Agno allows agents to maintain conversation history and context across multiple interactions with users. This enables natural, continuous conversations that remember what was discussed.
To enable persistent sessions, provide a database and session ID:
from agno.agent import Agentfrom agno.db.postgres import PostgresDbagent = Agent( db=PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"), session_id="user_123_session", add_history_to_context=True,)# First messageagent.print_response("Hi, my name is Alice")# Second message - agent remembers the conversationagent.print_response("What's my name?") # Response: "Your name is Alice"
Session IDs allow you to manage multiple conversations:
from uuid import uuid4session_id = str(uuid4())agent.print_response( "Tell me about space", session_id=session_id)agent.print_response( "Tell me more", # Continues the space conversation session_id=session_id)
Control whether conversation history is included in the agent’s context:
With History
Without History
agent = Agent( db=db, session_id="chat_session", add_history_to_context=True, # Default)agent.print_response("My favorite color is blue")agent.print_response("What's my favorite color?") # Works
agent = Agent( db=db, session_id="chat_session", add_history_to_context=False,)agent.print_response("My favorite color is blue")agent.print_response("What's my favorite color?") # Won't remember
from agno.agent import Agentfrom agno.db.postgres import PostgresDbagent = Agent( db=PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"), session_id="long_conversation", enable_session_summaries=True, add_session_summary_to_context=True,)# After many messages, the agent automatically creates summariesfor i in range(50): agent.print_response(f"Message {i}")# Summary is used instead of full history to save tokens
# Get all sessions for a usersessions = db.get_sessions( session_type=SessionType.AGENT, user_id="[email protected]", limit=10, sort_by="created_at", sort_order="desc")for session in sessions: print(f"{session.session_id}: {len(session.messages)} messages")
from agno.agent import Agentagent = Agent( db=db, session_id="agent_session",)# Session type is automatically AGENTagent.print_response("Hello")
from agno.team import Teamteam = Team( members=[agent1, agent2], db=db, session_id="team_session",)# Session type is automatically TEAMteam.print_response("Solve this problem")
from agno.workflow import Workflowworkflow = Workflow( db=db, session_id="workflow_session",)# Session type is automatically WORKFLOWworkflow.run(input_data)
import asynciofrom agno.agent import Agentfrom agno.db.postgres import PostgresDbdb = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")chat_agent = Agent( db=db, update_memory_on_run=True, add_history_to_context=True,)async def multi_user_chat(): # User 1, Session 1 await chat_agent.aprint_response( "My name is Mark and I like anime", user_id="[email protected]", session_id="mark_session_1" ) # User 1, Session 2 (different conversation) await chat_agent.aprint_response( "I'm going to the movies tonight", user_id="[email protected]", session_id="mark_session_2" ) # User 2, Session 1 await chat_agent.aprint_response( "Hi, my name is John", user_id="[email protected]", session_id="john_session_1" ) # Back to User 1, Session 1 await chat_agent.aprint_response( "What do you suggest I do this weekend?", user_id="[email protected]", session_id="mark_session_1" # Remembers Mark likes anime )asyncio.run(multi_user_chat())
agent = Agent( db=db, session_id="chat_session", add_history_to_context=True, # Session state update_memory_on_run=True, # User memories)# Session state: remembers this conversation# User memories: extracts "user likes pizza"agent.print_response( "I love pizza, especially margherita", user_id="[email protected]")# New session - no conversation history# But memories persist!agent.print_response( "What food do I like?", # Agent: "You like pizza" user_id="[email protected]", session_id="new_session" # Different session)