English | 한국어 | 日本語 | 简体中文 | 繁體中文 | Español | Français | Deutsch | Português | Русский | Italiano
llm-kernel
Foundation library for Rust AI-native apps — provider catalog, LLM client, MCP server, search, telemetry, and safety
Overview
llm-kernel provides the foundational layer for building LLM-powered tools, agents, and servers in Rust:
- Provider catalog — 16 built-in providers, 114 models with metadata, pricing, and capabilities
- Async client — trait-based client for OpenAI and Anthropic with SSE streaming
- Model discovery — dynamic model discovery from models.dev, Ollama, OpenAI-compatible endpoints
- Credential vault — dotenv-style API key management with atomic writes
- Config loader — TOML config with auto-create from template
- Knowledge graph —
GraphBackendtrait (SQLite impl), FTS5 search, smart recall, BFS traversal, CJK search, schema migrations, async wrappers - MCP server — JSON-RPC 2.0 server framework with stdio and HTTP/SSE transports, async handlers, Bearer auth
- Key-value store —
KvStoretrait powering LLM response caching and other byte-oriented stores - Embedding — provider trait + cosine similarity, local ONNX (44 models), Qwen3 candle, Nomic V2 MoE candle, OpenAI remote, compressed vector indexing (full model list →)
- Search — Reciprocal Rank Fusion for hybrid search result merging
- Token estimation — zero-dependency Unicode-script heuristic token counting
- Telemetry — enum-gated events with no PII, console and noop sinks
- Safety — secret masking, error classification, output sanitization
- Install wizard — MCP config generation for Claude Desktop, Cursor, Copilot, OpenCode, Cline
Feature flags
Each module is gated behind a feature flag so you only pay for what you use.
| Feature | Description | Default |
|---|---|---|
provider |
Provider catalog, model descriptors, pricing | ✅ |
client-async |
Async LLM client (reqwest) with streaming | |
discovery |
Dynamic model discovery (models.dev, Ollama, OpenAI-compat) | |
discovery-async |
Async model discovery — DiscoverySource trait over reqwest |
|
secrets |
SecretVault credential management | |
store |
SQLite init helpers (WAL, FTS5, schema versioning) + KvStore |
|
config |
TOML config loader | |
graph |
Knowledge graph — GraphBackend trait, SQLite impl, FTS5, smart recall, BFS, migrations |
|
graph-async |
Async graph wrappers (requires tokio) | |
graph-pool |
Multi-connection async graph pool (AsyncPoolGraph, WAL concurrency) |
|
graph-cjk |
CJK-aware graph search via Rust-side segmentation (no schema change) | |
mcp |
MCP server — JSON-RPC 2.0, stdio transport, async handlers, Bearer auth | |
mcp-http |
MCP remote transport — HTTP/SSE (axum + tokio) | |
cache |
LLM response cache — CacheClient over KvStore |
|
tokens |
Token estimation, budgeting, and sentence-aware document chunking | |
install |
AI tool installation wizard | |
search |
Hybrid search — SearchProvider trait, RRF / weighted-sum / CombMNZ fusion |
|
embedding |
Embedding provider trait + cosine similarity | |
embedding-openai |
OpenAI text-embedding client (sync HTTP) | |
embedding-fastembed |
Local ONNX embedding via fastembed-rs (44 models) | |
embedding-fastembed-qwen3 |
Qwen3 embedding via candle backend | |
embedding-fastembed-nomic-moe |
Nomic V2 MoE embedding via candle backend | |
vector-index |
TurboQuant compressed vector index — 2-bit/4-bit, SIMD ANN search | |
telemetry |
Enum-gated telemetry events, no PII | |
safety |
Secret masking, error classification, output sanitization, prompt-injection detection | |
eval |
Quality evaluation CLI — tokens, safety, embedding, search | |
eval-full |
All eval modules including graph | |
full |
All features |
Quick start
Add to your Cargo.toml:
[]
= "0.7.0"
The provider feature is enabled by default. For the async client:
[]
= { = "0.7.0", = ["client-async"] }
For the knowledge graph with async wrappers:
[]
= { = "0.7.0", = ["graph", "graph-async"] }
For local embedding (ONNX, no API key):
[]
= { = "0.7.0", = ["embedding-fastembed"] }
Usage
Provider catalog
The embedded catalog contains 16 providers with 114 models aligned to the models.dev schema.
use *;
let catalog = embedded;
// List all providers
for id in catalog.ids
// Query models for a provider
for model in catalog.models_for
// Find a specific model
if let Some = catalog.find_model
Async chat completion
use *;
let config = ModelConfig ;
let client = new?;
let response = client.complete.await?;
println!;
println!;
Streaming
use *;
let config = ModelConfig ;
let client = new?;
let stream = client.stream_complete.await?;
// Stream yields Delta, Usage, and Done events
Model discovery
use ;
// Fetch from models.dev (caches to disk)
let payload = fetch_and_cache?;
for model in &payload.models
// Load from cache (no network)
if let Some = load_cache?
// Discover local Ollama models
let ollama_models = fetch_ollama_models?;
for name in &ollama_models
Async discovery
The discovery-async feature exposes a pluggable DiscoverySource trait so model listings can be fetched from any async backend behind one interface:
use ;
let source = new;
let models = source.discover.await?; // Vec<ModelEntry>
Credential vault
use *;
let vault = load_from?;
vault.set;
vault.save_to?;
// Redact credentials for logging
println!;
// → "sk-abcd...7890"
TOML config
use load_toml_config;
use Deserialize;
let config: AppConfig = load_toml_config?;
SQLite store
use init_schema;
let ddl = "CREATE TABLE items (id TEXT PRIMARY KEY, content TEXT);";
let conn = init_schema?;
// WAL mode, busy timeout, and schema versioning applied automatically
Knowledge graph
use *;
use Connection;
let conn = open_in_memory.unwrap;
init_graph_schema.unwrap;
// Create nodes
upsert_node.unwrap;
// Connect with edges
append_edge.unwrap;
// Smart recall with composite scoring
let results = smart_recall.unwrap;
for scored in &results
// Lifecycle management
decay_importance.unwrap;
tag_stale_nodes.unwrap;
let stats = compute_stats.unwrap;
println!;
MCP server
use ;
use json;
let mut server = new;
server.register_tool;
// Runs JSON-RPC 2.0 over stdio with Bearer auth
server.run_stdio.await?;
Token estimation
use estimate_tokens;
let tokens = estimate_tokens;
println!;
Sentence-aware chunking splits a long document into token-budgeted chunks (CJK + Latin terminators, optional overlap):
use ;
let chunks = chunk_text;
Embedding + search
use ;
use ;
// Cosine similarity between vectors
let sim = cosine_similarity;
// Reciprocal Rank Fusion for hybrid search
let bm25 = vec!;
let vector = vec!;
let merged = rrf_fuse;
A SearchProvider trait unifies ranking backends behind one sync interface, with min-max normalization and alternative fusion strategies:
use ;
// A dependency-free keyword backend behind the unified trait
let index = new;
let mut hits = index.search?;
// Normalize each backend to [0,1] before score-based fusion
normalize_minmax;
Local ONNX embedding (fastembed-rs)
44 models via ONNX Runtime — no API key, no network after first download.
use ;
let provider = new?;
let result = provider.embed?;
assert_eq!;
Qwen3 embedding (candle)
Pure Rust GPU/CPU inference via candle-nn — no ONNX Runtime.
use ;
let provider = new?;
let result = provider.embed?;
Nomic V2 MoE embedding (candle)
Lightweight MoE model — 8 experts, top-2 routing, 305M active params.
use ;
let provider = new?;
let result = provider.embed?;
assert_eq!;
Vector indexing
The VectorIndex trait is defined in llm-kernel (zero dependencies). For a concrete implementation with TurboQuant compression (up to 16x, SIMD search), see llm-kernel-vector-index.
use VectorIndex;
use TurbovecIndex;
let mut idx = new?;
idx.add?;
let hits = idx.search?;
use ;
// Mask secrets in logs
let safe = mask_secrets;
// → "Authorization: Bearer [REDACTED]"
// Classify errors
let category = classify_failure;
// → ErrorCategory::Timeout
// Sanitize untrusted output
let clean = sanitize_output?;
// detect_injection returns InjectionScore { score, signals } — a coarse lexical heuristic
let injection = detect_injection;
// injection.score is in [0.0, 1.0]; injection.signals lists the matched rule labels
Prompt templates
PromptTemplate substitutes {{variable}} placeholders and renders any few-shot examples before the body. It derives Serialize/Deserialize for config-driven prompts.
use PromptTemplate;
let tpl = new
.with_few_shot;
let prompt = tpl.render;
Model metadata
Each model in the catalog includes:
| Field | Description |
|---|---|
cost |
Per-million-token pricing (input, output, cache_read, cache_write) |
limit |
Context and output token limits |
modalities |
Input/output modalities (text, image, audio) |
capabilities |
Flags: attachment, reasoning, temperature, tool_call, streaming |
knowledge |
Training data cutoff date |
Why llm-kernel?
| llm-kernel | rig | langchain-rust | |
|---|---|---|---|
| Provider catalog | ✅ 16 providers, 114 models built-in | Manual config | Manual config |
| Feature gates | ✅ Independent modules | Monolithic | Monolithic |
| Local embedding | ✅ 44 ONNX + Qwen3 + Nomic MoE | ❌ | ❌ |
| Vector indexing | ✅ VectorIndex trait + separate crate | ❌ | ❌ |
| Quality eval | ✅ 5 modules, baseline regression, CI | ❌ | ❌ |
| MCP server | ✅ JSON-RPC 2.0 | ❌ | ❌ |
| Knowledge graph | ✅ SQLite + FTS5 + smart recall | ❌ | ❌ |
| Mandatory deps | serde only |
reqwest, tokio, … |
Many |
| Chains / agents | ❌ | ✅ | ✅ |
| RAG pipelines | ❌ | ✅ | ✅ |
llm-kernel is a lightweight foundation layer — compose it with rig or langchain-rust when you need chains, agents, or RAG.
Architecture
┌──────────────────────────────────────────┐
│ Your app │
├──────────────────────────────────────────┤
│ prelude │ ← use llm_kernel::prelude::*;
├───────────────┬──────────┬───────────────┤
│ provider │ client │ discovery │ ← catalog, async LLM, model discovery
│ catalog │ async │ │
├───────────────┴──────────┴───────────────┤
│ graph │ mcp │ embedding │ search │ ← graph, MCP, ONNX/Qwen3/Nomic embed, RRF
├──────────────────────────────────────────┤
│ tokens │ telemetry │ safety │ install │ ← token est., events, masking, wizard
├──────────────────────────────────────────┤
│ secrets │ config │ store │ ← vault, TOML, SQLite infra
└──────────────────────────────────────────┘
LLMClienttrait — unified interface forOpenAIClientandAnthropicClientEmbeddingProvidertrait — unified interface forFastembedProvider(ONNX),Qwen3Provider(candle),NomicMoeProvider(candle),OpenAIEmbeddingClient(remote)VectorIndextrait — unified interface for compressed vector indexes;TurbovecIndex(TurboQuant) implements 2-bit/4-bit quantized ANN search with SIMD kernelsProviderIndex— zero-copy access to embedded catalog, queryable by provider or modelMcpServer— JSON-RPC 2.0 server with stdio transport, Bearer auth, tool registrationSecretVault—HashMap<String, String>with dotenv load/save and symlink guardsgraph— SQLite knowledge graph with FTS5 search, composite scoring recall, BFS traversal, importance decayTelemetryEvent— enum-gated variants for structured observability (no PII)safety— secret masking, error classification, bidi/ANSI/null sanitization, prompt-injection detectionSearchProvider— unified sync interface for ranking backends;KeywordIndexreference impl plus RRF / weighted-sum / CombMNZ fusionPromptTemplate—{{variable}}substitution with few-shot examples and serde round-tripdetect_injection— coarse prompt-injection risk scoring over weighted regex signals
Quality evaluation
Built-in evaluation CLI measures module quality against curated test datasets:
# Run all evaluations (tokens, safety, embedding, search)
# Include graph evaluation
# Regression check against baseline snapshot (exit 1 on regression)
# JSON output for tooling
| Module | Metrics |
|---|---|
| tokens | MAE, max_error, %±3, %±10%, by-category breakdown |
| safety | exact_match_rate, precision, recall, F1, missed_secrets |
| embedding | identity_accuracy, orthogonality, symmetry, bounds |
| search | precision@5, recall@5, MRR |
| graph | precision, recall, F1 by query type |
Pass --baseline eval/baseline.json to compare against a golden snapshot — the CLI exits with code 1 on any metric regression. CI runs this automatically on every push and PR via the eval job.
Benchmarks
Criterion benchmarks under benches/:
Examples
# List all providers and models (no API key needed)
# OpenAI chat (requires OPENAI_API_KEY)
# Anthropic streaming (requires ANTHROPIC_API_KEY)
Requirements
- Rust 1.92+ (edition 2024)
Contributing
See CONTRIBUTING.md. PRs welcome.
License
Apache-2.0 © 2026 EpicCounty