Chat History
Include previous messages in context for multi-turn conversations.
Chat history enables multi-turn conversations. Without it, each run is isolated—the agent has no idea what was said before. With a database and add_history_to_context=True, previous messages are automatically included in every request.
Enable Chat History
Set add_history_to_context=True to include previous messages in every run:
1from kern.agent import Agent2from kern.db.sqlite import SqliteDb34agent = Agent(5 db=SqliteDb(db_file="agent.db"),6 add_history_to_context=True,7 num_history_runs=3, # Include last 3 turns8)910agent.print_response("My name is Sarah", session_id="chat_123")11agent.print_response("What's my name?", session_id="chat_123") # Agent knows: "Sarah"1from kern.team import Team2from kern.db.sqlite import SqliteDb34team = Team(5 db=SqliteDb(db_file="team.db"),6 add_history_to_context=True,7 num_history_runs=3, # Include last 3 turns8)910team.print_response("My name is Sarah", session_id="chat_123")11team.print_response("What's my name?", session_id="chat_123") # Team knows: "Sarah"Chat history requires a database. Without one, there's nothing to retrieve.
Control History Size
More history means more tokens. Use these parameters to control what gets included:
| Parameter | Description |
|---|---|
num_history_runs | Number of previous runs to include (default: 3) |
num_history_messages | Maximum messages to include across all runs |
max_tool_calls_from_history | Limit tool call messages in history |
1agent = Agent(2 db=SqliteDb(db_file="agent.db"),3 add_history_to_context=True,4 num_history_runs=5,5 num_history_messages=20, # Cap at 20 messages total6)Start with num_history_runs=3. Increase only if your agent needs more context. For long conversations, combine limited history with session summaries.
On-Demand History Access
Instead of always including history, let the agent decide when to look it up:
1agent = Agent(2 db=SqliteDb(db_file="agent.db"),3 read_chat_history=True, # Agent gets a get_chat_history() tool4)The agent can call get_chat_history() when it needs context, rather than having history in every request. Useful for analytics, auditing, or when most queries don't need prior context.
Cross-Session History
Search across multiple sessions for context that spans conversations:
1agent = Agent(2 db=SqliteDb(db_file="agent.db"),3 search_session_history=True,4 num_history_sessions=2, # Search last 2 sessions5)Keep num_history_sessions low (2-3). Cross-session history can quickly fill your context window.
Programmatic Access
Retrieve history directly in your code:
1# Get user-assistant message pairs2chat_history = agent.get_chat_history(session_id="chat_123")34# Get all messages from the session5messages = agent.get_session_messages(session_id="chat_123")67# Get the last run output with metrics8last_run = agent.get_last_run_output()Use this for building custom UIs, debugging, or exporting transcripts.
Team History
Teams support additional history sharing between members:
1team = Team(2 db=SqliteDb(db_file="team.db"),3 add_history_to_context=True,4 num_history_runs=3,5 add_team_history_to_members=True, # Share history across team members6)With add_team_history_to_members=True, member agents see the full team conversation, not just their own interactions.
Workflow History
Workflows use add_workflow_history_to_steps to pass previous run results to steps:
1from kern.workflow import Workflow23workflow = Workflow(4 db=SqliteDb(db_file="workflow.db"),5 add_workflow_history_to_steps=True,6 num_history_runs=5,7 steps=[...],8)Workflow history passes previous workflow outputs to steps, not conversation messages. See Workflow Sessions for details.
Choosing a Pattern
| Scenario | Configuration |
|---|---|
| Chat-style products | add_history_to_context=True, num_history_runs=3 |
| Long conversations | Limited history + session summaries |
| Tool-heavy agents | Add max_tool_calls_from_history to reduce noise |
| Cross-session recall | search_session_history=True, num_history_sessions=2 |
| Selective lookup | read_chat_history=True (agent decides when to look up) |
| Custom UIs | Use get_chat_history() programmatically |
Developer Resources
- Session Storage - What gets stored and how to retrieve it.
- AgentSession reference
- TeamSession reference