mentedb 0.7.2

A purpose-built database engine for AI agent memory
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Token Efficiency Benchmark
//!
//! Measures token consumption across MenteDB's serialization formats
//! (Compact, Structured, Raw JSON) and multi-turn delta serving.
//!
//! Run with: cargo run --example token_efficiency -p mentedb
//!
//! Outputs markdown tables suitable for README / documentation.

use mentedb::context::budget::estimate_tokens;
use mentedb::context::{
    AssemblyConfig, ContextAssembler, DeltaTracker, OutputFormat, ScoredMemory,
};
use mentedb::prelude::*;
use mentedb_core::types::AgentId;

/// Realistic memory content samples spanning different types and lengths.
const MEMORY_CONTENTS: &[(&str, MemoryType)] = &[
    // Semantic — preferences, facts, decisions
    (
        "User prefers Rust for backend services and TypeScript for frontend",
        MemoryType::Semantic,
    ),
    (
        "The production database is PostgreSQL 15 running on RDS with read replicas",
        MemoryType::Semantic,
    ),
    (
        "Authentication uses OAuth2 with Google as the identity provider",
        MemoryType::Semantic,
    ),
    (
        "The deployment target is AWS ECS Fargate with auto-scaling policies",
        MemoryType::Semantic,
    ),
    (
        "API versioning follows URL-based strategy with /v1/ and /v2/ prefixes",
        MemoryType::Semantic,
    ),
    (
        "Redis is used for caching with a 15 minute TTL and LRU eviction",
        MemoryType::Semantic,
    ),
    (
        "The CI pipeline uses GitHub Actions with parallel test execution across 4 runners",
        MemoryType::Semantic,
    ),
    (
        "Monitoring stack is Prometheus for metrics and Grafana for dashboards",
        MemoryType::Semantic,
    ),
    (
        "Background tasks use a custom job queue backed by SQS with dead letter queues",
        MemoryType::Semantic,
    ),
    (
        "The project follows trunk-based development with short-lived feature branches",
        MemoryType::Semantic,
    ),
    // Episodic — conversation events
    (
        "User asked about implementing WebSocket support for real-time notifications in the dashboard",
        MemoryType::Episodic,
    ),
    (
        "User debugged a race condition in the connection pool that caused intermittent 500 errors",
        MemoryType::Episodic,
    ),
    (
        "User decided to migrate from REST to gRPC for internal service communication",
        MemoryType::Episodic,
    ),
    (
        "User discussed implementing rate limiting at the API gateway level using token buckets",
        MemoryType::Episodic,
    ),
    (
        "User refactored the authentication middleware to support multiple identity providers",
        MemoryType::Episodic,
    ),
    // Procedural — how-to knowledge
    (
        "To deploy: run cargo build --release, docker build, push to ECR, update ECS service",
        MemoryType::Procedural,
    ),
    (
        "Database migrations: create migration with sqlx, test on staging, apply with --dry-run first",
        MemoryType::Procedural,
    ),
    (
        "To add a new API endpoint: define route, add handler, write integration test, update OpenAPI spec",
        MemoryType::Procedural,
    ),
    // AntiPattern — things to avoid
    (
        "Never store API keys in environment variables visible in logs or error messages",
        MemoryType::AntiPattern,
    ),
    (
        "Do not use SELECT * in production queries, always specify explicit column lists",
        MemoryType::AntiPattern,
    ),
    // Correction — updated facts
    (
        "Corrected: the health check timeout should be 30 seconds not 300 seconds",
        MemoryType::Correction,
    ),
    (
        "Corrected: the maximum batch size for SQS is 10 messages not 100",
        MemoryType::Correction,
    ),
    // Reasoning — derived insights
    (
        "The intermittent latency spikes correlate with garbage collection pauses in the JVM sidecar",
        MemoryType::Reasoning,
    ),
    (
        "Based on traffic patterns, the optimal auto-scaling threshold is 70% CPU utilization",
        MemoryType::Reasoning,
    ),
    (
        "The user's architecture follows a modified hexagonal pattern with ports and adapters",
        MemoryType::Reasoning,
    ),
];

fn main() {
    println!("# MenteDB Token Efficiency Benchmark\n");
    println!(
        "Measures token consumption across serialization formats and multi-turn delta serving.\n"
    );

    format_comparison();
    println!();
    scale_comparison();
    println!();
    delta_savings();
    println!();
    budget_density();
}

/// Compare token counts for the same memories across formats.
fn format_comparison() {
    println!("## Format Comparison\n");
    println!("Same 25 memories serialized in each format:\n");

    let memories = generate_memories(25);

    // Raw JSON baseline (what you get without an optimized serializer)
    let json_output = serialize_as_json(&memories);
    let json_tokens = estimate_tokens(&json_output);

    // MenteDB Compact
    let compact_config = AssemblyConfig {
        token_budget: 100_000, // no budget limit for this test
        format: OutputFormat::Compact,
        include_edges: false,
        include_metadata: true,
    };
    let compact_window = ContextAssembler::assemble(memories.clone(), vec![], &compact_config);
    let compact_tokens = estimate_tokens(&compact_window.format);

    // MenteDB Structured
    let structured_config = AssemblyConfig {
        token_budget: 100_000,
        format: OutputFormat::Structured,
        include_edges: false,
        include_metadata: true,
    };
    let structured_window =
        ContextAssembler::assemble(memories.clone(), vec![], &structured_config);
    let structured_tokens = estimate_tokens(&structured_window.format);

    println!("| Format | Tokens | vs Raw JSON |");
    println!("|--------|-------:|------------:|");
    println!("| Raw JSON | {json_tokens} | — |");
    println!(
        "| Structured (markdown) | {structured_tokens} | {:.1}x fewer |",
        json_tokens as f64 / structured_tokens as f64
    );
    println!(
        "| **Compact (pipe-delimited)** | **{compact_tokens}** | **{:.1}x fewer** |",
        json_tokens as f64 / compact_tokens as f64
    );

    println!("\n<details><summary>Sample output: Compact format</summary>\n");
    println!("```");
    // Show first 15 lines
    for line in compact_window.format.lines().take(15) {
        println!("{line}");
    }
    println!("...");
    println!("```\n</details>");
}

/// Measure how token savings scale with memory count.
fn scale_comparison() {
    println!("## Token Savings at Scale\n");
    println!("Compact format vs Raw JSON at increasing memory counts:\n");

    println!("| Memories | Raw JSON | Compact | Savings |");
    println!("|---------:|---------:|--------:|--------:|");

    for count in [10, 25, 50, 100, 250] {
        let memories = generate_memories(count);

        let json_output = serialize_as_json(&memories);
        let json_tokens = estimate_tokens(&json_output);

        let config = AssemblyConfig {
            token_budget: 1_000_000,
            format: OutputFormat::Compact,
            include_edges: false,
            include_metadata: true,
        };
        let window = ContextAssembler::assemble(memories, vec![], &config);
        let compact_tokens = estimate_tokens(&window.format);

        let savings = (1.0 - compact_tokens as f64 / json_tokens as f64) * 100.0;
        println!("| {count} | {json_tokens} | {compact_tokens} | {savings:.1}% |");
    }
}

/// Simulate multi-turn conversation and measure delta savings.
fn delta_savings() {
    println!("## Multi-Turn Delta Serving\n");
    println!(
        "Simulates a 20-turn conversation where 1-3 memories change per turn.\n\
         Full retrieval resends everything each turn. Delta only sends changes.\n"
    );

    let agent_id = AgentId::new();
    let num_turns = 20;

    // Build a pool of memories (simulating growing knowledge)
    let mut all_memories: Vec<ScoredMemory> = Vec::new();
    let mut tracker = DeltaTracker::new();

    let config = AssemblyConfig {
        token_budget: 100_000,
        format: OutputFormat::Structured,
        include_edges: false,
        include_metadata: true,
    };

    let mut total_full_tokens = 0usize;
    let mut total_delta_tokens = 0usize;
    let mut turn_data: Vec<(usize, usize, usize)> = Vec::new(); // (turn, full, delta)

    for turn in 0..num_turns {
        // Add 1-3 new memories per turn
        let new_count = 1 + (turn % 3);
        for j in 0..new_count {
            let idx = (turn * 3 + j) % MEMORY_CONTENTS.len();
            let (content, mem_type) = MEMORY_CONTENTS[idx];
            let mut node = MemoryNode::new(
                agent_id,
                mem_type,
                format!("[turn {turn}] {content}"),
                vec![],
            );
            node.salience = 0.5 + (0.5 * ((turn * 7 + j * 13) % 10) as f32 / 10.0);
            let score = 0.3 + (0.7 * ((turn * 11 + j * 3) % 10) as f32 / 10.0);
            all_memories.push(ScoredMemory {
                memory: node,
                score,
            });
        }

        // Full retrieval: assemble all current memories
        let full_window = ContextAssembler::assemble(all_memories.clone(), vec![], &config);
        let full_tokens = estimate_tokens(&full_window.format);
        total_full_tokens += full_tokens;

        // Delta retrieval: only send changes
        let delta_window =
            ContextAssembler::assemble_delta(all_memories.clone(), vec![], &mut tracker, &config);
        let delta_tokens = estimate_tokens(&delta_window.format);
        total_delta_tokens += delta_tokens;

        turn_data.push((turn + 1, full_tokens, delta_tokens));
    }

    println!("| Turn | Memories | Full (tokens) | Delta (tokens) | Turn Savings |");
    println!("|-----:|---------:|--------------:|---------------:|-------------:|");
    let mut mem_count = 0;
    for (i, (turn, full, delta)) in turn_data.iter().enumerate() {
        mem_count += 1 + (i % 3);
        let savings = if *full > 0 {
            (1.0 - *delta as f64 / *full as f64) * 100.0
        } else {
            0.0
        };
        println!("| {turn} | {mem_count} | {full} | {delta} | {savings:.1}% |");
    }

    let total_savings = if total_full_tokens > 0 {
        (1.0 - total_delta_tokens as f64 / total_full_tokens as f64) * 100.0
    } else {
        0.0
    };

    println!("\n**Cumulative over {num_turns} turns:**\n");
    println!("| Metric | Tokens |");
    println!("|--------|-------:|");
    println!("| Full retrieval (total) | {total_full_tokens} |");
    println!("| Delta serving (total) | {total_delta_tokens} |");
    println!("| **Total savings** | **{total_savings:.1}%** |");
}

/// Measure how many memories fit within common token budgets per format.
fn budget_density() {
    println!("## Memory Density per Token Budget\n");
    println!(
        "How many memories fit within a given **serialized output** token budget.\n\
         This measures what the LLM actually receives:\n"
    );

    println!("| Budget | Compact | Structured | Raw JSON |");
    println!("|-------:|--------:|-----------:|---------:|");

    for budget in [1_000, 2_000, 4_096, 8_192, 16_384] {
        let memories = generate_memories(500); // large pool

        let compact_count = serialized_fit_count(&memories, OutputFormat::Compact, budget);
        let structured_count = serialized_fit_count(&memories, OutputFormat::Structured, budget);
        let json_count = json_fit_count(&memories, budget);

        println!(
            "| {} | {} | {} | {} |",
            format_number(budget),
            compact_count,
            structured_count,
            json_count
        );
    }
}

// --- Helpers ---

fn format_number(n: usize) -> String {
    let s = n.to_string();
    let mut result = String::new();
    for (i, c) in s.chars().rev().enumerate() {
        if i > 0 && i % 3 == 0 {
            result.push(',');
        }
        result.push(c);
    }
    result.chars().rev().collect()
}

fn generate_memories(count: usize) -> Vec<ScoredMemory> {
    let agent_id = AgentId::new();
    (0..count)
        .map(|i| {
            let idx = i % MEMORY_CONTENTS.len();
            let (content, mem_type) = MEMORY_CONTENTS[idx];
            let mut node = MemoryNode::new(
                agent_id,
                mem_type,
                if count > MEMORY_CONTENTS.len() {
                    format!("{content} (instance {i})")
                } else {
                    content.to_string()
                },
                vec![],
            );
            node.salience = 0.5 + (0.5 * ((i * 7) % 10) as f32 / 10.0);
            node.tags = vec!["project".to_string()];
            let score = 0.3 + (0.7 * ((i * 11) % 10) as f32 / 10.0);
            ScoredMemory {
                memory: node,
                score,
            }
        })
        .collect()
}

fn serialize_as_json(memories: &[ScoredMemory]) -> String {
    let items: Vec<serde_json::Value> = memories
        .iter()
        .map(|sm| {
            serde_json::json!({
                "id": sm.memory.id.to_string(),
                "type": format!("{:?}", sm.memory.memory_type),
                "content": sm.memory.content,
                "salience": sm.memory.salience,
                "score": sm.score,
                "tags": sm.memory.tags,
                "created_at": sm.memory.created_at,
            })
        })
        .collect();
    serde_json::to_string_pretty(&items).unwrap_or_default()
}

fn serialized_fit_count(memories: &[ScoredMemory], format: OutputFormat, budget: usize) -> usize {
    // Binary search: find the maximum number of memories whose serialized output fits in budget
    let mut sorted: Vec<ScoredMemory> = memories.to_vec();
    sorted.sort_by(|a, b| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    // Use huge internal budget so the assembler doesn't limit us — we measure the output
    let config = AssemblyConfig {
        token_budget: 1_000_000,
        format,
        include_edges: false,
        include_metadata: true,
    };

    let mut lo = 0usize;
    let mut hi = sorted.len();
    while lo < hi {
        let mid = (lo + hi).div_ceil(2);
        let subset: Vec<ScoredMemory> = sorted[..mid].to_vec();
        let window = ContextAssembler::assemble(subset, vec![], &config);
        let tokens = estimate_tokens(&window.format);
        if tokens <= budget {
            lo = mid;
        } else {
            hi = mid - 1;
        }
    }
    lo
}

fn json_fit_count(memories: &[ScoredMemory], budget: usize) -> usize {
    let mut tokens_used = 0;
    let mut count = 0;
    // Sort by score descending like the assembler does
    let mut sorted: Vec<&ScoredMemory> = memories.iter().collect();
    sorted.sort_by(|a, b| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    for sm in sorted {
        let json = serde_json::to_string(&serde_json::json!({
            "id": sm.memory.id.to_string(),
            "type": format!("{:?}", sm.memory.memory_type),
            "content": sm.memory.content,
            "salience": sm.memory.salience,
            "tags": sm.memory.tags,
        }))
        .unwrap_or_default();
        let t = estimate_tokens(&json);
        if tokens_used + t > budget {
            break;
        }
        tokens_used += t;
        count += 1;
    }
    count
}