> ## Documentation Index
> Fetch the complete documentation index at: https://chatjs.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Architecture

> How ChatJS is structured and how data flows through the system

## System Overview

```mermaid theme={null}
flowchart LR
    Client[Browser] --> App[Next.js]
    App --> DB[(PostgreSQL)]
    App --> Cache[(Redis)]
    App --> AI[AI Providers]
    App --> Storage[Blob Storage]
    App --> Observability[Langfuse]
```

The application uses:

* **Next.js App Router** for the frontend and API routes
* **PostgreSQL** (via [Drizzle ORM](https://orm.drizzle.team)) for persistent data (users, chats, messages, documents)
* **Redis** for ephemeral data (resumable streams, caching). See [Resumable Streams](/cookbook/resumable-streams)
* **AI SDK** to connect to multiple AI providers through a [gateway abstraction](/gateways/overview)
* **Blob Storage** ([Vercel Blob](https://vercel.com/storage/blob)) for file attachments and generated images
* **tRPC** for end-to-end type-safe API routes between client and server
* **Langfuse** (optional) for LLM observability, tracing, and analytics

## Chat Message Flow

When a user sends a message:

```mermaid theme={null}
sequenceDiagram
    Client->>API: Send message
    API->>DB: Save messages
    API->>AI: Stream response
    AI-->>Client: Token stream (SSE)
    Note over API,Cache: Chunks stored in Redis for resumability
    API->>DB: Save final response
```

Messages are stored with normalized **parts** (text, tool calls, files, reasoning) allowing efficient querying and streaming updates.

If [resumable streams](/cookbook/resumable-streams) are enabled, response chunks are published to Redis so clients can reconnect mid-generation without losing progress.

## Configuration

All settings flow from a single source:

1. `chat.config.ts` - Your configuration
2. `lib/config/` - Parse and apply defaults
3. `lib/env.ts` - Validate environment variables
4. Runtime - Features enabled/disabled

This ensures type-safe configuration and build-time validation of environment variables. See [Configuration](/core/configuration) for the full reference.
