Mini Context Graph Skill
The Core Idea
Standard RAG re-discovers knowledge from scratch on every query. This skill is different:
- Wiki layer — The LLM writes and maintains persistent markdown pages (summaries, entity pages, topic syntheses). Cross-references are already there. The wiki gets richer with every ingest.
- Graph layer — Entities and relations are extracted once and stored as a navigable knowledge graph. BFS traversal answers structural queries without re-reading sources.
- Raw source layer — Original documents are stored immutably with chunks. Provenance links tie every graph node and edge back to the exact text that supports it.
The LLM writes; the Python tools handle all bookkeeping.
Three Layers
| Layer |
Where |
What the LLM does |
What Python does |
| Raw Sources |
data/documents.json |
Reads (never modifies) |
Stores chunks + metadata |
| Wiki |
wiki/ (markdown) |
Writes/updates pages |
Manages index.md + log.md |
| Graph |
data/graph.json |
Extracts entities + relations |
Persists, deduplicates, traverses |
⚡ Quick Start for Agents
from scripts.contextgraph import ContextGraphSkill
from scripts.tools import wiki_store
skill = ContextGraphSkill()
# ===== INGEST WITH FULL RAG + WIKI =====
# 1. Read references/ingestion.md and references/ontology.md first
# 2. Extract entities and relations (LLM reasoning step)
entities = [
{"name": "memory leak", "type": "issue", "supporting_text": "memory leaks cause crashes"},
{"name": "system crash", "type": "issue", "supporting_text": "system crashes due to memory leaks"},
]
relations = [
{"source": "memory leak", "target": "system crash", "type": "causes",
"confidence": 1.0, "supporting_text": "System crashes due to memory leaks."},
]
result = skill.ingest_with_content(
doc_id="doc_001",
title="System Crash Analysis",
source="/docs/incident_report.pdf",
raw_content="System crashes due to memory leaks. Memory leaks occur when objects are not released.",
entities=entities,
relations=relations,
)
# result = {"doc_id": "doc_001", "chunk_count": 1, "nodes_added": 2, "edges_added": 1}
# 3. Write a wiki summary page for this document
wiki_store.write_page(
category="summary",
title="System Crash Analysis Summary",
content="""---
title: System Crash Analysis
source_document: doc_001
tags: [summary, incident]
---
# System Crash Analysis
**Source:** incident_report.pdf
## Key Claims
- [[memory-leak]] causes [[system-crash]] (confidence: 1.0)
## Entities
- [[memory-leak]] (issue)
- [[system-crash]] (issue)
""",
summary="Incident report: memory leaks cause system crashes.",
)
# ===== QUERY WITH EVIDENCE =====
result = skill.query_with_evidence("Why does the system crash?")
# Returns: {"query": ..., "subgraph": ..., "supporting_documents": [...], "evidence_chain": ...}
# ===== WIKI SEARCH (read wiki before answering) =====
pages = wiki_store.search_wiki("memory leak")
# Returns: [{slug, category, path, snippet}, ...]
Operations
Ingest
When a user provides a new document:
- Read
references/ingestion.md — entity/relation extraction rules.
- Read
references/ontology.md — type normalization rules.
- Extract entities and relations using your LLM reasoning.
- Call
skill.ingest_with_content(...) — stores raw content + chunks + graph nodes + provenance.
- Write a wiki summary page using
wiki_store.write_page(category="summary", ...).
- Update entity pages — for each new/updated entity, write or update
wiki_store.write_page(category="entity", ...).
- Update topic pages if the document touches an existing synthesis topic.
- A single document ingest will typically touch 3–10 wiki pages.
Query
When a user asks a question:
- Check the wiki first —
wiki_store.search_wiki(query) to find relevant pages. Read them.
- If the wiki has a good answer, synthesize from wiki pages (fast path).
- If deeper graph traversal is needed, call
skill.query_with_evidence(query).
- Return the answer with evidence citations from
supporting_documents.
- If the answer is valuable, file it back as a new wiki topic page.
Lint
Periodically health-check the wiki:
from scripts.tools import wiki_store
issues = wiki_store.lint_wiki()
# Returns: {orphan_pages, missing_pages, broken_wikilinks, isolated_pages}
Ask the LLM to review and fix: broken links, orphan pages, stale claims, missing cross-references. See references/lint.md for full lint workflow.
Ingestion Constraints
- ❌ Do NOT hallucinate entities not present in the text
- ❌ Do NOT add relations without explicit textual evidence
- ❌ Do NOT add edges with confidence < 0.6
- ✅ Provide
supporting_text for every entity and relation — this enables provenance
- ✅ Write a wiki summary page for every ingested document
- ✅ Update existing entity pages when new information arrives
- ✅ Flag contradictions in wiki pages when new data conflicts with old claims
Retrieval Constraints
- 🔒 Traversal depth MUST NOT exceed 2 (config: MAX_GRAPH_DEPTH)
- 🔒 Only edges with confidence ≥ 0.6 (config: MIN_CONFIDENCE)
- 🔒 Maximum 50 nodes returned (config: MAX_NODES)
- ❌ Do NOT fabricate nodes or edges not in the graph
Full Python API Reference
| Method |
Purpose |
When to Use |
skill.ingest_with_content(doc_id, title, source, raw_content, entities, relations) |
Full RAG ingest: raw docs + graph + provenance |
Every new document |
skill.add_node(name, node_type) |
Add single entity (no provenance) |
Quick additions without a source doc |
skill.add_edge(source_name, target_name, relation, confidence) |
Add single relation |
Quick additions without a source doc |
skill.query(query) |
Graph-only retrieval → subgraph |
Structural queries |
skill.query_with_evidence(query) |
Graph + provenance → subgraph + source chunks |
Queries requiring citations |
wiki_store.write_page(category, title, content, summary) |
Write/update a wiki page |
After every ingest; after answering queries |
wiki_store.read_page(category, title) |
Read a wiki page |
Before answering; for cross-referencing |
wiki_store.search_wiki(query) |
Keyword search across wiki |
Fast path before graph traversal |
wiki_store.list_pages(category) |
List all wiki pages |
Getting an overview |
wiki_store.get_log(last_n) |
Read recent operations |
Understanding wiki history |
wiki_store.lint_wiki() |
Health check |
Periodic maintenance |
documents_store.list_documents() |
List all ingested raw sources |
Audit / provenance checking |
documents_store.search_chunks(query) |
Chunk-level search |
Finding specific evidence |
Design Philosophy
"The wiki is a persistent, compounding artifact. The cross-references are already there. The synthesis already reflects everything you've read." — Karpathy
| Layer |
What Happens |
Who Owns It |
| LLM Reasoning |
Extraction, synthesis, writing wiki pages |
Agent (.md guidance files) |
| Wiki Persistence |
Index, log, file I/O |
wiki_store.py |
| Graph Persistence |
Dedup, index, BFS traverse |
graph_store.py, retrieval_engine.py |
| Raw Source Storage |
Immutable docs + chunks + provenance |
documents_store.py |
The human curates sources and asks questions. The LLM writes the wiki, extracts the graph, and answers with citations. Python handles all bookkeeping.
1---2name: mini-context-graph3description: |4 A persistent, compounding knowledge base combining Karpathy's LLM Wiki pattern5 with a structured knowledge graph. Ingest documents once — the LLM writes wiki6 pages, extracts entities/relations into the graph, and stores raw content for7 evidence retrieval. Knowledge accumulates and cross-references; it is never8 re-derived from scratch.9metadata:10 source: https://skillmd.com/skills/github/mini-context-graph11---1213# Mini Context Graph Skill1415## The Core Idea1617Standard RAG re-discovers knowledge from scratch on every query. This skill is different:18191. **Wiki layer** — The LLM writes and maintains persistent markdown pages (summaries, entity pages, topic syntheses). Cross-references are already there. The wiki gets richer with every ingest.202. **Graph layer** — Entities and relations are extracted once and stored as a navigable knowledge graph. BFS traversal answers structural queries without re-reading sources.213. **Raw source layer** — Original documents are stored immutably with chunks. Provenance links tie every graph node and edge back to the exact text that supports it.2223> The LLM writes; the Python tools handle all bookkeeping.2425---2627## Three Layers2829| Layer | Where | What the LLM does | What Python does |30|-------|-------|-------------------|-----------------|31| **Raw Sources** | `data/documents.json` | Reads (never modifies) | Stores chunks + metadata |32| **Wiki** | `wiki/` (markdown) | Writes/updates pages | Manages index.md + log.md |33| **Graph** | `data/graph.json` | Extracts entities + relations | Persists, deduplicates, traverses |3435---3637## ⚡ Quick Start for Agents3839```python40from scripts.contextgraph import ContextGraphSkill41from scripts.tools import wiki_store4243skill = ContextGraphSkill()4445# ===== INGEST WITH FULL RAG + WIKI =====46# 1. Read references/ingestion.md and references/ontology.md first47# 2. Extract entities and relations (LLM reasoning step)48entities = [49 {"name": "memory leak", "type": "issue", "supporting_text": "memory leaks cause crashes"},50 {"name": "system crash", "type": "issue", "supporting_text": "system crashes due to memory leaks"},51]52relations = [53 {"source": "memory leak", "target": "system crash", "type": "causes",54 "confidence": 1.0, "supporting_text": "System crashes due to memory leaks."},55]5657result = skill.ingest_with_content(58 doc_id="doc_001",59 title="System Crash Analysis",60 source="/docs/incident_report.pdf",61 raw_content="System crashes due to memory leaks. Memory leaks occur when objects are not released.",62 entities=entities,63 relations=relations,64)65# result = {"doc_id": "doc_001", "chunk_count": 1, "nodes_added": 2, "edges_added": 1}6667# 3. Write a wiki summary page for this document68wiki_store.write_page(69 category="summary",70 title="System Crash Analysis Summary",71 content="""---72title: System Crash Analysis73source_document: doc_00174tags: [summary, incident]75---7677# System Crash Analysis7879**Source:** incident_report.pdf8081## Key Claims8283- [[memory-leak]] causes [[system-crash]] (confidence: 1.0)8485## Entities8687- [[memory-leak]] (issue)88- [[system-crash]] (issue)89""",90 summary="Incident report: memory leaks cause system crashes.",91)9293# ===== QUERY WITH EVIDENCE =====94result = skill.query_with_evidence("Why does the system crash?")95# Returns: {"query": ..., "subgraph": ..., "supporting_documents": [...], "evidence_chain": ...}9697# ===== WIKI SEARCH (read wiki before answering) =====98pages = wiki_store.search_wiki("memory leak")99# Returns: [{slug, category, path, snippet}, ...]100```101102---103104## Operations105106### Ingest107108When a user provides a new document:1091101. Read `references/ingestion.md` — entity/relation extraction rules.1112. Read `references/ontology.md` — type normalization rules.1123. Extract entities and relations using your LLM reasoning.1134. Call `skill.ingest_with_content(...)` — stores raw content + chunks + graph nodes + provenance.1145. **Write a wiki summary page** using `wiki_store.write_page(category="summary", ...)`.1156. **Update entity pages** — for each new/updated entity, write or update `wiki_store.write_page(category="entity", ...)`.1167. **Update topic pages** if the document touches an existing synthesis topic.1178. A single document ingest will typically touch 3–10 wiki pages.118119### Query120121When a user asks a question:1221231. **Check the wiki first** — `wiki_store.search_wiki(query)` to find relevant pages. Read them.1242. If the wiki has a good answer, synthesize from wiki pages (fast path).1253. If deeper graph traversal is needed, call `skill.query_with_evidence(query)`.1264. Return the answer with evidence citations from `supporting_documents`.1275. If the answer is valuable, file it back as a new wiki topic page.128129### Lint130131Periodically health-check the wiki:132133```python134from scripts.tools import wiki_store135issues = wiki_store.lint_wiki()136# Returns: {orphan_pages, missing_pages, broken_wikilinks, isolated_pages}137```138139Ask the LLM to review and fix: broken links, orphan pages, stale claims, missing cross-references. See `references/lint.md` for full lint workflow.140141---142143## Ingestion Constraints144145- ❌ Do NOT hallucinate entities not present in the text146- ❌ Do NOT add relations without explicit textual evidence147- ❌ Do NOT add edges with confidence < 0.6148- ✅ Provide `supporting_text` for every entity and relation — this enables provenance149- ✅ Write a wiki summary page for every ingested document150- ✅ Update existing entity pages when new information arrives151- ✅ Flag contradictions in wiki pages when new data conflicts with old claims152153---154155## Retrieval Constraints156157- 🔒 Traversal depth MUST NOT exceed 2 (config: MAX_GRAPH_DEPTH)158- 🔒 Only edges with confidence ≥ 0.6 (config: MIN_CONFIDENCE)159- 🔒 Maximum 50 nodes returned (config: MAX_NODES)160- ❌ Do NOT fabricate nodes or edges not in the graph161162---163164## Full Python API Reference165166| Method | Purpose | When to Use |167|--------|---------|-------------|168| `skill.ingest_with_content(doc_id, title, source, raw_content, entities, relations)` | Full RAG ingest: raw docs + graph + provenance | Every new document |169| `skill.add_node(name, node_type)` | Add single entity (no provenance) | Quick additions without a source doc |170| `skill.add_edge(source_name, target_name, relation, confidence)` | Add single relation | Quick additions without a source doc |171| `skill.query(query)` | Graph-only retrieval → subgraph | Structural queries |172| `skill.query_with_evidence(query)` | Graph + provenance → subgraph + source chunks | Queries requiring citations |173| `wiki_store.write_page(category, title, content, summary)` | Write/update a wiki page | After every ingest; after answering queries |174| `wiki_store.read_page(category, title)` | Read a wiki page | Before answering; for cross-referencing |175| `wiki_store.search_wiki(query)` | Keyword search across wiki | Fast path before graph traversal |176| `wiki_store.list_pages(category)` | List all wiki pages | Getting an overview |177| `wiki_store.get_log(last_n)` | Read recent operations | Understanding wiki history |178| `wiki_store.lint_wiki()` | Health check | Periodic maintenance |179| `documents_store.list_documents()` | List all ingested raw sources | Audit / provenance checking |180| `documents_store.search_chunks(query)` | Chunk-level search | Finding specific evidence |181182---183184## Design Philosophy185186> "The wiki is a persistent, compounding artifact. The cross-references are already there. The synthesis already reflects everything you've read." — Karpathy187188| Layer | What Happens | Who Owns It |189|-------|-----------|-------------|190| **LLM Reasoning** | Extraction, synthesis, writing wiki pages | Agent (.md guidance files) |191| **Wiki Persistence** | Index, log, file I/O | `wiki_store.py` |192| **Graph Persistence** | Dedup, index, BFS traverse | `graph_store.py`, `retrieval_engine.py` |193| **Raw Source Storage** | Immutable docs + chunks + provenance | `documents_store.py` |194195The human curates sources and asks questions. The LLM writes the wiki, extracts the graph, and answers with citations. Python handles all bookkeeping.196