Overview
LibreChat is a full-stack application built with a modern monorepo architecture. It consists of a Node.js/Express backend, a React frontend, and shared TypeScript packages.Architecture Layers
Frontend Layer
The frontend is a single-page application (SPA) built with React and TypeScript.Frontend Stack
Frontend Stack
- Framework: React 18.2.0
- Build Tool: Vite (with HMR)
- Styling: Tailwind CSS
- State Management:
- React Query (@tanstack/react-query) for server state
- Recoil for client state
- Jotai for atomic state
- Routing: React Router v6
- UI Components: Radix UI primitives
- Form Handling: React Hook Form
- Internationalization: i18next + react-i18next
Frontend Architecture
Frontend Architecture
Location:
/clientKey Directories:src/components/- React components organized by featuresrc/data-provider/- React Query hooks and data fetchingsrc/hooks/- Custom React hookssrc/store/- Global state management (Recoil)src/routes/- Application routingsrc/locales/- Translation files (43 languages)src/utils/- Utility functions
http://localhost:3090/packages/client
packages/client
Location:
/packages/clientPurpose: Shared frontend utilities and components- Reusable UI components
- Theme configuration
- Common hooks
- Shared TypeScript types
packages/data-providerShared Layer
The shared layer contains code used by both frontend and backend.packages/data-provider
packages/data-provider
Location:
/packages/data-providerPurpose: Central source of truth for API contracts and data structuresContains:- API Endpoints (
src/api-endpoints.ts): URL definitions and endpoint paths - Data Service (
src/data-service.ts): HTTP client wrapper for API calls - Types (
src/types/): Shared TypeScript interfaces and types - Query Keys (
src/keys.ts): React Query cache keys for consistent data management - Mutation Keys (
src/keys.ts): React Query mutation identifiers
npm run build:data-providerBackend Layer
The backend uses a dual architecture: legacy JavaScript code in/api with new TypeScript code in /packages/api.
api (Legacy Express Server)
api (Legacy Express Server)
Location:
/apiLanguage: JavaScript (legacy)Purpose: Main Express server - minimize changes hereKey Directories:server/- Express server setup and entry pointserver/routes/- API route definitionsserver/controllers/- Request handlers (thin wrappers)server/middleware/- Express middleware (auth, validation, etc.)server/services/- Business logic (being migrated to packages/api)models/- Mongoose model definitions (being migrated)strategies/- Passport authentication strategiesdb/- Database connection and utilities
packages/apipackages/data-schemaspackages/data-provider@librechat/agents
http://localhost:3080/This is a legacy codebase. All new backend code must be TypeScript in
/packages/api.packages/api (New TypeScript Backend)
packages/api (New TypeScript Backend)
Location:
/packages/apiLanguage: TypeScript (strict mode)Purpose: New backend code lives here - all new features must be implemented in TypeScriptArchitecture:- Model Context Protocol (MCP) services
- Cache management and Redis integration
- Stream handling and resumable streams
- File processing (PDF, DOCX, XLSX, PPTX)
- Agent orchestration and tool execution
- Advanced business logic
- Built with Rollup for optimal bundling
- Full TypeScript coverage
- Integration with
@librechat/agents - Peer dependency architecture
packages/data-schemaspackages/data-provider
packages/data-schemas
packages/data-schemas
Location:
/packages/data-schemasLanguage: TypeScriptPurpose: Database models and schemas shareable across backend projectsContains:- Mongoose schema definitions
- Database model types
- Validation schemas (Zod)
- Migration utilities
packages/data-providerBuild Command: npm run build:data-schemasExternal Dependencies
@librechat/agents
@librechat/agents
Purpose: Major backend dependency for AI agent orchestrationMaintained by: Same team (separate repository)Source code:
/home/danny/agentus (in development environment)Features:- LangChain integration
- Tool execution framework
- Agent state management
- Context protocol support
MongoDB
MongoDB
Purpose: Primary database for persistent storageCollections:
- Users and authentication
- Conversations and messages
- Files and uploads
- Agents and tools
- Presets and prompts
- API keys and tokens
MONGO_MAX_POOL_SIZEMONGO_MIN_POOL_SIZEMONGO_MAX_CONNECTINGMONGO_MAX_IDLE_TIME_MS
Redis (Optional)
Redis (Optional)
Purpose: Caching and session managementUse Cases:
- Session storage
- Rate limiting
- Resumable streams (multi-tab/multi-device sync)
- Cache invalidation
@keyv/redisconnect-redisrate-limit-redis
Data Flow
Client to Server Request Flow
Streaming Response Flow
For AI chat responses, LibreChat uses Server-Sent Events (SSE):LibreChat’s resumable streams feature allows responses to automatically reconnect and resume if the connection drops, and works across multiple tabs and devices.
Build System
Turborepo
LibreChat uses Turborepo for efficient monorepo builds:turbo.json
- Parallel builds across workspaces
- Intelligent caching (only rebuilds changed packages)
- Dependency-aware execution
- Remote caching support
Build Tools by Package
| Package | Build Tool | Output |
|---|---|---|
client | Vite | dist/ (static files) |
packages/api | Rollup | dist/ (CommonJS + ES modules) |
packages/data-provider | Rollup | dist/ (CommonJS + ES modules) |
packages/data-schemas | Rollup | dist/ (CommonJS + types) |
packages/client | Rollup | dist/ (ES modules) |
Performance Optimizations
Frontend
- React Query caching and background refetching
- Cursor pagination for large datasets
- Code splitting and lazy loading
- Virtual scrolling (react-virtualized)
- Image optimization (sharp)
- PWA support with offline caching
Backend
- Connection pooling (MongoDB, Redis)
- Rate limiting (express-rate-limit)
- Response compression (gzip)
- Static file serving optimization
- Stream processing for large files
- Horizontal scaling support
Security Architecture
Authentication
Authentication
Strategies Supported:
- Local (username/password with bcrypt)
- OAuth2 (Google, GitHub, Discord, Facebook, Apple)
- LDAP
- SAML 2.0
- JWT tokens
/api/strategiesAuthorization
Authorization
Security Measures
Security Measures
- MongoDB injection prevention (express-mongo-sanitize)
- Rate limiting per IP/user
- CORS configuration
- Helmet.js security headers
- Session management (express-session)
- Proxy trust configuration
- Input validation (Zod schemas)
Next Steps
Monorepo Guide
Explore the workspace structure and dependency relationships
Frontend Development
Learn about React components and state management
Backend Development
Understand backend routes and API patterns
Testing
Write tests for LibreChat components