Session Context
Goals, plans, and progress for active sessions.
The Session Context Store captures the current state of a conversation: what's been discussed, what the goal is, and what progress has been made. Unlike other stores that accumulate data, session context is a snapshot that gets replaced on each update.
| Aspect | Value |
|---|---|
| Scope | Per session |
| Persistence | Session lifetime (replaced on update) |
| Default mode | Always |
| Supported modes | Always |
Basic Usage
1from kern.agent import Agent2from kern.db.postgres import PostgresDb3from kern.learn import LearningMachine4from kern.models.openai import OpenAIResponses56agent = Agent(7 model=OpenAIResponses(id="gpt-5.2"),8 db=PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),9 learning=LearningMachine(session_context=True),10)1112# Session tracks what's being discussed13agent.print_response(14 "I'm designing a REST API for a todo app. Should I use PUT or PATCH for updates?",15 user_id="alice@example.com",16 session_id="api_design",17)1819# Later in the session, context is maintained20agent.print_response(21 "What about the delete endpoint?",22 user_id="alice@example.com",23 session_id="api_design",24)The agent knows the ongoing context about REST API design.
Summary Mode
Default behavior. Captures the essence of the conversation without detailed planning.
1from kern.learn import LearningMachine, SessionContextConfig23agent = Agent(4 model=OpenAIResponses(id="gpt-5.2"),5 db=db,6 learning=LearningMachine(7 session_context=SessionContextConfig(),8 ),9)What gets captured: what's being worked on, key decisions made, current state, open questions.
Planning Mode
Enable planning to track goals, plan steps, and progress.
1from kern.learn import LearningMachine, SessionContextConfig23agent = Agent(4 model=OpenAIResponses(id="gpt-5.2"),5 db=db,6 learning=LearningMachine(7 session_context=SessionContextConfig(enable_planning=True),8 ),9)1011agent.print_response(12 "Help me deploy a Python app to production. Give me the steps.",13 user_id="alice@example.com",14 session_id="deploy_app",15)1617# Later, progress is tracked18agent.print_response(19 "Done with step 1. What's next?",20 user_id="alice@example.com",21 session_id="deploy_app",22)Data Model
| Field | Description |
|---|---|
session_id | Unique session identifier |
user_id | User this session belongs to |
summary | What's been discussed |
goal | What user is trying to accomplish (planning mode) |
plan | Steps to achieve goal (planning mode) |
progress | Completed steps (planning mode) |
created_at | When created |
updated_at | Last update |
Accessing Session Context
1lm = agent.get_learning_machine()23context = lm.session_context_store.get(session_id="api_design")4if context:5 print(f"Summary: {context.summary}")6 if context.goal:7 print(f"Goal: {context.goal}")89# Debug output10lm.session_context_store.print(session_id="api_design")Context Injection
Session context is injected into the system prompt:
1<session_context>2Summary: Helping user design a REST API for a todo app. Discussed resource naming conventions. Currently exploring HTTP methods for CRUD operations.34Goal: Design complete REST API for todo application56Plan:7 1. Define resource endpoints8 2. Choose HTTP methods for each operation9 3. Design request/response schemas10 4. Add authentication1112Completed:13 ✓ Define resource endpoints14</session_context>When to Use
Session context is essential when:
- Message history gets truncated: long conversations lose early context
- Sessions are resumed: user returns after a break
- Complex multi-step tasks: track progress through long workflows
- Handoffs: another agent or human needs to understand the state
Combining with Other Stores
Session context works well alongside user-level stores:
1from kern.learn import LearningMachine, SessionContextConfig23agent = Agent(4 model=OpenAIResponses(id="gpt-5.2"),5 db=db,6 learning=LearningMachine(7 user_profile=True, # Who the user is8 session_context=SessionContextConfig(enable_planning=True), # Current state9 ),10)Long-term user knowledge plus short-term session state.