Chapter 3
Metagraphs
A metagraph drops the line between vertex and edge. Edges can be vertices, vertices can hold subgraphs, and both can be nested to any depth. This chapter gives the definitions and the operations, and lists what a store must provide.
Three definitions, one idea
The word "metagraph" has been used for several related structures. Three are worth knowing.
Basu and Blanning
Basu and Blanning define a metagraph over a set of elements X, its generating set. A metaedge connects a set of elements to a set of elements:
M = ⟨X, E⟩
e = ⟨V, W⟩ V ⊆ X (invertex set), W ⊆ X (outvertex set)
So a metaedge is already a directed hyperedge from chapter 2. What Basu and Blanning add is set-to-set edges and metapaths: a path in a metagraph is a set of edges that together connect a source set to a target set, not a single chain. In their definition an edge is not itself an element of X. Letting edges act as elements is the later step, taken by Gapanyuk's metavertices below.
Gapanyuk's annotated metagraph
Gapanyuk adds metavertices: a vertex that contains vertices, edges and other metavertices. Metavertices and metaedges carry annotations, and an annotation may itself be a metagraph. Provenance, confidence and semantic type are all annotations in this view.
Goertzel's fold and unfold
Goertzel's paper Folding and Unfolding on Metagraphs treats fold and unfold as recursion schemes over metagraphs. This book uses the words in the simpler sense of Metagraph for AI Agents: two operations that move between levels of detail. Unfold opens a metavertex into the subgraph it stands for. Fold collapses a subgraph into one metavertex. A query or an algorithm can work at the folded level ("Alice had a meeting") and unfold only where it needs detail ("with whom, about what"). Chapter 9 shows both as ordinary reads and writes.
The two powers
Strip the definitions down and a metagraph has two powers that a hypergraph lacks.
| Power | Meaning | In the running example |
|---|---|---|
| Edges are vertices | A relationship can be the subject or object of another relationship. | The fact :alice :worksAt :acme has a confidence of 0.9 and a source, :chat12. |
| Vertices are containers | A vertex can hold a subgraph and still be a vertex. | :session7 holds what the agent learned in one conversation, and is itself in :aliceMemory. |
Two more follow from combining them:
- Edges are containers. If an edge is a vertex, and a vertex can hold a subgraph, then an edge can hold a subgraph. The meeting
e1can contain its minutes: the facts that were stated in it. - The model is data. If anything can be a vertex, so can a schema element. "
:Meetinghas roles:agent,:targetand:topic" is a statement like any other. A system whose schema, rules and data share one representation is called homoiconic. It can query and change its own schema with the same tools.
Recursive metagraphs
Apply the powers to their own results and you get a recursive metagraph. Each level is a vertex for the level above:
level 0 a fact
s1 = (:alice, :worksAt, :acme)
level 1 statements about the fact
s2 = (s1, :confidence, 0.9)
s3 = (s1, :source, :chat12)
level 2 a statement about the statement about the fact
s4 = (s3, :checkedBy, :carol)
containers at every level
s1, s2, s3 ∈ :session7 :session7 ∈ :aliceMemory :aliceMemory ∈ :agentMemory
The same structure repeats at every scale: a fact inside an episode, an episode inside a person's memory, that memory inside the agent's. The same queries and algorithms work at each level. This self-similarity is why one small set of operations is enough.
What breaks without each power
| Missing | What you cannot say | Usual workaround |
|---|---|---|
| Edges as vertices | How sure, where from, who said it | Reify by hand: a statement node with rdf:subject, rdf:predicate, rdf:object. Four extra triples per fact. |
| Vertices as containers | Which facts belong to one episode or document | A :context property on every fact, or one database per context. |
| Edges as containers | What happened inside a relationship | A stand-in node for the edge, which drifts when the edge changes. |
| Model as data | Change the schema in the same transaction as the data; reason over rules | A separate schema file and migration tool. |
Why there are few metagraph databases
Most stores are built on one of four shapes, and none holds a metagraph natively. Relational tables are flat. Property graphs have binary edges whose properties hold values, not nodes or other edges. Classic RDF can only reify with four extra triples. Document stores have no graph queries. Each can approximate a metagraph, and the approximations are the subject of the Metagraph for AI Agents book. They share one move: give every relationship an identity, and let identities appear anywhere a vertex can.
That move is the definition of a statement graph, the subject of chapter 4.
In graphlava. The data model spec names four metagraph kinds, each mapped onto statements and IDs:
| Kind | Logical form | Chapter |
|---|---|---|
| statements about statements | (stmt#42, :confidence, 0.9) | 6 |
| n-ary hyperedge | role triples (e, :role, node), one record | 5 |
| nested graph | (stmt, :in, g), (g, :in, parent) | 7 |
| meta-model | schema, shapes and rules as statements in the system graph | 8 |
Takeaways
- A metaedge links sets to sets; metavertices let a structure be an element of a larger one.
- Two powers: edges are vertices, vertices are containers. Combined, they give edges as containers and a model stored as data.
- Fold and unfold move between levels of detail. Recursion makes the same operations work at every level.