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.

AspectValue
ScopeConfigurable (global, user, or custom namespace)
PersistenceLong-term
Default modeAgentic
Supported modesAlways, Agentic, Propose
RequiresKnowledge base with vector database

Prerequisites

Learned Knowledge requires a Knowledge base for semantic search:

1from kern.knowledge import Knowledge
2from kern.knowledge.embedder.openai import OpenAIEmbedder
3from kern.vectordb.pgvector import PgVector, SearchType
4
5knowledge = 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 Agent
2from kern.db.postgres import PostgresDb
3from kern.learn import LearningMachine
4from kern.models.openai import OpenAIResponses
5
6agent = 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)
14
15# User 1 saves an insight
16agent.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)
21
22# User 2 benefits from the insight
23agent.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, LearnedKnowledgeConfig
2
3agent = 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, LearnedKnowledgeConfig
2
3agent = 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)
11
12agent.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 saved

Always Mode

Learnings are extracted automatically after each response.

1from kern.learn import LearningMachine, LearningMode, LearnedKnowledgeConfig
2
3agent = 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

FieldDescription
titleShort, searchable title
learningThe actual insight
contextWhen/where this applies
tagsCategories for organization
namespaceSharing scope
user_idOwner (if namespace="user")
created_atWhen captured

What to Save

Good to saveDon't save
Non-obvious discoveriesRaw facts or data
Reusable patternsUser-specific preferences
Domain-specific insightsCommon knowledge
Problem-solving approachesConversation summaries
Best practicesTemporary 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()
2
3# Search for relevant learnings
4results = lm.learned_knowledge_store.search(query="cloud costs", limit=5)
5for result in results:
6 print(f"{result.title}: {result.learning}")
7
8# Debug output
9lm.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 workloads
4Insight: Always check egress costs first - they can be 10x different between providers.
5
6**API rate limiting strategies**
7Context: When designing APIs with high traffic
8Insight: 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 LearnedKnowledgeConfig
2
3# Global: shared with all users (default)
4learned_knowledge=LearnedKnowledgeConfig(namespace="global")
5
6# User: private per user
7learned_knowledge=LearnedKnowledgeConfig(namespace="user")
8
9# Custom: team or domain-specific
10learned_knowledge=LearnedKnowledgeConfig(namespace="engineering")

Combining with Other Stores

1from kern.learn import LearningMachine
2
3agent = Agent(
4 model=OpenAIResponses(id="gpt-5.2"),
5 db=db,
6 learning=LearningMachine(
7 knowledge=knowledge,
8 user_profile=True, # Who the user is
9 user_memory=True, # User's preferences
10 learned_knowledge=True, # Collective insights
11 ),
12)

Personalized responses drawing on collective knowledge.