Expand description
§hirn — A Brain for Large Language Models
hirn /hɪʁn/ (German: brain) — a cognitive memory database engine for LLM systems, written in Rust.
hirn is a purpose-built database engine for cognitive memory. Not a wrapper around a vector database. Not an agent framework. A production-grade memory engine implementing neuroscience-grounded layered memory — working, episodic, and semantic — with graph-based associations, spreading activation, Hebbian self-organization, and narrative consolidation.
Ship it how you need it:
hirn— embed as a library. LanceDB-backed, zero network overhead. Like SQLite for memory.hirnd— run as a standalone daemon with gRPC + HTTP + MCP.
§Quick Start
The easiest way to get started is HirnMemory — zero-config, auto-detects
embedding providers from environment variables:
use hirn::prelude::*;
#[tokio::main]
async fn main() -> HirnResult<()> {
let memory = HirnMemory::open("./brain").await?;
memory.remember("User prefers dark mode").await?;
let ctx = memory.think("What are the user's preferences?", 2048).await?;
println!("{}", ctx.context);
Ok(())
}§Tokenizers
Tokenizer implementations are provider-owned and resolved through
hirn_engine::ProviderRegistry. The tiktoken feature is enabled by
default; enable hf-tokenizer for local HuggingFace tokenizers. When no
model-backed tokenizer is available, hirn falls back to EstimatingTokenizer.
For fine-grained control, use Hirn directly with a PhysicalStore:
use hirn::prelude::*;
use hirn_storage::{HirnDb, HirnDbConfig};
#[tokio::main]
async fn main() -> HirnResult<()> {
// Open LanceDB storage
let config = HirnDbConfig::local("./brain/lance");
let storage = HirnDb::open(config).await.unwrap().store_arc();
// Open the database
let brain = Hirn::open("./brain", storage).await.unwrap();
// Register an agent
let agent = AgentId::new("my_agent").unwrap();
brain.register_agent(&agent, "My Agent").await.unwrap();
// Get an agent-scoped context
let ctx = brain.as_agent(&agent).await.unwrap();
// Remember an experience (episodic memory)
let episode = EpisodicRecord::builder()
.content("Benchmark: HNSW with PQ outperforms brute-force by 40x")
.event_type(EventType::Experiment)
.agent_id(agent.clone())
.importance(0.85)
.embedding(vec![0.1; 768])
.build()
.unwrap();
let id = ctx.remember(episode).await.unwrap();
// Recall with spreading activation
let results = brain
.recall_view()
.query(vec![0.1; 768])
.activation(ActivationMode::Spreading)
.limit(10)
.execute()
.await
.unwrap();
// Think — assemble optimal LLM context under token budget
let context = brain
.recall_view()
.think(vec![0.1; 768])
.budget(4096)
.execute()
.await
.unwrap();
// Or use HirnQL directly
let result = brain.ql().execute(
r#"RECALL episodic ABOUT "vector database" LIMIT 5"#
).await.unwrap();
Ok(())
}§Architecture
hirn implements three memory layers inspired by neuroscience:
| Layer | Brain Analog | Purpose |
|---|---|---|
| Working | Prefrontal cortex | Token-bounded scratchpad for active reasoning |
| Episodic | Hippocampus | Time-anchored experiences and events |
| Semantic | Neocortex | Consolidated knowledge and stable facts |
All memories exist as nodes in a typed property graph with spreading activation, Hebbian co-retrieval learning, causal reasoning, and namespace-based multi-agent isolation.
§Module Organization
prelude— Common imports for most use casesagent— Multi-agent context and namespace isolationepisodic— Episodic memory records and builderssemantic— Semantic knowledge records and buildersworking— Working memory entries and buildersgraph— Property graph with typed edgesactivation— Spreading activation with lateral inhibitioncausal— Causal chain extraction and counterfactual reasoningconsolidation— Episodic → semantic consolidation enginescoring— Composite relevance scoringhebbian— Hebbian edge weight learningql— HirnQL query language parser and executorprovenance— Provenance tracking and audit trailsecurity— Anomaly detection and quarantinevector— HNSW vector index (advanced)
Re-exports§
pub use memory::HirnMemory;pub use memory::MemoryRecallBuilder;pub use memory::MemoryThinkBuilder;
Modules§
- activation
- Spreading activation with lateral inhibition.
- agent
- Multi-agent context, namespace isolation, and team management.
- causal
- Causal chain extraction and counterfactual reasoning.
- consolidation
- Episodic → semantic consolidation engine.
- content
- Multi-modal content payloads and composite embedding helpers.
- episodic
- Episodic memory — the hippocampus.
- graph
- Property graph with typed, weighted edges.
- hebbian
- Hebbian edge weight learning.
- memory
HirnMemory— zero-config, high-level memory API.- metadata
- Metadata key-value storage.
- prelude
- Common imports for most hirn use cases.
- procedural
- Procedural memory — the basal ganglia / cerebellum.
- provenance
- Provenance tracking and audit trail.
- ql
- HirnQL — the cognitive memory query language.
- query
- Recall and think builder APIs.
- record
- Memory record types spanning all layers.
- resource
- First-class resource memory types: resources, artifacts, hydration, and governance.
- scoring
- Composite relevance scoring.
- security
- Memory security: anomaly detection and quarantine.
- semantic
- Semantic memory — the neocortex.
- working
- Working memory — the prefrontal cortex.
Structs§
- Agent
Context - Agent-scoped database context enforcing namespace isolation.
- Char
Estimate Counter - Character-estimate fallback:
ceil(len / 4). Always available, zero dependencies. - Chat
Message - Chat message for LLM generation.
- Conflict
Resolution Policy - Conflict
Resolution Policy Overrides - DbStats
- Database statistics.
- Defaults
Config - Which provider name to use as default for each category.
- Embedder
Circuit Breaker Runtime Config - Embedder
Config - Configuration for a single embedder provider.
- Embedder
Persistent Cache Runtime Config - Embedder
Retry Config - Embedder
Runtime Config - Embedding
- A single embedding result with its source model identifier.
- Estimating
Tokenizer - Character-estimate tokenizer:
ceil(len / 4). Zero dependencies, always available. - Extracted
Entity - An entity extracted from unstructured text.
- Extracted
Relation - A relation between two extracted entities.
- Hirn
Config - Database configuration with production-ready defaults.
- HirnDB
- The main database handle.
- Integrity
Issue - A specific integrity issue found in the database.
- Integrity
Report - Result of a database integrity check.
- Layer
Counts - Layer counts.
- LlmChunk
- A single chunk from a streaming LLM response.
- LlmConfig
- Configuration for a single LLM provider.
- LlmOptions
- Options for LLM generation requests.
- LlmResponse
- Full response from the LLM provider.
- Memory
Id - Time-sortable, globally unique memory identifier wrapping a ULID.
- Noop
Reranker - Identity reranker — returns all documents in original order.
- Provider
Config - Top-level provider configuration, TOML-deserializable.
- Provider
Defaults - Names of the default providers in each category.
- Provider
Registry - Central registry for AI providers, supporting runtime hot-swap.
- Providers
Section - The
[providers]section: maps category → name → config. - Repair
Report - Result of a repair operation.
- Rerank
Result - Result of a reranking operation on a single document.
- Reranker
Config - Configuration for a single reranker provider.
- Revision
Id - Immutable identifier for a specific revision of a logical memory.
- Semantic
Revision Integrity Issue - A specific semantic revision integrity issue.
- Semantic
Revision Integrity Report - Result of validating semantic revision chains and the runtime head cache.
- Semantic
Revision Repair Report - Result of repairing the semantic revision head cache.
- Timestamp
- Nanosecond-precision timestamp backed by
chrono::DateTime<Utc>. - Token
Usage - Token usage reported by the provider.
- Tokenizer
Config - Configuration for a single tokenizer provider.
Enums§
- ApiKey
Source - How an API key is specified in the TOML config.
- Hirn
Error - Errors emitted by the hirn library.
- Issue
Kind - Categories of integrity issues.
- Memory
Event - An event emitted when the database state changes.
- Recall
Snapshot - Snapshot target for revision-aware recall.
- Response
Format - Desired response format from the LLM.
- Semantic
Revision Issue Kind - Categories of semantic revision integrity issues.
- Store
Error - Internal storage error conversions.
Traits§
- Embedder
- Pluggable embedding provider (F-39).
- Entity
Extractor - Entity and relation extraction from unstructured text (F-41).
- LlmProvider
- Pluggable LLM provider for structured extraction and generation (F-41, §12).
- Reranker
- Two-stage reranker (§12.4.2): cross-encoder precision after bi-encoder recall.
- Token
Counter - Pluggable token counter (§11.5).
- Tokenizer
- Pluggable tokenizer providing encode, decode, truncate, and count operations.
Functions§
Type Aliases§
- Hirn
- The database handle. Open a
Hirnto start working with cognitive memory. - Hirn
Result - Result type alias for hirn operations.
- LlmStream
- Stream of LLM chunks.