Learning
Agents that learn, adapt, and improve over time using the Learning Machine.
The Learning Machine enables agents to learn from every interaction. Instead of building memory, knowledge, and feedback systems separately, configure one system that handles all learning.
The goal: An agent on interaction 1000 is fundamentally better than it was on interaction 1.
1from kern.agent import Agent2from kern.db.postgres import PostgresDb3from kern.models.openai import OpenAIResponses45db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")67agent = Agent(8 model=OpenAIResponses(id="gpt-5.2"),9 db=db,10 learning=True, # Enable all learning stores11)1213# Session 114agent.run("I'm Alex, I prefer concise answers.", user_id="alex@example.com")1516# Session 2 - agent remembers17agent.run("What do you know about me?", user_id="alex@example.com")18# -> "You're Alex, you prefer concise answers"Learning Stores
The Learning Machine coordinates five specialized stores:
| Store | What It Captures | Scope |
|---|---|---|
| User Profile | Structured fields (name, preferences) | Per user |
| User Memory | Unstructured observations | Per user |
| Session Context | Goal, plan, progress, summary | Per session |
| Entity Memory | Facts, events, relationships | Configurable |
| Learned Knowledge | Insights, patterns, best practices | Global |
Configuration Levels
1from kern.learn import LearningMachine, UserProfileConfig, SessionContextConfig, LearningMode23# Level 1: Dead Simple4agent = Agent(model=model, db=db, learning=True)56# Level 2: Pick What You Want7agent = Agent(8 model=model,9 db=db,10 learning=LearningMachine(11 user_profile=True,12 session_context=True,13 entity_memory=False,14 learned_knowledge=False,15 ),16)1718# Level 3: Full Control19agent = Agent(20 model=model,21 db=db,22 learning=LearningMachine(23 user_profile=UserProfileConfig(mode=LearningMode.AGENTIC),24 session_context=SessionContextConfig(enable_planning=True),25 ),26)Learning Modes
Each store can run in different modes:
| Mode | Behavior | Best For |
|---|---|---|
| ALWAYS | Automatic extraction after each turn | User profiles, session context |
| AGENTIC | Agent decides when to save | Learned knowledge |
| PROPOSE | Agent proposes, user confirms | High-stakes knowledge |
Featured Patterns
user
Personal Assistant
Remembers preferences, tracks context, learns from feedback.
headset
Support Agent
Multi-tenant learning with namespace isolation. Tracks customer context and accumulates support knowledge.
Run the Examples
1git clone https://github.com/kern-ai/kern.git2cd kern/cookbook/08_learning34# Setup5./setup_venv.sh67# Run examples8python 01_basics/1a_user_profile_always.py9python 07_patterns/personal_assistant.py