Implementation:0xK3vin MegaMemory Stats CLI
| Knowledge Sources | |
|---|---|
| Domains | CLI, Observability, Diagnostics |
| Last Updated | 2026-10-27 14:00 GMT |
Overview
Concrete CLI command for printing knowledge graph statistics — node/edge counts, per-kind breakdown, token-cost estimates, and per-node size analysis — provided by the MegaMemory library.
Description
The Stats CLI (src/stats.ts) implements the megamemory stats command via the exported runStats function. It opens a KnowledgeDB, then prints a colorized report covering:
- Database: resolved path and file size (human-readable bytes)
- Graph: node count (split into root vs children), edge count, removed-node count, and timeline entry count
- list_roots: a realistic estimate of the list_roots MCP tool response — node count and approximate token cost (chars ÷ 4) — by actually calling listRoots(db) and serializing the payload the MCP server would send
- Kinds: per-kind node counts, sorted by a preferred KIND_ORDER (feature, module, component, pattern, config, decision) with any extra kinds appended alphabetically
- Size: total tokens, total chars/bytes, average tokens per node, and the largest/smallest nodes by token estimate
The token estimate uses the standard heuristic of chars ÷ 4. The list_roots estimate is significant because agents call list_roots at session start, so its token cost directly impacts the LLM context budget.
Usage
Import this function when you need to:
- Inspect graph size from the shell: megamemory stats
- Point at a non-default DB: megamemory stats --db /path/to/knowledge.db
- Diagnose token cost: Check how many tokens the list_roots response will consume before it overflows the context window
- Audit graph structure: See the per-kind breakdown and identify the largest/smallest concepts
Code Reference
Source Location
- Repository: 0xK3vin_MegaMemory
- File: src/stats.ts
- Lines: 1-170 (entire module)
- runStats: L55-170
- getFlag: L10-13
- getDefaultDbPath: L15-17
- formatBytes: L19-32
- formatNumber: L34-36
- printRow: L38-45
- sortedKinds: L47-53
Signature
export async function runStats(args: string[]): Promise<void>;
// Usage: megamemory stats [--db <path>]
// Resolves DB path from --db flag or MEGAMEMORY_DB_PATH env or cwd/.megamemory/knowledge.db
// Exits with code 1 if the database file is not found or cannot be stat'd.
// Internal helpers (not exported):
function getFlag(args: string[], flag: string): string | undefined;
function getDefaultDbPath(): string;
function formatBytes(bytes: number): string;
function formatNumber(value: number): string;
function printRow(label: string, value: string, detail?: string): void;
function sortedKinds(kinds: Record<string, number>): Array<[string, number]>;
Import
import { runStats } from "./stats.js";
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| args | string[] | Yes | CLI arguments (typically process.argv.slice(3)) |
| --db | string (flag) | No | Path to the knowledge DB; default cwd/.megamemory/knowledge.db or MEGAMEMORY_DB_PATH |
Outputs
| Name | Type | Description |
|---|---|---|
| runStats() | void (console) | Prints a colorized stats report to stdout; exits 1 on error |
| Report sections | text | Database, Graph, list_roots, Kinds, Size |
| Token estimates | number | list_roots and per-node token cost approximated as chars ÷ 4 |
Usage Examples
Basic Stats
# Print stats for the default DB (cwd/.megamemory/knowledge.db)
megamemory stats
# megamemory stats
#
# Database
# Path: /home/user/project/.megamemory/knowledge.db
# File size: 1.25 MB (1310720 bytes)
#
# Graph
# Nodes: 142 (12 root, 130 children)
# Edges: 89
# Removed: 7
# Timeline: 215 entries
#
# list_roots
# Nodes: 142 (12 roots + 130 children)
# Response size: ~3840 tokens (15360 chars)
#
# Kinds
# feature: 8
# module: 15
# component: 42
# ...
#
# Size
# Total tokens: ~12480
# Total chars: 49920 (51200 bytes)
# Avg node tokens:~88
# Largest: db-layer (520 tokens)
# Smallest: config (12 tokens)
Custom DB Path
# Point at a merged DB
megamemory stats --db /tmp/merged.db
# Or via environment variable
MEGAMEMORY_DB_PATH=/tmp/merged.db megamemory stats
Programmatic Import
import { runStats } from "./stats.js";
// Run with the default DB
await runStats([]);
// Run with a custom DB path
await runStats(["--db", "/tmp/merged.db"]);