gptme-rag
Local semantic search over your files for AI agents: index directories into ChromaDB with local
(sentence-transformers) or API embeddings, then query them from the CLI, from gptme's built-in
rag tool, or from any MCP client.
Status: beta. Upstreamed from the now-archived
gptme/gptme-rag and developed here. The gptme-rag package
on PyPI is an old release from that repo — install from this repository for current features.
Why / when to use it
Use gptme-rag when an agent needs to find relevant passages in a large body of local text (docs, notes, code, a workspace's knowledge base) by meaning rather than exact words. It is the vector-search complement to gptme-wisdom (BM25/SQLite, good for exact terms over reference books). For structural code navigation see gptme-codegraph.
How it fits with gptme:
- gptme core's
ragtool calls thegptme-ragCLI when it is onPATHand[rag] enabled = trueis set ingptme.toml(gptme docs). gptme-rag mcpserves the same index to Claude Code, Cursor, Codex, gptme or any MCP client.- gptme-retrieval is a gptme plugin for automatic context retrieval.
Install
# Lesson-matching library only (regex; no torch, ChromaDB or embeddings)
uv pip install "gptme-rag @ git+https://github.com/gptme/gptme-contrib#subdirectory=packages/gptme-rag"
# CLI / dense retrieval (local embeddings pull in sentence-transformers/torch)
uv tool install "gptme-rag[full] @ git+https://github.com/gptme/gptme-contrib#subdirectory=packages/gptme-rag"
# With the MCP server and lexical (TF-IDF) extras
uv tool install "gptme-rag[mcp,lexical] @ git+https://github.com/gptme/gptme-contrib#subdirectory=packages/gptme-rag"
Extras: full (CLI and dense retrieval), mcp (MCP server, includes full),
lexical (scikit-learn TF-IDF library plus tiktoken), test / dev (include full).
Existing plain installs used to include the dense stack: select full explicitly when
upgrading a CLI or Indexer consumer. The base installation supports the lesson-matching
Python API, not the gptme-rag console command. It uses the matcher's built-in YAML subset
parser when PyYAML is absent; install PyYAML for unrestricted YAML frontmatter. The shared
Claude Code hook also needs tomli on Python 3.10 to read configured lesson directories.
Quickstart
gptme-rag index ~/notes ~/projects/docs # build/update the index
gptme-rag search "how do we rotate credentials" # semantic search
gptme-rag status # what's in the default index
The default index lives in ~/.cache/gptme/rag; pass --persist-dir to index, search,
watch, clean, gc-orphans and mcp to use another one. Re-indexing is incremental (per-file
change detection).
Commands
| Command | Description |
|---|---|
index PATHS... [-p GLOB] |
Index files (default glob **/*.*). Options: --embedding-function, --device cpu|cuda, --chunk-size, --chunk-overlap, --file-limit (per directory, default 100000), --force-recreate |
search QUERY [PATHS...] |
Search and assemble context. Options: -n/--n-results, --max-tokens, --format summary|full, --expand none|adjacent|file, -f/--filter GLOB (repeatable), --score, --explain, --weights JSON, --json, --raw |
watch DIR |
Index DIR and keep the index updated as files change (--ignore-patterns) |
status |
Document/chunk counts and source breakdown for the default index |
clean [--force] |
Delete the index directory |
gc-orphans [--apply] |
List (or delete) leftover Chroma segment directories not referenced by the index |
mcp |
Run an MCP stdio server (needs the mcp extra) |
benchmark indexing|search-benchmark|watch-perf DIR |
Performance benchmarks |
Global flag: -v/--verbose.
Embeddings
--embedding-function accepts auto (default — reuse whatever model an existing collection was
built with), modernbert, minilm, mpnet, openrouter or default.
- Local (sentence-transformers, CPU by default). Embeddings are cached by chunk content hash
in
~/.cache/gptme-rag/local-embeddings.sqlite(respectsXDG_CACHE_HOME), so re-indexing a file where one line changed doesn't re-embed every chunk. SetGPTME_RAG_EMBEDDING_CACHE=/path/to/cache.sqliteto move it, or=offto disable. - OpenRouter API:
OPENROUTER_API_KEY=... gptme-rag index DIR --embedding-function openrouter. Model defaults toopenai/text-embedding-3-large; override withOPENROUTER_EMBEDDING_MODEL. Without an API key it falls back to the local ModernBERT backend.
Switching models on an existing index requires --force-recreate.
MCP server
gptme-rag mcp --persist-dir ~/.cache/gptme/rag
# e.g. register with Claude Code:
claude mcp add gptme-rag -- gptme-rag mcp
Tools: rag_query(query, top_k=5) (max 50), rag_index_status(),
rag_index_refresh(directory, pattern="**/*.*"). Each accepts an optional persist_dir.
Python API
Beyond the CLI, the package exposes building blocks for agents that assemble their own retrieval:
gptme_rag.Indexer,gptme_rag.ContextAssembler— the dense index and context assembly (lazy-loaded; need the full dependencies).gptme_rag.lexical.TfidfIndex— TF-IDF backend for exact-identifier queries (function names, paths, error strings) where embeddings are weak; needs thelexicalextra.gptme_rag.lesson_matcher—scan_lessons,score_lessons,filter_by_session_category: keyword/wildcard + BM25 lesson scoring, compatible with gptme's lesson format.gptme_rag.sources— declarative source registry for collecting a reproducible corpus.gptme_rag.knowledge_source.KnowledgeEntrySource— indexes the store written bygptme-util knowledge saveasmemory_type="knowledge_entry"documents. Both importtiktoken, so they need thefullorlexicalextra; a base install raisesModuleNotFoundError: No module named 'tiktoken'.gptme_rag.memory_type— caller-supplied rules for classifying documents (identity, project, preference, …) and boosting matching results.gptme_rag.observability— injection logging and index-health ("is my index stale?") checks.
Lesson match-term compatibility
Both top-level and nested match fields accept keyword/pattern lists or a single
string. A scalar keywords: "foo, bar" is shorthand for two keywords. A scalar
patterns value is one regex, including any commas: patterns: "x{1,3}y"
remains intact.
Migration: older versions split top-level scalar patterns on commas (and did
the same in the no-PyYAML fallback). Replace patterns: "foo, bar" with an explicit
list, patterns: ["foo", "bar"], to keep matching either expression independently.
Do not split commas that belong to a regex quantifier or literal. Lists work at
both frontmatter levels and with either parser backend.
Numeric and date match terms retain their source spelling (3.10, 2026-08-05).
With PyYAML, boolean/null values are excluded; quote literal terms such as "on",
"true", or "null". The lightweight fallback is a YAML subset, not a complete
YAML type resolver; quoting these terms makes intent explicit across backends.
Development
uv run pytest packages/gptme-rag/ -v -m "not slow" # skip tests that load embedding models
uv run pytest packages/gptme-rag/ -v # everything
License
MIT