System Overview
Khoj is a full-stack AI application with a Python backend, Next.js frontend, and multiple client interfaces. It uses PostgreSQL with pgvector for semantic search and supports various AI models through a plugin architecture.High-Level Architecture
Client Layer
Multiple client interfaces communicate with the Khoj server:
- Web App (Next.js)
- Desktop App (Electron)
- Obsidian Plugin
- Emacs Package
- Android App
- API Clients
API Layer
FastAPI-based REST API handles:
- Authentication & Authorization
- Chat & Search Endpoints
- Content Indexing
- Agent Management
- Automation Triggers
Processing Layer
Core business logic for:
- Document Processing
- Embedding Generation
- Conversation Management
- Tool Execution
- Research Mode
Storage Layer
- PostgreSQL with pgvector for embeddings
- Django ORM for database management
- File system for static assets
Backend Architecture
Directory Structure
Core Components
main.py - Application Bootstrap
main.py - Application Bootstrap
The entry point that:
- Initializes Django
- Sets up FastAPI application
- Configures CORS middleware
- Runs database migrations
- Collects static files
- Starts the uvicorn server
routers/ - API Endpoints
routers/ - API Endpoints
FastAPI routers organized by domain:
- api.py: Core API (health, config, sync)
- api_chat.py: Chat & conversation (70KB+, main chat logic)
- api_agents.py: Agent creation and management
- api_content.py: Content upload and indexing
- api_automation.py: Scheduled automations
- api_subscription.py: User subscriptions
- auth.py: OAuth, login, registration
- helpers.py: Shared router utilities (130KB+)
processor/ - Business Logic
processor/ - Business Logic
Contains core processing modules:Content Processors (
processor/content/):- PDF, Markdown, Org-mode, Word, Notion
- Image, plaintext, Github, Docx
processor/conversation/):- LLM integrations (OpenAI, Anthropic, Google, Offline)
- Prompt management
- Context building
- Tool calling
processor/tools/):- Web search
- Code execution
- Document retrieval
- Custom agent tools
processor/embeddings.py):- Text embedding generation
- Batch processing
- Model management
database/ - Data Layer
database/ - Data Layer
Django-based database management:Models (
database/models/):- User, Subscription
- Conversation, ChatModel
- Agent, Entry
- SearchModel, Automation
- ProcessLock
database/adapters/):- High-level database operations
- Business logic for data access
- Caching and optimization
database/migrations/):- Schema version control
- Database evolution
utils/ - Utilities
utils/ - Utilities
Shared utility modules:
- helpers.py: Common helper functions (47KB)
- initialization.py: App startup logic
- state.py: Application state management
- cli.py: Command-line interface
- constants.py: Application constants
Frontend Architecture
Web Application
Key Frontend Technologies
Next.js 14
- App Router for file-based routing
- Server-side rendering (SSR)
- API routes for backend proxying
- Static export for production
TypeScript
- Type-safe React components
- API client with full typing
- Enhanced IDE support
Tailwind CSS
- Utility-first styling
- Responsive design
- Custom design system
Radix UI
- Accessible primitives
- Dialog, Dropdown, Tooltip
- Form components
State Management
- SWR: Data fetching and caching
- React Context: Global state (user, settings)
- URL State: Search params for routing
- WebSockets: Real-time chat streaming
Client Architecture
- Desktop App
- Obsidian Plugin
- Emacs Package
- Android App
Technology: Electron + ReactLocation:
src/interface/desktop/Features:- Tray icon integration
- System notifications
- Auto-start on login
- Local file indexing
- Embedded web view
Data Flow
Search & Retrieval
Chat Conversation
Content Indexing
Database Schema
Key Models
User & Authentication
User & Authentication
Conversation
Conversation
Entry (Indexed Content)
Entry (Indexed Content)
Agent
Agent
Testing Architecture
Test Organization
Testing Stack
pytest
- Unit and integration tests
- Fixtures for database setup
- Parallel test execution
pytest-django
- Django test database
- Model factories
- Transaction management
pytest-asyncio
- Async endpoint testing
- WebSocket tests
factory-boy
- Test data generation
- Model factories
CI/CD Pipeline
GitHub Actions Workflows
- test.yml
- pre-commit.yml
- dockerize.yml
- pypi.yml
- desktop.yml
Runs on every PR and push to master:
- Matrix testing on Python 3.10, 3.11, 3.12
- PostgreSQL service with pgvector
- Full test suite execution
- Type checking with mypy
Performance Considerations
Embedding Caching
Embeddings cached to avoid recomputation. Incremental updates only process changed content.
Connection Pooling
PostgreSQL connection pooling via Django to handle concurrent requests efficiently.
Async Processing
FastAPI async endpoints allow non-blocking I/O for external API calls.
Vector Indexing
pgvector indexes optimize similarity search performance for large datasets.
Security Architecture
Extension Points
Adding New Content Types
- Create processor in
processor/content/ - Implement
ContentProcessorinterface - Register in content type registry
- Add tests in
tests/
Adding New LLM Providers
- Create provider class in
processor/conversation/ - Implement
LLMProviderinterface - Add configuration in settings
- Update model selection UI
Adding New Tools
- Create tool in
processor/tools/ - Define tool schema
- Register in agent toolset
- Add to tool selection UI
Visual References
Codebase Visualization
Interactive visualization of the entire codebase structure
Architecture Diagram
Refer to “ for the complete system diagram
Additional Resources
Development Setup
Set up your local environment
Performance Guide
Optimization best practices
API Documentation
Complete API reference
GitHub Repository
View the source code
