Agentic Knowledge Graphs over PubMed
A citation and ontology knowledge graph across the full PubMed 2026 baseline,
with every answer traceable to a source path, and anything untraceable refused
rather than generated.
Headline
83.2% on 600 held-out PubMedQA questions (95% CI 80.2 to 86.2), against a
53.3% majority-class floor and 37.2% for the same model given no evidence.
The knowledge graph is built from 929,824,202 edges with zero LLM calls,
against a published cost of roughly $33,000 in API spend for comparable
LLM-extracted graphs on a single dataset.
Every number below is reproducible on a laptop in about ten seconds.
No GPU, no corpus download, no API key.
Table of contents
- What this is
- Quick start
- Architecture
- Results
- Evaluation protocol
- Key findings
- Repository layout
- Reproducing the analysis
- Rebuilding the GPU pipeline
- Limitations and open questions
- Data sources and licensing
- Engineering notes
1. What this is
Retrieval-augmented generation over a very large corpus has a governance problem
before it has a quality problem. A system that answers 90% of questions well and
fabricates the other 10% without signalling which is which is unusable in a
clinical or regulatory setting. The interesting question is not "how do we make
the answers better" but "how do we make the system's entitlement to answer
legible".
This project builds that system over the whole of PubMed and then tries hard to
break its own claims.
mindmap
root(("Agentic KG<br/>over PubMed"))
Corpus
("1,334 XML files")
("28.3M abstracts embedded")
("Full 2026 baseline")
Graph
("929,824,202 edges")
("Zero LLM calls")
("CSR, in RAM")
("Citations + MeSH")
Answering
("Constrained decoding")
("No parsing regex")
("Full posterior returned")
Governance
("Calibrated abstention")
("Source path per answer")
("Thesis tested and refuted")
What makes it unusual: the study is adversarial toward itself. Four
previously reported conclusions were overturned by later measurement, including
the project's own founding thesis. Section 6 documents each
one, what produced it, and what the corrected number is. Negative results are
reported at the same prominence as positive ones.
2. Quick start
Read it (no setup at all):
notebook/agentic_kg_40m.ipynb
196 cells, already executed, with all output and 29 figures inline.
Run it (about ten seconds, CPU only):
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
python analysis/run_all.py
Expected tail:
ALL ANALYSES COMPLETE in 10.4s
configurations built 139 (137 selectable; oracle + blind excluded)
split DEV 400 / TEST 600
winner p0|concat|ce_k1
no-evidence floor 37.2%
majority-class floor 53.3%
graph certification AUROC 0.500 (chance)
free confidence AUROC 0.810
TEST accuracy (argmax) 82.3%
TEST accuracy 83.2%
reproduces the published 83.2%: YES
Exit code 0 means the shipped numbers reproduced. Outputs are written to
reproduced/, leaving results/ untouched so the two can be diffed.
Only numpy and matplotlib are required. The GPU work is pre-computed and
cached, which is why this is fast; see section 8.
3. Architecture
3.0 End to end
flowchart TD
subgraph INGEST["INGEST · one time · about 2.5 hours"]
A["PubMed 2026 baseline<br/>1,334 gzipped XML files"]
A --> B["parse_pubmed.py<br/>10.8 min"]
B --> C[("Parquet columnar store<br/>articles · citations · MeSH")]
end
subgraph BUILD["INDEX"]
C --> D["build_graph1..4.py"]
C --> E["embed_corpus3.py<br/>about 2 hours"]
D --> F[("CSR graph, in RAM<br/><b>929,824,202 edges</b><br/><b>ZERO LLM calls</b>")]
E --> G[("Dense index<br/>28,336,648 vectors<br/>384-dim fp16")]
end
subgraph SERVE["QUERY TIME"]
Q(["Biomedical question"])
Q --> H["HybridGrounder<br/>lexical + dense linker"]
H --> I["MeSH descriptors"]
Q --> J["Dense retrieval<br/>exact, no ANN index"]
J --> K["16-candidate pool<br/>gold present <b>97.4%</b>"]
K --> L["Cross-encoder rerank<br/>gold at rank 1: <b>96.2%</b>"]
L --> M["Qwen2.5-14B-Instruct<br/>constrained decoding"]
M --> N{"Calibrated<br/>abstention"}
N -->|"confident"| O(["Answer with source path"])
N -->|"not confident"| P(["Refusal"])
end
G -.->|"vectors"| J
I -.->|"concepts"| L
F -.->|"provenance"| O
style F fill:#2a78d6,color:#fff
style G fill:#1baf7a,color:#fff
style O fill:#1baf7a,color:#fff
style P fill:#eda100,color:#000
style N fill:#4a3aa7,color:#fff
Plain-text version of the same diagram, for viewers without Mermaid
PubMed 2026 baseline · 1,334 gzipped XML files
|
[ parse_pubmed.py · 10.8 min ]
v
Parquet columnar store · articles, citations,
MeSH headings, qualifiers
|
+---------------------+----------------------+
v v
[ build_graph1..4.py ] [ embed_corpus3.py ]
CSR adjacency, in RAM bge-small-en-v1.5
------------------------ -------------------------
CITES / CITED_BY 28,336,648 abstract vectors
ARTICLE <-> MeSH descriptor 384 dimensions, fp16
929,824,202 edges total exact search, no ANN index
ZERO LLM calls
| |
+---------------------+----------------------+
v
question -> [ HybridGrounder ] -> MeSH descriptors (lexical + dense)
|
v
[ dense retrieval ] -> 16-candidate pool -> [ cross-encoder rerank ]
|
v
[ Qwen2.5-14B-Instruct · constrained decoding over {yes, no, maybe} ]
|
v
[ calibrated abstention ] -> answer + source path, or refusal
3.1 Graph construction
The graph is built entirely from structured metadata that NLM already
publishes: citation edges and human-assigned MeSH indexing. No language model
reads a document to extract entities or relations.
erDiagram
ARTICLE ||--o{ CITES : "outbound citation"
ARTICLE ||--o{ CITED_BY : "inbound citation"
ARTICLE }o--o{ MESH_DESCRIPTOR : "annotated with"
MESH_DESCRIPTOR ||--o{ MESH_DESCRIPTOR : "tree parent of"
MESH_DESCRIPTOR ||--o{ QUALIFIER : "refined by"
MESH_DESCRIPTOR ||--o{ ENTRY_TERM : "surface form"
ARTICLE {
int pmid PK
text title
text abstract
int year
}
MESH_DESCRIPTOR {
string ui PK
string name
string tree_numbers
int min_depth
}
ENTRY_TERM {
string surface
string source "MeSH or SCR"
}
| property |
value |
| total edges |
929,824,202 |
| build time |
10.8 minutes |
| LLM calls |
0 |
| representation |
Compressed Sparse Row, in RAM |
| edge types |
CITES, CITED_BY, ARTICLE <-> MeSH descriptor |
This matters for cost and for provenance. An LLM-extracted graph over one
dataset has been reported at roughly $33,000 in API spend, and its edges are
model assertions. Here every edge is a fact NLM published, so a path through the
graph is a citable claim about the literature rather than a model's opinion
about it.
CSR was chosen over a graph database deliberately. The workload is
breadth-first neighbourhood expansion over a static graph, which is an
indptr[i]:indptr[i+1] slice in CSR and a query planner round trip in a
database. The whole structure fits in RAM on a single machine.
3.2 Query path, step by step
sequenceDiagram
autonumber
actor U as User
participant G as HybridGrounder
participant V as Dense index<br/>28.3M vectors
participant R as Cross-encoder
participant K as CSR graph
participant M as Qwen2.5-14B
participant C as Calibrated gate
U->>G: biomedical question
G-->>U: MeSH descriptors (lexical + dense)
U->>V: query embedding
V-->>U: top 16 candidates (exact search)
Note over V: gold document present 97.4%
U->>R: rerank 16 candidates jointly
R-->>U: reordered pool
Note over R: gold at rank 1 for 96.2%
U->>M: top-1 document, 3000 chars
M-->>C: posterior over yes / no / maybe
Note over M: constrained decoding,<br/>no parsing regex
C->>K: resolve source path for the cited PMID
K-->>C: citation and MeSH provenance
alt confidence above DEV-fitted threshold
C-->>U: answer plus source path
else below threshold
C-->>U: refusal
end
3.3 Retrieval
Dense retrieval uses BAAI/bge-small-en-v1.5 over title plus abstract, with
exact search (a full matrix product against all 28.3M vectors on GPU) rather
than an approximate index. At this scale exact search is affordable and removes
recall loss as a confounder from every downstream measurement.
| retrieval property |
value |
| gold document inside 16-candidate pool |
97.4% |
| gold document at rank 1 after rerank |
96.2% |
| residual error attributable to retrieval |
1.3% |
Retrieval is effectively solved for this benchmark. That fact is what makes the
rest of the study meaningful: when retrieval is not the bottleneck, differences
between arms are differences in reading and deciding, not in finding.
3.4 Answering
The generator is Qwen2.5-14B-Instruct, run locally in bf16. Labels are obtained
by constrained decoding: a single forward pass, reading the softmax mass on
the yes, no and maybe token ids (both lower and capitalised forms) at the
first generated position, then renormalising over those three.
flowchart LR
A["Prompt:<br/>system + evidence + question"] --> B["Single forward pass"]
B --> C["Logits at final position"]
C --> D["Select 6 token ids:<br/>yes/no/maybe<br/>lower + capitalised"]
D --> E["Softmax, then<br/>renormalise over 3 classes"]
E --> F(["Posterior<br/>P(yes), P(no), P(maybe)"])
F --> G["argmax + DEV-fitted<br/>class bias"]
F --> H["max P(y)<br/>as confidence"]
G --> I(["Label"])
H --> J(["Abstention signal"])
style F fill:#2a78d6,color:#fff
style I fill:#1baf7a,color:#fff
style J fill:#4a3aa7,color:#fff
This removes an entire class of evaluation bug. A free-text regex such as
\b(yes|no|maybe)\b misreads hedged prose ("there is no clear indication
that...") as a label. Constrained decoding makes that failure mode structurally
impossible, and it returns a full posterior rather than a point prediction, which
is what makes the calibration and abstention analysis possible at all.
4. Results
4.1 Headline
All figures below are on the 600-question held-out TEST split. Configuration
and decision rule were fitted on a disjoint 400-question DEV split.
| system |
accuracy |
95% CI |
| No evidence (question only) |
37.2% |
|
Majority class (constant yes) |
53.3% |
|
| Previous best, before correction |
68.8% |
65.0 to 72.5 |
| Cross-encoder top-1, argmax |
82.3% |
79.3 to 85.3 |
| Cross-encoder top-1, calibrated |
83.2% |
80.2 to 86.2 |
xychart-beta
title "Held-out TEST accuracy, n = 600"
x-axis ["no evidence", "majority", "before fix", "argmax", "calibrated"]
y-axis "Accuracy (%)" 0 --> 100
bar [37.2, 53.3, 68.8, 82.3, 83.2]
The winning configuration, selected on DEV out of 137 candidates, is
p0|concat|ce_k1: a single cross-encoder-reranked document in the prompt, with a
class-prior bias fitted on DEV. It is the simplest arm in the search space.
4.2 Error budget
Every error on TEST assigned to exactly one cause:
pie showData
title Error budget on 600 held-out questions
"Answered correctly" : 82.3
"Residual, both renderings wrong" : 14.3
"Distraction" : 2.0
"Gold never retrieved" : 1.3
| bucket |
share |
what would fix it |
| Answered correctly |
82.3% |
|
| Gold never retrieved |
1.3% |
better retrieval |
| Distraction (gold retrieved, answer still wrong) |
2.0% |
better ranking or reading |
| Residual (both renderings wrong) |
14.3% |
reading or label limit |
4.3 Where the residual lives
| gold class |
n |
correct |
retrieval |
distraction |
residual |