Effortlessly turn codebase into context for your LLMs.
lmfetch is a lightning-fast CLI tool that fetches, chunks, and ranks code context from local files and remote GitHub repositories. It's designed to fit the most relevant code into your token budget, so your LLM can understand the implementation details without the noise.
- Lightning Fast
- Keyword matching with stemming, optional semantic embeddings, and dependency analysis
- Use as CLI tool or JavaScript/TypeScript library
- Works with local directories and GitHub repositories
curl -fsSL https://lmfetch.ai-query.dev/install.sh | bash# Install globally from npm
npm install -g lmfetch
# Or with bun
bun install -g lmfetch# Fetch local code
lmfetch . "how does authentication work"
# Fetch from GitHub
lmfetch https://github.com/vercel/ai "explain tool calling"
# Set token budget
lmfetch . "query" -b 100k
# Output context to file
lmfetch . "database models" -o context.md
# Context only (no LLM query)
lmfetch . "API routes" -c
# Enable semantic (embedding) ranking
lmfetch . "query" -s
# Clear cache
lmfetch --clean-cachenpm install lmfetch
# or
bun add lmfetchimport { ContextBuilder, query, fetchContext } from "lmfetch";
// Quick query with LLM
const answer = await query(".", "how does authentication work", {
model: "gemini-2.0-flash",
budget: "100k",
});
console.log(answer);
// Fetch context only
const context = await fetchContext(".", "database models", {
budget: "50k",
semantic: false, // Use keyword-only ranking (default)
});
console.log(context);
// Advanced usage with ContextBuilder
const builder = new ContextBuilder({
path: ".",
query: "API implementation",
budget: "100k",
fast: true, // Keyword-only ranking
onProgress: (msg) => console.log(msg),
});
const result = await builder.build();
console.log(`Context: ${result.context}`);
console.log(`Tokens: ${result.tokens}`);
console.log(`Files processed: ${result.filesProcessed}`);-b, --budget <budget> Token budget (e.g., 50k, 100k, 1m) (default: "50k")
-o, --output <file> Write context to file instead of stdout
-c, --context Output context only, skip LLM query
-i, --include <patterns...> Include patterns (glob)
-e, --exclude <patterns...> Exclude patterns (glob)
-m, --model <model> LLM model for answering (default: "gemini-2.0-flash")
-s, --semantic Use semantic (embedding) ranking (slower, requires API key)
--clean-cache Clear the internal cache
--force-large Process files larger than 1MB or 20k lines
Understands AST (classes, functions) for Python, TypeScript, Go, Rust, and more. Falls back to intelligent heuristics for other languages.
Default: Keyword Ranking (Fast)
- Uses intelligent keyword matching with stemming
- Filters stopwords and boosts important terms (e.g.,
.methodName) - Works offline, no API key needed for ranking
- Penalizes test files, fixtures, and codemods
- Recommended for most queries
Semantic Ranking (-s)
- Uses embedding similarity with HyDE (Hypothetical Document Embeddings)
- Requires
GOOGLE_GENERATIVE_AI_API_KEYfor embeddings - Slower but may be more accurate for vague queries
- Combines keyword + embedding + importance signals
GOOGLE_GENERATIVE_AI_API_KEY- For Google Gemini models and semantic rankingOPENAI_API_KEY- For OpenAI models (optional)ANTHROPIC_API_KEY- For Claude models (optional)
lmfetch-bun/src/
├── cli.ts # CLI interface (Commander)
├── builder.ts # Main orchestrator
├── cache.ts # SQLite caching (bun:sqlite)
├── tokens.ts # Token counting (js-tiktoken)
├── utils.ts # Utilities
├── sources/ # Data source adapters
│ ├── codebase.ts # Local directory scanner
│ └── github.ts # GitHub repository cloner
├── chunkers/ # Code splitting
│ └── code.ts # AST-heuristic regex chunker
├── rankers/ # Relevance scoring
│ ├── keyword.ts # BM25-like keyword matching
│ ├── embedding.ts # Semantic vector similarity
│ └── hybrid.ts # Combined ranker with HyDE
├── analyzers/ # Code analysis
│ ├── dependency.ts # Import/dependency graph
│ ├── importance.ts # File importance heuristics
│ └── llm.ts # LLM-powered reranking
└── llm/ # LLM integration
└── client.ts # Unified client (AI SDK)
See ARCHITECTURE.md for a deep dive into the pipeline.
cd lmfetch-bun
# Install dependencies
bun install
# Run in development
bun run dev <path> <query>
# Type check
bun run tsc --noEmit
# Build binary
bun run build
# Build for all platforms
bun run build:all# Install with uv
uv sync --all-extras --dev
# Run
uv run lmfetch . "query"
# Build
uv buildcd lmfetch-bun
# Build all platforms
bun run build:all
# Individual platforms
bun run build:linux-x64
bun run build:linux-arm64
bun run build:darwin-x64
bun run build:darwin-arm64
bun run build:windows-x64Binaries are created in lmfetch-bun/dist/.
This repository contains two implementations:
-
Bun.js (
lmfetch-bun/) - Recommended for best performance- ~0.25s startup time
- Available on npm:
npm install -g lmfetch - Can be used as a JavaScript/TypeScript library
-
Python (
lmfetch/) - Original implementation- ~30s startup time on macOS
- Available via uv, or pip (uv run lmfetch)
- Maintained for compatibility
MIT - see LICENSE for details.
Built with: