LibreChat

repository·main·Indexed 12 days ago

https://github.com/danny-avila/librechat

A self-hosted, open-source AI chat platform that unifies major AI providers like OpenAI, Anthropic, and Google into a single, privacy-focused interface. It features AI Agents, Code Interpreter, Web Search, and enterprise-grade multi-user management. Version v0.8.7.

Tokens
65.8K
Snippets
180
Records
305
Agent score
99%

What's inside LibreChat

  1. Overview of LibreChat Features

    main

    LibreChat is a self-hosted, privacy-focused AI conversation platform that integrates multiple major AI providers into a single interface.

    Key capabilities include:

    • AI Model Support: Connect to Anthropic, OpenAI, Azure, Google (Vertex AI), AWS Bedrock, and custom endpoints compatible with the OpenAI specification. It also supports local/remote providers like Ollama, Groq, Mistral, and more.
    • Agents & Tools: Build no-code custom assistants, use an Agent Market, and leverage the Model Context Protocol (MCP) for tool calling. Features include Skills (reusable instruction sets) and Subagents (isolated task delegation).
    • Code Execution: A secure sandbox for running Python, Node.js, Go, C/C++, Java, PHP, Rust, and Fortran via the Code Interpreter API.
    • Advanced UI: Supports Generative UI with Code Artifacts (React, HTML, Mermaid), dynamic Reasoning UI for models like DeepSeek-R1, and Resumable Streams for connection reliability.
    • Multimodal & Media: Image generation (DALL-E, Stable Diffusion, Flux), image analysis, and voice/audio interaction (STT/TTS) via OpenAI, Azure, and Elevenlabs.
    • Enterprise & Management: Multi-user authentication (OAuth2, LDAP, Email), conversation search, message/dialogue forking, and import/export capabilities (Markdown, JSON, etc.).
  2. Mirror existing Tool Capability patterns

    main

    The Tool Intent feature is designed as a structural sibling to existing tool capabilities. When implementing, follow the pattern established by defer_loading, allowed_callers, and run_in_background.

    Capability Comparison

    Capabilitytool_options keyMechanism
    Deferred toolsdefer_loadingTool withheld from context, discovered via tool_search
    Programmatic (PTC)allowed_callersCallable only from run_tools_with_code / run_tools_with_bash
    Background toolsrun_in_backgroundInjected via schema; intercepted by handlers.ts
    Tool Intentsdescribe_intentInjected via schema; uses intent as the first argument
  3. Understand Tool Intent & Outcome Labels

    main

    Tool Intent & Outcome Labels is a capability that allows an AI model to declare its purpose for a specific tool call. Instead of showing generic, mechanical labels (e.g., Ran search_code in github), the UI displays a natural-language statement of what the model is attempting to do.

    Key Concepts

    • Intent: A live, per-call string provided by the model as the first argument of a tool call. It streams into the UI while the tool is running (e.g., "Searching for OAuth handling").
    • Outcome: A settled, per-call string that replaces the intent once the tool call completes (e.g., "Searched for OAuth handling"). This is authored by the tool/model and acts as an in-place edit of the intent.
    • Relationship to Activity Labels:
      • Intents are live, per-call, and authored by the calling model via tool arguments.
      • Activity Labels are settled, per-block, and authored by a fast-model to provide a narrative summary of a group of tool calls.
  4. Trace tool intents and outcomes in Langfuse

    main

    When using the SDK, tool intents and outcomes are integrated into Langfuse traces to create a narrative view of agent activity without needing to open individual span payloads.

    • Intent: Attached at the start of a tool span. It is treated as a model-authored argument and follows the args-tracing policy. If a tool's arguments are redacted for a specific agent, the intent is also redacted to prevent side-channel leaks.
    • Outcome: Patched into the span when the tool settles.
    • Tagging: Intent-annotated spans are tagged with ["librechat", "tool-intent"] for easy querying.
    • Trace Grouping: Label generations are traced through run.generateActivityLabel using the conversation as the Langfuse session, ensuring they group under the conversation (trace name LibreChat Activity Label) rather than appearing as orphans.
  5. Measure Constant History with the follow-up Turn Profile

    main
    Setting E2E_LATENCY_TURN=follow-up creates a specific testing scenario where one unmeasured seed exchange is performed before every sample. This allows you to measure the next request (the follow-up) while exercising conversation/history reads without allowing the history to grow indefinitely across samples, as the conversation is deleted after each measurement.
  6. Configure Langfuse Fanout Gateway

    main

    The Helm chart can optionally deploy a Langfuse fanout gateway with an internal OpenTelemetry Collector sidecar. This handles media fanout and proxies traces to both a central Langfuse project and tenant-specific projects.

    Key Configuration Options:

    • Enable/Disable: Controlled via the chart (disabled by default). When enabled, it sets LANGFUSE_FANOUT_ENABLED and LANGFUSE_FANOUT_COLLECTOR_URL.
    • Tenant Export: Set librechat.configEnv.LANGFUSE_FANOUT_TENANT_EXPORT_DISABLED=true to keep central traces flowing while disabling tenant-specific trace and score export.
    • Redis: The gateway uses Redis to store one-time media upload plans. Use langfuseFanout.redis.uri for an external service, or enable the bundled Redis via redis.enabled=true.
    • Scaling: Scale the gateway using langfuseFanout.replicaCount. Note that the chart does not create a Horizontal Pod Autoscaler (HPA) for the fanout component.
    • Metrics: The gateway exposes Prometheus metrics at /metrics. To secure this, use langfuseFanout.metrics.secret.name and .key to provide a bearer token.
  7. Understand chat_search_db Roles and Permissions

    main

    The chat_search_db uses a least-privilege model with three specific security roles created via search/init/chat-search-roles.sh. All roles have search_path set to chat_search, public to allow unqualified type and table references.

    Roles

    1. chat_search_owner: The migration owner. It owns the chat_search schema. Every table it creates automatically grants full DML and sequence usage to chat_search_writer via default privileges.
    2. chat_search_writer: The projection writer. Used by the projector, outbox consumer, and sweep processes.
    3. chat_search_reader: The request reader. This role is subject to Row Level Security (RLS). It has no default grants on new tables (like outbox or watermark), enforcing a deny-by-default policy.

    Security Implementation Details

    • Extensions: vector (for embeddings) and pg_trgm (for trigram search) are installed into the chat_search schema.
    • RLS: While the init script sets up the roles, Track 4 migrations are responsible for enabling RLS on specific tables (e.g., chat_search.documents) and creating the necessary policies (e.g., tenant_id/user_id predicates).
    • Default Privileges: ALTER DEFAULT PRIVILEGES is used so that chat_search_owner's new tables are automatically accessible to the writer, but the reader remains restricted until explicit GRANT SELECT commands are run during migration.
  8. How the MCP Authority Proof substrate works

    main

    The MCP (Model Context Protocol) authority proof is an additive, default-off substrate designed to ensure that tool calls, connections, and publications are backed by a verified, immutable configuration state.

    Core Workflow

    1. Resolver Initialization: A caller creates one MCPAuthorityProofResolver per immutable boot configuration.
    2. Proof Resolution: The resolver resolves selected servers into an "authority proof" envelope.
    3. Proof Carrying: This envelope is carried alongside the parsed configuration and schemas.
    4. Fence Verification: Integration "fences" (helpers) accept only the resolution envelope issued by the resolver. They verify the artifact revision and pass the exact envelope to the publication, binding, or execution callback.

    Key Constraints

    • Immutability: The resolver never freezes or mutates caller-owned config or schema objects.
    • No Detached Proofs: Fences do not accept detached proofs with arbitrary artifacts; they must use the envelope issued by the resolver.
    • No Side Effects: A current-authority assertion does not reload YAML, call the MCP registry, scan Redis, initialize servers, or perform network I/O. It operates within a fresh MongoDB transaction with majority read/write concerns.
  9. Compose Tool Intent with Activity Labels

    main

    Tool Intent and Activity Labels are complementary systems that operate on different layers:

    FeatureIntentActivity Label
    ScopePer-call, model-authoredPer-block, fast-model-authored
    LifecycleLive and freeSettled and paid
    Data Sourcetool_call.argsACTIVITY_LABEL content part
    UI RoleProvides human-readable context for specific tool callsProvides high-level grouping and summary of a sequence of calls

    Key Integration Rules:

    • Do not couple transports: Intents ride tool_call.args, while activity labels use SSE events (on_activity_label).
    • Enhancement: Activity labels benefit from intents. Instead of echoing raw JSON, an activity label can use the intent to read: "Searching for OAuth handling in the callback router".
    • Auto-collapse: The UI auto-collapse feature for tool groups should remain keyed only on the activity label. An intent-labeled group without an activity label should remain expanded.
  10. Understand FerretDB Multi-Tenancy Architecture

    main

    LibreChat uses FerretDB (PostgreSQL-backed) to achieve database-per-org data isolation.

    Key Architectural Details:

    • Logical Isolation: FerretDB does not create separate PostgreSQL schemas per MongoDB database. Instead, all data resides in a single documentdb_data PG schema.
    • Data Mapping: Each MongoDB collection is mapped to a documents_<id> and retry_<id> table pair in PostgreSQL. The catalog is managed in documentdb_api_catalog.collections and .collection_indexes.
    • Database Creation: Using mongoose.connection.useDb('org_X') creates a logical database within FerretDB's catalog.
    • Isolation Enforcement: Isolation is enforced at the FerretDB wire protocol layer, not at the PostgreSQL schema level.

    Important: Because isolation is logical, you cannot use standard pg_dump for backups/restores. You must use driver-level or FerretDB-compatible methods.

  11. Understand Amazon DocumentDB compatibility and limitations

    main

    LibreChat's compatibility with Amazon DocumentDB depends on the engine version and cluster type. Use the following matrix to determine your deployment capability:

    Capability3.64.05.08.0Elastic
    Plain update operators
    Unique indexes
    Partial unique indexes (e.g., OAuth IDs)
    Transactions
    TTL indexes

    Critical Warnings:

    • Elastic Clusters: These are unsupported. They lack unique indexes (required for email + tenantId), partial indexes, ACID transactions, and change streams.
    • DocumentDB 4.0: Works, but partial unique indexes (used for OAuth account uniqueness) will fail to build. LibreChat will log this failure loudly at startup, but uniqueness will not be enforced by the database.