Support Agent

Multi-tenant support agent that learns solutions and applies them to similar issues.

Overview

A customer support agent that combines all learning stores to provide faster resolutions over time. Uses namespace isolation for multi-tenant deployments where different organizations share the system but have separate knowledge.

Code

1from kern.agent import Agent
2from kern.db.postgres import PostgresDb
3from kern.knowledge import Knowledge
4from kern.knowledge.embedder.openai import OpenAIEmbedder
5from kern.learn import (
6 EntityMemoryConfig,
7 LearnedKnowledgeConfig,
8 LearningMachine,
9 LearningMode,
10 SessionContextConfig,
11 UserProfileConfig,
12)
13from kern.models.openai import OpenAIResponses
14from kern.vectordb.pgvector import PgVector, SearchType
15
16db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
17db = PostgresDb(db_url=db_url)
18
19# Shared knowledge base for solutions
20knowledge = Knowledge(
21 vector_db=PgVector(
22 db_url=db_url,
23 table_name="support_kb",
24 search_type=SearchType.hybrid,
25 embedder=OpenAIEmbedder(id="text-embedding-3-small"),
26 ),
27)
28
29
30def create_support_agent(customer_id: str, ticket_id: str, org_id: str) -> Agent:
31 """Create a support agent for a specific ticket."""
32 return Agent(
33 model=OpenAIResponses(id="gpt-5.2"),
34 db=db,
35 instructions=(
36 "You are a helpful support agent. "
37 "Check if similar issues have been solved before. "
38 "Save successful solutions for future reference."
39 ),
40 learning=LearningMachine(
41 knowledge=knowledge,
42 user_profile=UserProfileConfig(mode=LearningMode.ALWAYS),
43 session_context=SessionContextConfig(enable_planning=True),
44 entity_memory=EntityMemoryConfig(
45 mode=LearningMode.ALWAYS,
46 namespace=f"org:{org_id}:support",
47 ),
48 learned_knowledge=LearnedKnowledgeConfig(mode=LearningMode.AGENTIC),
49 ),
50 user_id=customer_id,
51 session_id=ticket_id,
52 markdown=True,
53 )
54
55
56if __name__ == "__main__":
57 org_id = "acme"
58
59 # Ticket 1: First customer with login issue
60 agent = create_support_agent("customer_1@example.com", "ticket_001", org_id)
61 agent.print_response(
62 "I can't log into my account. It says 'invalid credentials' "
63 "even though I know my password is correct. I'm using Chrome.",
64 stream=True,
65 )
66
67 # Solution worked - agent saves it
68 agent.print_response("Clearing the cache worked! Thanks!", stream=True)
69
70 # Ticket 2: Similar issue - agent finds prior solution
71 agent2 = create_support_agent("customer_2@example.com", "ticket_002", org_id)
72 agent2.print_response(
73 "Login not working in Chrome, says wrong password but I'm sure it's right.",
74 stream=True,
75 )

How It Works

  • User Profile: Tracks customer history and preferences
  • Session Context: Manages current ticket with goal/plan/progress
  • Entity Memory: Stores product info, past tickets, shared across organization
  • Learned Knowledge: Saves successful solutions for future retrieval

Key pattern: The namespace=f"org:{org_id}:support" isolates entity memory per organization while the learned knowledge base can be shared globally or per-org.

Run This Example

1git clone https://github.com/kern-ai/kern.git
2cd kern/cookbook/08_learning
3
4# Setup (requires Docker for Postgres)
5./setup_venv.sh
6./cookbook/scripts/run_pgvector.sh
7
8# Run
9python 07_patterns/support_agent.py

Full source: kern/cookbook/08_learning/07_patterns/support_agent.py

Related