Chat History
Manage and access agent chat history.
Chat History.
1"""2Chat History3=============================45Chat History.6"""78from kern.agent.agent import Agent9from kern.db.postgres import PostgresDb10from kern.models.openai import OpenAIResponses1112db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"1314db = PostgresDb(db_url=db_url, session_table="sessions")1516# ---------------------------------------------------------------------------17# Create Agent18# ---------------------------------------------------------------------------19agent = Agent(20 model=OpenAIResponses(id="gpt-5-mini"),21 db=db,22 session_id="chat_history",23 instructions="You are a helpful assistant that can answer questions about space and oceans.",24 add_history_to_context=True,25)2627# ---------------------------------------------------------------------------28# Run Agent29# ---------------------------------------------------------------------------30if __name__ == "__main__":31 agent.print_response("Tell me a new interesting fact about space")32 print(agent.get_chat_history())3334 agent.print_response("Tell me a new interesting fact about oceans")35 print(agent.get_chat_history())Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/05_state_and_session45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python chat_history.py