Learned Knowledge
Insights that transfer across users.
The Learned Knowledge Store captures reusable insights, patterns, and best practices that apply across users and sessions. Powered by semantic search, agents find and apply relevant knowledge automatically.
| Aspect | Value |
|---|---|
| Scope | Configurable (global, user, or custom namespace) |
| Persistence | Long-term |
| Default mode | Agentic |
| Supported modes | Always, Agentic, Propose |
| Requires | Knowledge base with vector database |
Prerequisites
Learned Knowledge requires a Knowledge base for semantic search:
1from kern.knowledge import Knowledge2from kern.knowledge.embedder.openai import OpenAIEmbedder3from kern.vectordb.pgvector import PgVector, SearchType45knowledge = Knowledge(6 vector_db=PgVector(7 db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",8 table_name="learned_knowledge",9 search_type=SearchType.hybrid,10 embedder=OpenAIEmbedder(id="text-embedding-3-small"),11 ),12)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(10 knowledge=knowledge,11 learned_knowledge=True,12 ),13)1415# User 1 saves an insight16agent.print_response(17 "Save this: When comparing cloud providers, always check egress costs first - "18 "they can be 10x different between providers.",19 user_id="alice@example.com",20)2122# User 2 benefits from the insight23agent.print_response(24 "I'm choosing between AWS and GCP for our data platform. What should I consider?",25 user_id="bob@example.com",26)Agentic Mode
The agent receives tools to manage knowledge explicitly.
1from kern.learn import LearningMachine, LearningMode, LearnedKnowledgeConfig23agent = Agent(4 model=OpenAIResponses(id="gpt-5.2"),5 db=db,6 learning=LearningMachine(7 knowledge=knowledge,8 learned_knowledge=LearnedKnowledgeConfig(mode=LearningMode.AGENTIC),9 ),10)Available tools: search_learnings, save_learning
The agent searches before answering questions and before saving (to avoid duplicates).
Propose Mode
The agent proposes learnings for user confirmation before saving.
1from kern.learn import LearningMachine, LearningMode, LearnedKnowledgeConfig23agent = Agent(4 model=OpenAIResponses(id="gpt-5.2"),5 db=db,6 learning=LearningMachine(7 knowledge=knowledge,8 learned_knowledge=LearnedKnowledgeConfig(mode=LearningMode.PROPOSE),9 ),10)1112agent.print_response(13 "That's a great insight about Docker networking. We should remember that.",14 user_id="alice@example.com",15)16# Agent proposes the learning, user confirms before it's savedAlways Mode
Learnings are extracted automatically after each response.
1from kern.learn import LearningMachine, LearningMode, LearnedKnowledgeConfig23agent = Agent(4 model=OpenAIResponses(id="gpt-5.2"),5 db=db,6 learning=LearningMachine(7 knowledge=knowledge,8 learned_knowledge=LearnedKnowledgeConfig(mode=LearningMode.ALWAYS),9 ),10)Tradeoff: extra LLM call per interaction, may save low-value insights.
Data Model
| Field | Description |
|---|---|
title | Short, searchable title |
learning | The actual insight |
context | When/where this applies |
tags | Categories for organization |
namespace | Sharing scope |
user_id | Owner (if namespace="user") |
created_at | When captured |
What to Save
| Good to save | Don't save |
|---|---|
| Non-obvious discoveries | Raw facts or data |
| Reusable patterns | User-specific preferences |
| Domain-specific insights | Common knowledge |
| Problem-solving approaches | Conversation summaries |
| Best practices | Temporary information |
Good example:
"When comparing cloud providers, always check egress costs first - they vary dramatically (AWS: $0.09/GB, GCP: $0.12/GB, Cloudflare R2: free)."
Poor example:
"AWS has egress costs."
Accessing Learned Knowledge
1lm = agent.get_learning_machine()23# Search for relevant learnings4results = lm.learned_knowledge_store.search(query="cloud costs", limit=5)5for result in results:6 print(f"{result.title}: {result.learning}")78# Debug output9lm.learned_knowledge_store.print(query="cloud costs")Context Injection
Relevant learnings are injected via semantic search:
1<relevant_learnings>2**Cloud egress cost variations**3Context: When selecting cloud providers for data-intensive workloads4Insight: Always check egress costs first - they can be 10x different between providers.56**API rate limiting strategies**7Context: When designing APIs with high traffic8Insight: Use token bucket algorithm for rate limiting - it handles bursts better than fixed windows.9</relevant_learnings>Namespaces
Control knowledge sharing:
1from kern.learn import LearnedKnowledgeConfig23# Global: shared with all users (default)4learned_knowledge=LearnedKnowledgeConfig(namespace="global")56# User: private per user7learned_knowledge=LearnedKnowledgeConfig(namespace="user")89# Custom: team or domain-specific10learned_knowledge=LearnedKnowledgeConfig(namespace="engineering")Combining with Other Stores
1from kern.learn import LearningMachine23agent = Agent(4 model=OpenAIResponses(id="gpt-5.2"),5 db=db,6 learning=LearningMachine(7 knowledge=knowledge,8 user_profile=True, # Who the user is9 user_memory=True, # User's preferences10 learned_knowledge=True, # Collective insights11 ),12)Personalized responses drawing on collective knowledge.