Engineering a Cognitive Clone: Architecting a Verified Claude Agent via Heuristic Extraction and LLM-Driven Wiki Structures
In the current landscape of Large Language Model (LLM) deployment, we face a pervasive failure mode: models that prioritize "sounding right" over "being right." This phenomenon is particularly evident in coding assistants like Claude Code, where the model's drive to generate syntax often overrides its capacity for pedagogical explanation. When an agent provides code without explaining the underlying mechanics or verifying the execution, it creates technical debt and obscures understanding.
To solve this, we can move beyond simple prompt engineering and instead architect a specialized Claude Agent designed to mimic the cognitive heuristics of a domain expert—in this case, Andrej Karpathy. By leveraging his public corpus (lectures, GitHub repositories, and technical writings), we can build an agent that doesn't just output code, but follows a rigorous, verifiable pedagogical framework.
The Architecture of Knowledge Extraction
The construction of this agent follows a four-stage pipeline: Corpus Ingestion, Knowledge Graph Construction (Wiki), Heuristic Definition, and the Implementation of an Empirical Verification Loop.
Phase 1: Distributed Data Ingestion
The first challenge is transforming unstructured, multi-modal data into a machine-readable format. The target corpus includes YouTube lecture transcripts, X (formerly Twitter) posts, and GitHub documentation. To achieve this at scale, I utilized a parallelized agentic approach:
- Video Processing: Using
yt-dlpand theYouTube transcript API, I extracted text from Karpathy's educational content. - Social Corpus Extraction: For X data, I leveraged
Twitter API.ioto pull historical posts dating back to 2023. - Parallel Execution: Rather than a sequential crawl, I deployed one agent per source type running in parallel. This process yielded a raw corpus of approximately 700,000 words.
While this provides the "raw material," simply pointing an LLM at a massive folder of messy text leads to high noise-to-signal ratios and retrieval inefficiencies.
Phase ability: The LLM-Driven Wiki (Knowledge Graph)
To make this data actionable, I implemented a system inspired by Karpathy's own methodology for organizing information: converting raw sources into an LLM-generated Wiki. Using a specialized coding agent, the system parses the raw folder and constructs an interconnected web of notes compatible with Obsidian.
The architecture focuses on creating relationships rather than just storing text. The LLM identifies topics, principles, methods, and rules, then creates "purple links" (bidirectional references) between them. For example, a specific rule is linked directly to the source transcript or blog post that justifies it. This transforms a flat directory of files into a structured knowledge graph where every claim can be traced back to an empirical source.
Phase 3: Heuristic Extraction and Sub-Agent Definition
The core "intelligence" of the agent lies in extracting seven specific pedagogical rules from the Wiki. To prevent hallucination, each rule must be paired with an exact quote and a verifiable source link. The extracted heuristics include:
- Empirical Construction: Build it or you don't understand it.
- First-Order Complexity: Focus on the most important term/component first; show it working, then incrementally add complexity.
- Predictive Execution: Predict the output before running the cell to ensure mental model alignment.
- Error Transparency: Intentionally present a broken version of the code first to demonstrate debugging.
- Empirical Proof: Prioritize proof over claims.
- Assumption Disclosure: Explicitly state all underlying assumptions.
- Occam’s Razor for Code: Simpler implementations are superior.
These rules were then codified into two distinct functional units within Claude:
- The Sub-Agent: A dedicated instance of Claude with its own isolated instructions and context window, preventing "context drift" from the main conversation.
- The Skill (
/karpathy-teach): A specialized tool that triggers the agent to grade its own responses against a checklist derived from these seven rules.
Phase 4: Implementing the Empirical Verification Loop (The Run Gate)
The final, and most critical, technical component is the Run Gate. To prevent the model from skipping execution—a common failure in autonomous coding agents—I implemented a script-based "hook."
This hook intercepts the agent's turn right before it finishes. If the agent has generated code but failed to execute it within the environment, the hook blocks the response and sends an error message: "You must run this before you say it works." This forces the model into a continuous loop of Write $\rightarrow$ Run $\rightarrow$ Test $\rightarrow$ Fix.
Testing the Framework: BPE Tokenizer Implementation
To validate the agent, I tasked it with building a Byte Pair Encoding (BPE) tokenizer from scratch. The results demonstrated the efficacy of the heuristic-driven approach:
- Pre-computation: Before writing code, the agent defined "done" and predicted the expected output.
- Incremental Complexity: It built the smallest functional version on a tiny input first.
- Failure Analysis: The agent intentionally presented a breaking version of the code to demonstrate why it failed before providing the fix.
- Traceability: Every step in the terminal was accompanied by a log indicating which specific Karpathy heuristic was being applied (e.g., "Applying Rule 3: Predict, then run").
Continuous Learning via Automated Ingestion
The system is designed for longitudinal growth. By implementing an /karpathy-ingest command, I can provide any new URL (blog post or video). The agent crawls the source, updates the Wiki structure, creates new relationship links, and—crucially—updates the rule set if a new principle emerges in the text. This allows the "brain" to evolve without manual intervention, effectively automating the expansion of its own expertise.
In conclusion, by moving from simple prompting to an architecture involving structured knowledge graphs, sub-agent isolation, and execution hooks, we can transform LLMs from mere code generators into highly specialized, verifiable pedagogical experts.