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 Agent
2from kern.db.postgres import PostgresDb
3from kern.models.openai import OpenAIResponses
4
5db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")
6
7agent = Agent(
8 model=OpenAIResponses(id="gpt-5.2"),
9 db=db,
10 learning=True, # Enable all learning stores
11)
12
13# Session 1
14agent.run("I'm Alex, I prefer concise answers.", user_id="alex@example.com")
15
16# Session 2 - agent remembers
17agent.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:

StoreWhat It CapturesScope
User ProfileStructured fields (name, preferences)Per user
User MemoryUnstructured observationsPer user
Session ContextGoal, plan, progress, summaryPer session
Entity MemoryFacts, events, relationshipsConfigurable
Learned KnowledgeInsights, patterns, best practicesGlobal

Configuration Levels

1from kern.learn import LearningMachine, UserProfileConfig, SessionContextConfig, LearningMode
2
3# Level 1: Dead Simple
4agent = Agent(model=model, db=db, learning=True)
5
6# Level 2: Pick What You Want
7agent = 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)
17
18# Level 3: Full Control
19agent = 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:

ModeBehaviorBest For
ALWAYSAutomatic extraction after each turnUser profiles, session context
AGENTICAgent decides when to saveLearned knowledge
PROPOSEAgent proposes, user confirmsHigh-stakes knowledge

Featured Patterns

Run the Examples

1git clone https://github.com/kern-ai/kern.git
2cd kern/cookbook/08_learning
3
4# Setup
5./setup_venv.sh
6
7# Run examples
8python 01_basics/1a_user_profile_always.py
9python 07_patterns/personal_assistant.py