Session Options
Simple example demonstrating store_history_messages option.
1"""2Session Options3=============================45Simple example demonstrating store_history_messages option.6"""78from kern.agent import Agent9from kern.db.sqlite import SqliteDb10from kern.models.openai import OpenAIResponses11from kern.utils.pprint import pprint_run_response1213# ---------------------------------------------------------------------------14# Create Agent15# ---------------------------------------------------------------------------16agent = Agent(17 model=OpenAIResponses(id="gpt-5-mini"),18 db=SqliteDb(db_file="tmp/example_no_history.db"),19 add_history_to_context=True, # Use history during execution20 num_history_runs=3,21 store_history_messages=False, # Don't store history messages in database22)232425# ---------------------------------------------------------------------------26# Run Agent27# ---------------------------------------------------------------------------28if __name__ == "__main__":29 print("\n=== First Run: Establishing context ===")30 response1 = agent.run("My name is Alice and I love Python programming.")31 pprint_run_response(response1)3233 print("\n=== Second Run: Using history (but not storing it) ===")34 response2 = agent.run("What is my name and what do I love?")35 pprint_run_response(response2)3637 # Check what was stored38 stored_run = agent.get_last_run_output()39 if stored_run and stored_run.messages:40 history_messages = [m for m in stored_run.messages if m.from_history]41 print("\n Storage Info:")42 print(f" Total messages stored: {len(stored_run.messages)}")43 print(f" History messages: {len(history_messages)} (scrubbed!)")44 print("\n History was used during execution (agent knew the answer)")45 print(" but history messages are NOT stored in the database!")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 session_options.py