Leanpub Header

Skip to main content

Metagraph for AI Agents

Metagraph for AI Agents
This book is 100% completeUpdated 3 Dec 2026

Your agent remembers facts, but not why it believes them, so it cannot tell when a belief or a saved answer has lost its support. This build-first book shows you how to give an LLM agent a memory that records episodes, keeps the evidence behind every belief, revises beliefs when their support changes, and hands the next prompt context it can cite. You run a working memory in Chapter 2, without an API key, and grow it chapter by chapter into the shared memory of a multi-agent team.

Minimum price

$24.76

$37.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

Buying multiple copies for your team? See below for a discount!

PDF
EPUB
WEB
APP
About

About

About the Book

An LLM agent that works with people for weeks has to remember what happened, and that is a data-modeling problem before it is a retrieval problem. Extraction proposes facts. The memory has to decide how to identify, validate and keep them, and whether the agent can still inspect their evidence a month later.

Vector stores and plain knowledge graphs store facts. They struggle with facts about facts: which of two reports to trust, which claim someone disputed, which saved answer rested on a preference the user has since changed. Metagraphs for AI Agents builds a memory where those are ordinary queries.

The foundation is the statement graph: every statement has its own identity, so other statements can point at it to give its source, its confidence, a dispute, or the belief derived from it. Each statement carries two clocks, for when the memory learned it and when it was true, so you can correct a fact without losing its history. Containers and n-ary events are built from the same statements, which gives you metagraphs (graphs whose edges can hold and connect other edges) without a separate engine.

What you will build

  • A small memory in Chapter 2, run with one command through a real MCP server: it admits and rejects proposed memories, corrects a preference, detects the stale answer that depended on it, and retrieves attributed context. No LLM key needed.
  • A statement memory (Chapters 4–7): a travel memory you can see statement by statement, then the memory of a four-agent research team, with an extraction gate in front of the LLM and a context serializer whose output the answer must cite.
  • Memory for agents working together (Chapters 8–11): A2A tasks and MCP tool calls recorded as statements with provenance, teams as holons with enforced boundaries, Semantic Spacetime types for retrieval, and promise graphs that put commitments, verdicts and trust in the same structure as knowledge.
  • Typed contexts and stored rules (Chapters 12–13): dependent typing in a restricted, decidable fragment, with types indexed by stored contexts and refined by predicates, and λProlog rules and queries stored as metagraphs.
  • The same memory on the database you already run (Chapters 14–19): SQL (SQLite, DuckDB, PostgreSQL), LadybugDB, TypeDB, RDF 1.2 and property graphs, ending with a scorecard of what each engine enforces and what your application must.
  • A release gate (Chapter 20): a decision guide, a reference architecture and the checks to pass before an agent memory ships.

Every example runs

The companion repository holds the code for every chapter, pinned versions, and the output the book prints, under the MIT license and checked by CI: https://github.com/Volland/metagraphs-for-ai-agents. Where a guarantee can be proved, it is proved in Lean 4. The book labels each claim as proved, tested, designed or not executed, so you know what you can rely on.

Who this book is for

Engineers building memory for LLM agents and multi-agent systems. You should know basic graphs and SQL and have called an LLM from code. SPARQL and the statement syntax are taught through examples. Part Four also asks for comfort with type judgments and logic programming, with worked examples before the formal details.

What you need

Python 3.10+, a Rust toolchain to install the Tiramemsu 0.13.0 command-line tool and MCP server, and, for the backend chapters, the database you want to try. No API key is needed for the build path

Share this book

Featured in our articles on

Team Discounts

Team Discounts

Get a team discount on this book!

  • Up to 3 members

    Minimum price
    $61.00
    Suggested price
    $92.00
  • Up to 5 members

    Minimum price
    $99.00
    Suggested price
    $148
  • Up to 10 members

    Minimum price
    $173
    Suggested price
    $259
  • Up to 15 members

    Minimum price
    $247
    Suggested price
    $370
  • Up to 25 members

    Minimum price
    $371
    Suggested price
    $555

Author

About the Author

Volodymyr Pavlyshyn

Hey I am Volodymyr 

Seasoned Developer's Journey from COBOL to Web 3.0, SSI, Privacy First Edge AI, and Beyond

 As a seasoned developer with over 20 years of experience, I have worked with various programming languages, including some that are considered "dead," such as COBOL and Smalltalk. However, my passion for innovation and embracing cutting-edge technology has led me to focus on the emerging fields of Web 5.0, Self-Sovereign Identity (SSI),AI Agents, Knowledge Graphs, Agentiic memory systems, and the architecture of a decentralized world that empowers data democratization.

A firm believer in the potential of agent systems and the concept of a "soft" internet, I am dedicated to exploring and promoting these transformative ideas. In addition to writing, I also enjoy sharing my knowledge and insights through videoblogging. Most of my Medium posts serve as supplementary content to the videos on my YouTube channel, which you can explore here: https://www.youtube.com/c/VolodymyrPavlyshyn. 

Join me on this exciting journey as we delve into the future of technology and the possibilities it holds.

Contents

Table of Contents

Part One: Foundations

Introduction: Building Memory That Agents Can Revise

  1. What an agent has to remember
  2. Where agent memory stands in 2026
  3. How the Book Builds the Memory
  4. The Book’s Structure
  5. Build First, Then Choose Your Depth

Graphs, Hypergraphs and Metagraphs

  1. Graphs
  2. Hypergraphs
  3. Metagraphs: a working definition
  4. The thesis, stated once
  5. What reification costs, and when it is right
  6. Choosing a level
  7. An agent-memory query
  8. Build a Small Memory Now
  9. Summary
  10. Further Reading

Metagraph Formalisms

  1. What We Need to Represent
  2. Basu–Blanning Metagraphs
  3. Gapanyuk’s Annotated Metagraph
  4. Goertzel’s Metagraphs and OpenCog Hyperon
  5. The Three Side by Side
  6. Homoiconicity and Self-Similarity
  7. The Impedance Problem
  8. Summary
  9. Further Reading
  10. Part Two: Building Agent Memory

Statement Graphs: Statements About Statements

  1. The Trouble With a Plain Fact
  2. What the Workarounds Lose
  3. What a Statement Graph Is
  4. One Brick, Many Structures
  5. And Time
  6. Why “No Loops, Nothing Missing” Matters
  7. What It Looks Like in RDF 1.2
  8. Statement Graphs as Metagraphs
  9. Summary

Building Metagraphs from Statement Graphs

  1. Half a Metagraph, and the Other Half
  2. The Formal Target
  3. One Idea: A Metavertex Is Any Id That Has Contents
  4. Power One: An Edge That Holds a Subgraph
  5. Power Two: Metavertices Inside Metavertices
  6. Power Three: Edges Between Sets
  7. Power Four: Enclose and Expand
  8. Time Comes for Free
  9. Annotated Metagraphs: Annotations Are Layers
  10. What We Deliberately Do Not Do
  11. How the Pieces Map
  12. Three Invariants for the Memory Builder
  13. Where the Proofs Stop
  14. Summary

Tiramemsu: Statement Graphs in Practice

  1. From Model to Store
  2. Build Milestone
  3. Three Vocabularies
  4. Loading the Travel Memory
  5. Facts, Occurrences and Reports
  6. Seeing the Statements
  7. The Five Patterns in SPARQL
  8. The Same Memory in Cypher
  9. Traveling in Time
  10. The Metagraph Containers in Practice
  11. Exporting a Statement Graph
  12. Checking the Export with Lean
  13. The Theorems
  14. The Memory as MCP Tools
  15. Summary
  16. Further Reading

Agent Memory as a Statement Graph

  1. Run the Memory Before Extending It
  2. What Cognitive Science Lends
  3. The Agent-Memory Landscape
  4. One Statement Graph, Six Ways of Reading It
  5. Time: Two Clocks and the Tensor Clock
  6. The Extraction Step
  7. Context Engineering: Choosing the Subgraph
  8. The Whole Loop
  9. Evaluating Agent Memory
  10. The Memory over MCP
  11. Summary
  12. Further Reading
  13. Part Three: Agents Working Together

Agent Interaction and Protocols

  1. Beyond Pairwise Interaction
  2. What Group Interactions Mean for Memory
  3. Multi-Tool Orchestration: What the Hyperedge Adds
  4. The Protocols Agents Actually Speak
  5. Recording an Interaction as a Metagraph
  6. What the Metagraph Adds over the Hypergraph
  7. Summary
  8. Further Reading

Holonic Systems

  1. The Holon and Its Janus Face
  2. Near-Decomposability: A Boundary You Can Measure
  3. Holarchy and Multiple Containment
  4. Levels: Self-Similarity and More Is Different
  5. Agents as Holons
  6. A Team Recorded as a Holon
  7. Boundaries: Shapes over an Export, Rules at Commit
  8. Emergence and Resilience
  9. Holons as Superagents
  10. Summary
  11. Further Reading

Semantic Spacetime

  1. Where Semantic Spacetime Comes From
  2. The Four Relation Types
  3. The γ(3,4) Representation
  4. SST as a Vocabulary for Metaedges
  5. Reasoning Along Typed Paths
  6. Typed Relations and Embeddings
  7. Summary
  8. Further Reading

Promise Graphs

  1. Promise Theory in Brief
  2. Promises in an Agent Architecture
  3. Six Layers of a Promise Graph
  4. The Research Team, Step by Step
  5. Storing the Promise Graph
  6. Verdicts That Go Stale
  7. Causal Analysis: What Was Promised and What Happened
  8. Promises, A2A Tasks and Agent SLAs
  9. Memory and Promises in One Store
  10. Summary
  11. Further Reading
  12. Part Four: Typed Contexts and Stored Rules

Metastrudel: Typed Metagraphs

  1. The Price of Generality
  2. Second-Class Citizens
  3. A Worked Example Before the Rules
  4. The Core: Atoms and Links
  5. Contexts, Homes and Membership
  6. Three Layers
  7. Context-Relative Typing
  8. Signatures, Rules and Opt-In Enforcement
  9. Parameters Bound by Links on Links
  10. What Kind of Typing Is This?
  11. For AI Engineers
  12. The Five Derivation Rules
  13. Refinement Types
  14. Views
  15. Conformance
  16. Checking at Commit Time
  17. What Is Proved, and What Is Not
  18. Statement Graphs and Typed Metagraphs Side by Side
  19. Summary
  20. Further Reading

Higher-Order Logic in the Metagraph

  1. What “Higher-Order” Means Here
  2. Why Datalog Alone Is Not Enough
  3. λProlog in One Page
  4. The DSL as Users See It
  5. Scope as Hypothetical Implication
  6. Patterns of Higher-Order Reasoning
  7. Queries as Metagraphs
  8. Two Engines, Routed per Component (design, not yet implemented)
  9. Beyond the Decidable Core: Verdicts and Certificates
  10. How the Pieces Fit
  11. Elpi in Tiramemsu, Briefly
  12. Summary
  13. Further Reading
  14. Part Five: Choosing a Backend

Hypergraphs in Depth

  1. The Running Hypergraph
  2. Variants
  3. Representations
  4. Hypergraphs in Python
  5. Algorithms
  6. Identity, Attributes and Roles
  7. Semantic Hypergraphs
  8. Knowledge Hypergraphs and LLM Extraction
  9. Summary
  10. Further Reading

Hypergraphs and Metagraphs in Relational Databases

  1. The meeting as rows
  2. Constraints SQL can enforce
  3. Direction, two ways
  4. From hyperedge to metagraph
  5. Choosing a level, and where this leads
  6. Summary
  7. Further Reading

Bipartite and Multipartite Graphs in LadybugDB

  1. LadybugDB in brief
  2. The Levi graph
  3. Schema: typed layers and multi-pair REL tables
  4. Loading the episode
  5. Querying the Levi graph
  6. Meta-edges: edges between edge-nodes
  7. The S-layer: contexts
  8. Search, transactions and export
  9. Comparison with the relational encoding
  10. An agent’s tool calls as hyperedges
  11. Summary

TypeDB: Typed Relations as Hyperedges and Metaedges

  1. The PERA Model
  2. A Relation Is a Hyperedge With Named Slots
  3. Variable Arity and Cardinality
  4. Relations Playing Roles: The Metagraph Step
  5. Functions: Recursion and Inference
  6. An Agent-Memory Schema
  7. The Role-Player Gap
  8. In Practice
  9. Summary
  10. Further Reading

RDF 1.2: Triple Terms, Reifiers and Named Graphs

  1. The Meeting as Plain RDF
  2. Classic Reification
  3. Three Granularities in RDF 1.2
  4. Edges About Edges
  5. Rules You Have to Check Yourself
  6. Engines and Related Models
  7. RDF 1.2 Against the Statement Memory
  8. Choosing a Granularity
  9. Summary
  10. Further Reading

Property Graphs: Identified Edges, No Metaedges

  1. The Model
  2. Attributes, Not Edges About Edges
  3. Property Graphs and RDF 1.2
  4. Reification, Gradually
  5. Systems That Let Edges Be Nodes
  6. Standards: ISO GQL and SQL/PGQ
  7. When a Property Graph Is Enough
  8. Part Five Scorecard
  9. Choosing a Backend for the Memory You Built
  10. Summary
  11. Further Reading

Conclusion: Shipping an Agent Memory System

  1. Start from the Questions Your Agent Must Answer
  2. A Reference Architecture for Agent Memory
  3. A Release Gate for the Memory System
  4. Migrating from Where You Are
  5. What Is Proved, Tested and Designed
  6. Open Problems
  7. Summary

Appendix A: Notation and Glossary

  1. Notation
  2. Glossary

Appendix B: Tiramemsu and LeanRDF Reference

  1. B.1 Installing
  2. B.2 The Statement-Graph Model in LeanRDF
  3. B.3 The Layered Theorems in Full
  4. B.4 The tiramemsu-mcp Tool Catalogue
  5. B.5 More Queries on the Travel Memory
  6. B.6 More Containers: Enclose With GRAPH, Roles and Schema
  7. B.7 Rust Quickstart: Correcting a Fact
  8. B.8 Query Recipes for Agent Memory
  9. B.9 The Extraction Gate

Appendix C: Metastrudel Reference

  1. C.1 Building the Reference Executable
  2. C.2 The Derivation Rules in Lean
  3. C.3 The Ten Conformance Rules
  4. C.4 Open Proofs, the Axiom Audit and the Preservation Induction
  5. C.5 Clause Forms the Query Codec Round-Trips

References

  1. Metagraph and Hypergraph Theory
  2. Databases and Query Languages
  3. Statement Graphs, Tiramemsu and Formal Methods
  4. Agent Memory and Cognitive Science
  5. Multi-Agent Systems, Protocols and Higher-Order Networks
  6. Holons, Semantic Spacetime and Promise Theory
  7. Software, Standards and Specifications

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub