---
title: "ZeroMemory + Context Graph"
description: "Build agents with persistent memory, knowledge graphs, and GraphRAG search"
canonical: "https://docs.ainative.studio/docs/guides/zerodb-memory"
last-updated: "2026-10-03T21:47:10.764Z"
---

# ZeroMemory + Context Graph

Source: https://docs.ainative.studio/docs/guides/zerodb-memory

> Build agents with persistent memory, knowledge graphs, and GraphRAG search

## ZeroMemory + Context Graph

ZeroMemory gives your AI agents persistent cognitive memory. The Context Graph adds a knowledge graph layer on top for multi-hop reasoning and contextual retrieval.

## Quick Start

### 1. Provision a database

```bash
curl -X POST https://api.ainative.studio/api/v1/public/instant-db
## Returns: api_key (tmp_ for anonymous, sk_ for authenticated),
## project_id, mcp block, database connection
```

### 2. Store memories

```python
import requests

BASE = "https://api.ainative.studio/api/v1/public/memory/v2"
HEADERS = {"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json"}

## Store a memory
requests.post(f"{BASE}/remember", headers=HEADERS, json={
    "content": "Alice is the CTO of AINative Studio. She prefers Python.",
    "session_id": "onboarding-session",
    "tags": ["person", "preference"]
})
```

### 3. Build the knowledge graph

Entities and edges are auto-extracted from memories, or you can create them explicitly:

```python
GRAPH = f"{BASE}/graph"

## Create entities
requests.post(f"{GRAPH}/entity", headers=HEADERS, json={
    "canonical_name": "Alice Johnson",
    "entity_type": "person",
    "aliases": ["alice", "AJ"],
    "properties": {"title": "CTO"}
})

requests.post(f"{GRAPH}/entity", headers=HEADERS, json={
    "canonical_name": "AINative Studio",
    "entity_type": "org"
})

## Create a relationship
requests.post(f"{GRAPH}/edge", headers=HEADERS, json={
    "source_name": "Alice Johnson",
    "target_name": "AINative Studio",
    "predicate": "works_at",
    "confidence": 0.95
})
```

### 4. Traverse the graph

```python
result = requests.post(f"{GRAPH}/traverse", headers=HEADERS, json={
    "entity": "Alice Johnson",
    "max_hops": 3
}).json()

## Returns nodes, edges, and paths reachable from Alice
print(f"Found {len(result['nodes'])} connected entities")
```

### 5. GraphRAG hybrid search

```python
results = requests.post(f"{GRAPH}/graphrag", headers=HEADERS, json={
    "query": "Who leads the engineering team?",
    "limit": 5,
    "graph_weight": 0.3  # 70% vector similarity + 30% graph proximity
}).json()

for r in results["results"]:
    print(f"Score: {r['score']:.2f} | Vector: {r['vector_score']:.2f} | Graph: {r['graph_boost']:.2f}")
    print(f"  {r['content'][:100]}...")
```

---

## Memory Hierarchy

ZeroMemory organizes information in three tiers:

| Tier | Purpose | Retention |
|------|---------|-----------|
| **Working** | Current conversation context | Session-scoped, decays fast |
| **Episodic** | Specific interactions and events | Days to weeks |
| **Semantic** | Consolidated knowledge and patterns | Long-term, low decay |

Memories promote upward via the `reflect()` engine, which clusters related memories and synthesizes insights.

---

## Ontology Setup

Define what types of entities and relationships exist in your domain:

### Manual definition

```python
requests.post(f"{GRAPH}/ontology", headers=HEADERS, json={
    "project_id": PROJECT_ID,
    "entity_types": ["customer", "order", "product"],
    "predicates": {
        "customer": {"places": "order", "prefers": "product"},
        "order": {"contains": "product"}
    }
})
```

### Auto-infer from usage

After storing enough data, let ZeroDB detect patterns:

```python
proposed = requests.post(f"{GRAPH}/ontology/infer", headers=HEADERS,
    params={"min_entity_count": 5, "min_predicate_count": 3}
).json()

## Review the proposed ontology, then apply it
requests.post(f"{GRAPH}/ontology/apply", headers=HEADERS, json=proposed["proposed_ontology"])
```

---

## Templates

Skip manual setup with pre-built ontologies:

```python
## List available templates
templates = requests.get(f"{GRAPH}/templates", headers=HEADERS).json()

## Apply one
requests.post(f"{GRAPH}/templates/customer-support/apply", headers=HEADERS,
    params={"project_id": PROJECT_ID})
```

**Available templates:** customer-support, research, code-review, sales, knowledge-base

---

## Edge Versioning

When relationships change (Alice leaves Company X, joins Company Y), ZeroDB tracks the history:

```python
## This auto-supersedes the old "works_at" edge
requests.post(f"{GRAPH}/edge", headers=HEADERS, json={
    "source_name": "Alice Johnson",
    "target_name": "New Company",
    "predicate": "works_at"
})

## View contradictions
contradictions = requests.get(f"{GRAPH}/contradictions", headers=HEADERS).json()

## Resolve
requests.post(f"{GRAPH}/contradictions/{edge_id}/resolve", headers=HEADERS,
    json={"action": "accept_new"})  # or "keep_both" or "reject_new"
```

---

## MCP Integration

Use the Context Graph via MCP tools in any AI IDE:

```bash
## Install the memory MCP (10 tools including graph)
npx ainative-zerodb-memory-mcp

## Or the full server (83 tools)
npx ainative-zerodb-mcp-server
```

The MCP server exposes `zerodb_graph_traverse`, `zerodb_graphrag_search`, `zerodb_entity_neighbors`, `zerodb_entity_list`, `zerodb_entity_merge`, and `zerodb_graph_stats` tools.
