llm-memory-graph
Graph-based context-tracking and prompt-lineage database for LLM systems.
llm-memory-graph provides a persistent, queryable graph database specifically designed for tracking LLM interactions, managing conversation contexts, and tracing prompt lineage through complex multi-agent systems.
Features
- Graph-based Storage: Store conversations, prompts, completions, and relationships as a connected graph
- Flexible Node Types: Support for multiple specialized node types:
PromptNode: Track prompts and their metadataCompletionNode: Store LLM responsesConversationNode: Organize multi-turn dialoguesToolInvocationNode: Track tool/function callsAgentNode: Multi-agent system coordinationDocumentNode,ContextNode,FeedbackNode, and more
- Edge Properties: Rich relationships with metadata, timestamps, and custom properties
- Query System: Powerful query interface for traversing and filtering the graph
- Async Support: Full async/await support with tokio runtime
- Streaming Queries: Efficient streaming for large result sets
- Persistent Storage: Built on Sled embedded database
- Type Safety: Strongly-typed API with comprehensive error handling
- Observability: Built-in metrics and telemetry integration
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
Basic Usage
use ;
use HashMap;
async
Async Streaming Queries
use ;
use StreamExt;
async
Working with Templates
use ;
// Create a reusable prompt template
let template = new;
// Render with variables
let mut vars = new;
vars.insert;
let rendered = template.render?;
Core Concepts
Node Types
The graph supports multiple specialized node types for different use cases:
- PromptNode: User prompts and instructions
- CompletionNode: LLM-generated responses
- ConversationNode: Multi-turn conversation containers
- ToolInvocationNode: Function/tool call records
- AgentNode: Multi-agent system coordination
- DocumentNode: Source documents and context
- ContextNode: Contextual information and metadata
- FeedbackNode: Human feedback and ratings
Edge Types
Relationships between nodes are typed:
- Generates: Prompt generates completion
- References: Node references another
- Contains: Container contains items
- Triggers: Action triggers another
- DependsOn: Dependency relationship
- Precedes: Temporal ordering
Query System
Powerful query interface with:
- Type filtering
- Time-range queries
- Metadata filtering
- Graph traversal
- Pagination
- Streaming results
Advanced Features
Edge Properties
Edges can carry rich metadata:
let mut edge_metadata = new;
edge_metadata.insert;
edge_metadata.insert;
edge_metadata.insert;
graph.create_edge_with_properties?;
Migration Support
Built-in migration system for schema evolution:
use ;
let mut engine = new;
engine.add_migration?;
engine.run_migrations?;
Observability Integration
Export metrics to Prometheus:
use ;
let observatory = new;
let exporter = new?;
observatory.add_exporter;
Use Cases
- Conversation Management: Track multi-turn conversations with full history
- Prompt Engineering: Version and test prompt variations
- Multi-Agent Systems: Coordinate communication between multiple LLM agents
- RAG Pipelines: Track document retrieval and context usage
- Observability: Monitor LLM usage patterns and performance
- Debugging: Trace prompt lineage and decision paths
- A/B Testing: Compare different prompt strategies
- Compliance: Audit trails for LLM interactions
Architecture
Built on proven technologies:
- Storage: Sled embedded database for persistence
- Graph: Petgraph for in-memory graph operations
- Serialization: Multiple formats (JSON, MessagePack, Bincode)
- Async: Tokio runtime for concurrent operations
- Caching: Moka for intelligent query caching
- Metrics: Prometheus integration
Examples
The repository includes comprehensive examples:
simple_chatbot.rs: Basic chatbot with conversation trackingasync_streaming_queries.rs: Async query patternsedge_properties.rs: Working with edge metadataprompt_templates.rs: Template system usagetool_invocations.rs: Tool call trackingobservatory_demo.rs: Observability integrationmigration_guide.rs: Schema migration patterns
Run an example:
Performance
Optimized for production use:
- Throughput: 10,000+ events/sec
- Latency: p95 < 200ms
- Caching: Automatic query result caching
- Batch Operations: Bulk insert/update support
- Streaming: Memory-efficient result streaming
Integration
Designed to integrate with the LLM DevOps ecosystem:
- LLM-Observatory: Real-time telemetry ingestion
- LLM-Registry: Model metadata synchronization
- LLM-Data-Vault: Secure storage with encryption
- gRPC API: High-performance API server
- REST API: HTTP/JSON interface
Documentation
Contributing
Contributions are welcome! Please ensure:
- All tests pass:
cargo test - Code is formatted:
cargo fmt - No clippy warnings:
cargo clippy - Add tests for new features
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.