How to Build a Self-Learning AI That Updates Its Own Instructions
Problem
Every time I started a new session with Claude, it forgot everything. My coding preferences, the patterns I liked, the mistakes I corrected last time - all gone.
I would spend the first 10 minutes re-explaining:
- “Use functional components, not classes”
- “Always run tests before committing”
- “Don’t use console.log for debugging”
By session 50, I was frustrated. By session 100, I was exhausted. Why couldn’t Claude remember what I taught it?
What I Tried First
I wrote a comprehensive CLAUDE.md file with all my preferences:
# Coding Preferences
- Use functional programming style over OOP- Always write tests first (TDD)- Prefer immutable data structures- Use Grep tool before editing files- Run linter before every commitThis helped, but Claude still made the same mistakes. The instructions were static. They didn’t evolve based on what worked and what didn’t.
I needed something that could learn from each session.
The Solution
I built a self-learning system with three components:
- Observation hooks - Capture everything that happens in a session
- Pattern detection - Find what should be learned
- Instinct storage - Store learned behaviors with confidence scores
Here’s the architecture:
┌─────────────────────────────────────────────────────────────┐│ SESSION ACTIVITY ││ User prompts, tool calls, corrections, outcomes │└─────────────────────────┬───────────────────────────────────┘ │ │ 1. OBSERVATION HOOKS ▼┌─────────────────────────────────────────────────────────────┐│ observations.jsonl ││ {"event":"tool_start","tool":"Edit","input":"..."} ││ {"event":"tool_complete","tool":"Edit","output":"..."} ││ {"event":"user_correction":"No, use X instead"} │└─────────────────────────┬───────────────────────────────────┘ │ │ 2. PATTERN DETECTION ▼┌─────────────────────────────────────────────────────────────┐│ LEARNED PATTERNS ││ - User corrections -> instinct ││ - Error resolutions -> instinct ││ - Repeated workflows -> instinct │└─────────────────────────┬───────────────────────────────────┘ │ │ 3. INSTINCT STORAGE ▼┌─────────────────────────────────────────────────────────────┐│ instincts/personal/*.md ││ - prefer-functional-style.md (0.7 confidence) ││ - always-test-first.md (0.9 confidence) ││ - use-zod-validation.md (0.6 confidence) │└─────────────────────────────────────────────────────────────┘Step 1: Set Up Observation Hooks
I added hooks to Claude Code’s settings to capture every tool use:
{ "hooks": { "PreToolUse": [{ "matcher": "*", "hooks": [{ "type": "command", "command": "~/.claude/skills/continuous-learning/hooks/observe.sh pre" }] }], "PostToolUse": [{ "matcher": "*", "hooks": [{ "type": "command", "command": "~/.claude/skills/continuous-learning/hooks/observe.sh post" }] }] }}The observe.sh script appends JSON lines to a log file:
{"event":"tool_start","tool":"Edit","input":"auth.js","timestamp":"2025-01-15T10:23:00Z"}{"event":"tool_complete","tool":"Edit","output":"success","timestamp":"2025-01-15T10:23:05Z"}{"event":"user_feedback","content":"No, use functional update instead","timestamp":"2025-01-15T10:24:00Z"}This captures 100% of session activity without me doing anything.
Step 2: Detect Patterns Automatically
After each session, a background agent analyzes the observations. It looks for four pattern types:
Type 1: User Corrections
User: "No, use functional update instead of direct mutation"Claude: Fixes the code-> New instinct: "When updating state in React, use functional updates"Type 2: Error Resolutions
Error: useEffect infinite loopFix: Added dependency array-> New instinct: "When using useEffect, always include all dependencies"Type 3: Repeated Workflows
Sequence observed 8 times: Grep -> Read -> Edit-> New instinct: "When modifying code, search first, then read, then edit"Type 4: Tool Preferences
Pattern: Always uses Grep before Edit-> New instinct: "When needing to find code, use Grep tool"Step 3: Store Instincts with Confidence Scores
Each learned behavior becomes an “instinct” - a small, atomic instruction with a confidence score:
---id: use-functional-updatestrigger: "when updating React state"confidence: 0.7domain: "react"source: "session-observation"created: 2025-01-15observations: 5---
# Use Functional Updates in React
## ActionWhen updating state that depends on previous state, use functional update form:
```javascript// Instead of: setCount(count + 1)setCount(c => c + 1)Evidence
- User corrected direct mutation on 2025-01-10
- Observed functional pattern in 5 similar contexts
- No corrections needed after adoption
The confidence score is crucial. It prevents wrong updates from taking over.
## Why Confidence Scores Matter
I was worried about the Reddit user's question: "How do you handle wrong corrections?"
The answer: graduated confidence.
```text title="Confidence evolution"Score 0.3: Tentative - suggested but not enforcedScore 0.5: Moderate - applied when relevantScore 0.7: Strong - auto-approved for applicationScore 0.9: Near-certain - core behaviorWhen Claude first learns something, it starts at 0.3. Only after repeated validation does it climb to 0.7+.
Confidence increases when:
- Pattern observed 3+ times
- User doesn’t correct the behavior
- Similar instincts agree
Confidence decreases when:
- User explicitly corrects the behavior
- Pattern not observed for extended time
- Contradicting evidence appears
The Self-Learning Loop
The Reddit philosophy captured it perfectly:
“AI is only as good as its context. You gotta 100-shot 10 apps before you can 1-shot 10 apps. The self-learning loop is what gets you there.”
Here’s what happened over time:
Sessions 1-20: Building baseline contextSessions 21-50: Accumulating preferencesSessions 51-100: Compound intelligence kicks inSessions 100+: One-shot clean codeBy session 100, Claude knew:
- My codebase structure
- My testing preferences
- My architectural patterns
- My common mistakes to avoid
It one-shots clean code because it accumulated enough context.
Common Mistakes I Made
Mistake 1: Auto-updating without confidence thresholds
{ "auto_update": true, "confidence_threshold": 0.0}This let wrong patterns get adopted immediately. I changed to:
{ "auto_update": true, "confidence_threshold": 0.7}Now only validated patterns get auto-applied.
Mistake 2: No version control for instruction files
I initially didn’t track my instincts folder in Git. When Claude learned something wrong, I couldn’t revert. Now I commit every change:
git add instincts/git commit -m "Update instincts from session $(date +%Y-%m-%d)"Mistake 3: Single vault for everything
I initially mixed personal notes with AI instructions. Claude learned inappropriate things from my personal journal. Now I keep separate vaults:
~/.claude/ ├── CLAUDE.md # Global instructions ├── instincts/ # AI-learned behaviors └── skills/ # Workflow definitions
~/notes/ └── personal/ # My private notes (Claude never sees)Mistake 4: Trusting without validation
During the first 20 sessions, I reviewed every auto-update. After the patterns stabilized, I trusted the system more. But I still review low-confidence additions.
How to Set This Up
If you want to build your own self-learning AI:
- Add observation hooks to capture session activity
- Create an instincts folder with confidence-weighted instructions
- Set up a background agent (I use Haiku for cost efficiency) to analyze observations
- Configure auto-commits to version control your evolving instructions
- Set confidence threshold at 0.7 before auto-applying
The key insight: don’t expect immediate results. This is a 100-shot system. Each session adds small learnings. Over time, they compound into genuine intelligence.
Summary
I built a self-learning AI by capturing session observations, detecting patterns, and storing learned behaviors with confidence scores. After 100+ sessions, Claude knows my preferences and one-shots clean code.
The critical piece is graduated confidence - new learnings start at 0.3 and only reach 0.7+ after repeated validation. This prevents wrong updates from taking over while allowing genuine patterns to accumulate.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 Reddit: Obsidian + Claude = no more copy paste
- 👨💻 knowledge-base-server GitHub
- 👨💻 Model Context Protocol
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments