Session Options

Simple example demonstrating store_history_messages option.

1"""
2Session Options
3=============================
4
5Simple example demonstrating store_history_messages option.
6"""
7
8from kern.agent import Agent
9from kern.db.sqlite import SqliteDb
10from kern.models.openai import OpenAIResponses
11from kern.utils.pprint import pprint_run_response
12
13# ---------------------------------------------------------------------------
14# Create Agent
15# ---------------------------------------------------------------------------
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 execution
20 num_history_runs=3,
21 store_history_messages=False, # Don't store history messages in database
22)
23
24
25# ---------------------------------------------------------------------------
26# Run Agent
27# ---------------------------------------------------------------------------
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)
32
33 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)
36
37 # Check what was stored
38 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 repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/05_state_and_session
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python session_options.py