Khoj

A self-hostable AI second brain that chats with your documents, searches the web, builds custom agents, and runs entirely on your own LLM.

35.5Kstars
2.3Kforks
GNU AGPLv3
Python

Khoj is an open-source personal AI assistant designed to extend human cognition by combining retrieval-augmented generation, semantic search, and agentic automation across both local documents and live web data. Built for researchers, writers, and knowledge workers, it lets you ingest PDFs, Markdown, Notion pages, Word documents, and org-mode files and then query them conversationally using any local or cloud LLM including Llama 3, Qwen, Mistral, GPT, Claude, and Gemini.

The platform goes beyond simple chat by enabling you to build custom AI agents with personalized knowledge bases, personas, and tool access. Agents can perform deep web research, run sandboxed code, schedule automated digests, and even control a desktop computer through its built-in computer-use capability. Khoj integrates naturally with the tools developers and researchers already use, offering plugins for Obsidian, Emacs, desktop apps, and WhatsApp.

Khoj is self-hostable via Docker Compose with a single-command setup that spins up the AI server, a PostgreSQL database with pgvector for semantic search, a SearXNG instance for private web search, and a sandboxed code execution environment. It scales from a lightweight single-user personal assistant running on a laptop to a multi-user enterprise deployment on cloud infrastructure, with a hosted cloud option available at app.khoj.dev for users who prefer not to self-host.

What You Get

  • Multi-source document RAG - Ingest and semantically search across PDFs, Markdown, Notion, Word documents, and org-mode files using pgvector embeddings for context-aware, cited answers.
  • Custom AI agents - Build agents with dedicated knowledge bases, custom personas, specific LLM models, and tool access to automate recurring research, writing, or data analysis tasks.
  • Integrated web search - Query the open web via self-hosted SearXNG or paid APIs (Serper, Exa, Firecrawl) and get answers that blend document knowledge with live search results.
  • Sandboxed code execution - Run Python code in an isolated Terrarium sandbox or E2B cloud environment, enabling data analysis, chart generation, and programmatic task automation directly in chat.
  • Computer-use operator - Control a virtual desktop to automate GUI tasks, fill forms, navigate websites, and perform actions that require a browser or desktop application.
  • Automated research digests - Schedule recurring AI-generated newsletters and smart notifications delivered to your inbox, summarizing topics you care about from your documents and the web.
  • Cross-platform clients - Access Khoj from a web browser, Obsidian plugin, Emacs package, Android or desktop app, or WhatsApp, with conversation history synced across all clients.
  • Local and cloud LLM flexibility - Connect to any OpenAI-compatible endpoint including local Ollama models, or use hosted APIs from OpenAI, Anthropic, Google, and others with per-user model selection.

Common Use Cases

  • Academic literature review - A PhD student ingests a library of research PDFs and uses Khoj to ask cross-cutting questions about methodology trends, get cited summaries, and surface contradictions across papers.
  • Personal knowledge base querying - A technical writer syncs their Obsidian vault to Khoj and queries years of notes conversationally, generating new drafts that draw on their own past writing.
  • Competitive intelligence automation - A product manager creates a Khoj agent that performs weekly web research on competitor announcements, summarizes findings, and emails a digest every Monday morning.
  • Private on-premises AI deployment - A data-sensitive organization deploys Khoj on an internal server with Ollama and local Llama 3 models so that no queries or documents ever leave the corporate network.
  • Multi-step research workflows - A journalist instructs Khoj to research a story topic across dozens of web sources, cross-reference with uploaded interview transcripts, and produce a structured draft outline.
  • Code-augmented data analysis - A data analyst uploads CSV exports and asks Khoj to run Python analysis in its sandbox, generate matplotlib charts, and explain statistical findings in plain language.

Under The Hood

Architecture Khoj combines a Django ORM layer with a FastAPI application server into a unified Python backend that shares a single PostgreSQL database, keeping deployment simple while creating some coupling between the web framework and the API layer. The system is organized into clearly scoped modules: routers handle HTTP endpoints, processors contain LLM conversation logic and tool orchestration, and database adapters abstract persistence. Agentic research loops use an iterative tool-dispatch pattern where a research director LLM selects from web search, document retrieval, code execution, computer-use, and MCP tool calls, accumulating results across iterations before synthesizing a final response. The frontend is a standalone Next.js application bundled into the same Docker image, with client state managed through React hooks and server-sent events used for streaming AI responses in real time.

Tech Stack The backend runs Python 3.10–3.12 under FastAPI and Django 5.x, with PostgreSQL and pgvector providing both relational persistence and vector similarity search for document embeddings. Sentence-transformers and Hugging Face Transformers handle local embedding generation, while PyTorch provides the underlying compute layer. The web frontend is built with Next.js, TypeScript, Tailwind CSS, Radix UI primitives, and Bun as the package manager. Docker Compose orchestrates the full stack including SearXNG for private web search, Terrarium for sandboxed Python execution, and an optional Khoj Computer container for desktop automation. The project integrates with every major LLM provider through the OpenAI-compatible API, Anthropic SDK, and Google GenAI SDK, and implements the Model Context Protocol for connecting external tool servers.

Code Quality Khoj maintains an extensive test suite organized into unit, integration, and end-to-end tests with pytest-django and pytest-asyncio, covering API endpoints, content processors, conversation flows, and multi-user scenarios. Ruff handles both linting and formatting across the Python codebase with import sorting enforced, while pre-commit hooks prevent regressions from reaching the repository. TypeScript is used throughout the Next.js frontend with proper component prop typing. Error handling is explicit in the API layer with structured HTTP responses, though the agentic pipeline relies primarily on LLM output validation rather than typed error classes. The migration history in Django is comprehensive with well over 70 migrations tracking schema evolution carefully.

What Makes It Unique Khoj’s most distinctive technical capability is its integrated computer-use operator, which lets the AI control a virtual desktop environment to perform tasks that require a real browser or GUI application rather than a web API. The unified retrieval pipeline harmonizes heterogeneous sources including GitHub repositories, Notion pages, Org-mode files, and PDFs into a single pgvector index, so a single query can draw on all of them simultaneously. The system uses Magika for content-aware file type detection rather than relying on file extensions, improving indexing reliability for files with missing or incorrect extensions. MCP client support lets Khoj connect to any MCP-compatible tool server as an external capability, making it extensible without modifying core code.

Self-Hosting

Khoj is released under the GNU Affero General Public License version 3 (AGPL-3.0). This is a strong copyleft license with a network use clause: if you run a modified version of Khoj as a network service and allow other users to interact with it, you must make the modified source code available to those users under the same license. For purely internal deployments where only employees of your own organization use the instance, the network clause is generally not triggered. Organizations that want to keep modifications proprietary or offer Khoj as an embedded component in a commercial product will need to contact the team for a commercial license arrangement.

Running Khoj yourself requires meaningful infrastructure: a PostgreSQL 15+ database with the pgvector extension, sufficient CPU or GPU resources for local embedding models, and optionally separate containers for SearxNG, Terrarium, and Khoj Computer. The provided Docker Compose file makes initial setup straightforward, but production operation demands you take responsibility for database backups, container orchestration, secret management, and zero-downtime upgrades across multiple services. Teams should expect to allocate DevOps time for monitoring, log aggregation, and scaling the embedding model inference if the user base grows significantly.

The managed cloud service at app.khoj.dev removes all of that operational overhead and adds a subscription tier that unlocks higher usage limits, priority support, and managed upgrades. Self-hosters miss out on the SLA guarantees, automatic model updates, and managed integration with third-party services like Stripe-based subscription billing and Twilio-based SMS notifications. The Khoj team makes the distinction clear in their documentation, positioning the cloud service for individuals and teams who want capability without infrastructure burden, while directing technically capable users toward the self-hosted path for maximum privacy and control.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack

No spam. Unsubscribe anytime.

Join 750+ subscribers
No spam. Unsubscribe anytime.

Search