# dhananjaylab/memegpt architecture

> Architecture diagram of the GitHub repository dhananjaylab/memegpt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dhananjaylab/memegpt
- Repository: https://github.com/dhananjaylab/memegpt

## Overview

MemeGPT is split into two primary applications plus background processing: a FastAPI backend, a React/Vite frontend, and an ARQ worker. The backend is the system boundary that owns auth, generation, persistence, storage, rate limiting, and integrations; the frontend is a thin SPA that drives user workflows and renders previews/editors.

The frontend is a TypeScript React app organized by page and component boundaries. Pages such as Home, Gallery, Dashboard, and Synthesize compose reusable UI pieces like the meme editor, template selector, preview, trending topics, share menu, and protected routes. The frontend talks to the backend through a single API client layer in `src/lib/api.ts`, and uses an auth context to manage logged-in state and gated routes.

The backend entrypoint is `main.py`, with shared setup in `core/` for config, CORS, and middleware. Domain functionality is split into routers: `auth`, `users`, `memes`, `ai`, `jobs`, `storage`, `trending`, `stripe`, and `health`. This makes the backend’s external API surface map closely to product features rather than infrastructure concerns.

Data persistence is PostgreSQL-backed through SQLAlchemy models and Alembic migrations. The migrations show that the schema covers core application entities plus evolving features like API key hashing, trending scores, template support changes, and contract additions. This is the durable source of truth for users, templates, jobs, generated memes, and subscription-related state.

The main generation pipeline is event-driven. A “quick generate” request first checks caches, then either returns immediately on a hit or offloads the expensive work to the ARQ worker on a miss. The API responds with a job identifier, and the client listens to a Server-Sent Events job stream for progress and completion. This is the key throughput boundary between request/response traffic and heavier AI/image work.

The worker process in `backend/workers/meme_worker.py` is the async execution boundary. It handles the expensive parts of generation such as AI captioning, image composition, and storage upload. Supporting services like `meme_ai`, `compositor`, `worker`, and `storage` keep that pipeline isolated from the web request path.

Redis is used for two distinct concerns: caching/queueing and rate limiting. The README describes a cache-first flow for fast responses and a sliding-window limiter with tiered quotas. That means Redis sits on the critical path for both performance and abuse prevention, not just background jobs.

Storage is abstracted behind a service layer with Cloudflare R2 as the primary object store and the local filesystem as a fallback. The `backend/public/frames` and `backend/public/fonts` assets support local template rendering, while `public/meme_data.json` and the template loader/seed scripts suggest a hybrid of static template definitions and database-backed catalog data. Cleanup scripts exist for maintaining fallback storage and migrating content to R2.

Authentication and API key handling are security-sensitive subsystems. The backend includes dedicated auth and API key services, and the README calls out a “store-hash, show-once” policy for API keys. Stripe integration sits alongside this to support subscription tiers that influence access and rate limits.

Template management is its own domain. The `load_templates.py` and `seed_templates.py` scripts, together with the `template_catalog` service and meme routers, indicate a pipeline for loading curated meme templates into the database, serving them to the UI, and keeping local fallback assets available when needed. Trending is also a separate concern, exposed through its own router and service.

Operationally, the repo is container-first. There is a backend Dockerfile, environment variants for local/staging/production, and scripts for deployment and storage cleanup. The architecture assumes multiple cooperating services in deployment: frontend, FastAPI backend, Redis, PostgreSQL, and the ARQ worker, with external dependencies such as Google Gemini, Cloudflare R2, and Stripe plugged in through service adapters.

Main entry points: [`backend/routers`](https://github.com/dhananjaylab/memegpt/tree/main/backend/routers) (API routers), [`backend/services`](https://github.com/dhananjaylab/memegpt/tree/main/backend/services) (Services), [`frontend/src/lib/api.ts`](https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/lib/api.ts) (API client), [`frontend/src/App.tsx`](https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/App.tsx) (SPA)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["SPA<br/>React/Vite app<br/>[App.tsx]"]
  node_frontend_pages["Pages<br/>page layer"]
  node_frontend_components["UI pieces<br/>component library"]
  node_frontend_auth(("Auth state<br/>context<br/>[AuthContext.tsx]"))
  node_frontend_api["API client<br/>[api.ts]"]
  node_frontend_canvas["Canvas<br/>render utils<br/>[canvas.ts]"]
end

subgraph group_backend["Backend"]
  node_backend_main["FastAPI app<br/>app entrypoint<br/>[main.py]"]
  node_backend_core["Core setup<br/>config/middleware"]
  node_backend_routers["API routers<br/>HTTP surface"]
  node_backend_services["Services"]
  node_backend_models[("Models<br/>[models.py]")]
  node_backend_migrations["Migrations<br/>Alembic history"]
  node_backend_session[("DB session<br/>database session<br/>[session.py]")]
  node_backend_worker["ARQ worker<br/>async worker<br/>[meme_worker.py]"]
  node_backend_templates["Templates<br/>catalog pipeline"]
  node_backend_storage[("Storage layer<br/>storage service<br/>[storage.py]")]
  node_backend_cache[("Redis uses<br/>cache/queue/rate limit<br/>[cache.py]")]
  node_backend_trending["Trending<br/>ranking service<br/>[trending.py]"]
  node_backend_auth["Auth & keys<br/>security service<br/>[auth.py]"]
  node_backend_integrations["Integrations<br/>external adapters<br/>[meme_ai.py]"]
  node_backend_public["Public assets<br/>static assets<br/>[meme_data.json]"]
end

subgraph group_ops["Ops"]
  node_ops_scripts["Scripts<br/>ops tooling"]
end

subgraph group_external["External Systems"]
  node_ext_postgres[("PostgreSQL<br/>database")]
  node_ext_redis[("Redis<br/>cache/queue")]
  node_ext_r2[("Cloudflare R2<br/>object storage")]
  node_ext_gemini{{"Gemini<br/>AI provider"}}
  node_ext_stripe{{"Stripe<br/>billing"}}
end

node_frontend_app -->|"composes"| node_frontend_pages
node_frontend_app -->|"uses"| node_frontend_components
node_frontend_app -->|"reads auth"| node_frontend_auth
node_frontend_pages -->|"calls"| node_frontend_api
node_frontend_components -->|"calls"| node_frontend_api
node_frontend_components -->|"renders"| node_frontend_canvas
node_frontend_auth -->|"authenticates"| node_frontend_api
node_frontend_api -->|"HTTP"| node_backend_main
node_backend_main -->|"boots"| node_backend_core
node_backend_main -->|"mounts"| node_backend_routers
node_backend_routers -->|"auth"| node_backend_auth
node_backend_routers -->|"templates"| node_backend_templates
node_backend_routers -->|"trending"| node_backend_trending
node_backend_routers -->|"storage"| node_backend_storage
node_backend_routers -->|"billing/ai"| node_backend_integrations
node_backend_routers -->|"jobs/rate"| node_backend_cache
node_backend_services -->|"uses"| node_ext_redis
node_backend_services -->|"uploads"| node_ext_r2
node_backend_services -->|"calls"| node_ext_gemini
node_backend_services -->|"calls"| node_ext_stripe
node_backend_services -->|"queues jobs"| node_backend_worker
node_backend_worker -->|"uses"| node_backend_services
node_backend_worker -->|"pulls jobs"| node_backend_cache
node_backend_models -->|"persists"| node_ext_postgres
node_backend_session -->|"connects"| node_ext_postgres
node_backend_migrations -->|"evolves"| node_ext_postgres
node_backend_templates -->|"loads"| node_backend_public
node_backend_templates -->|"stores"| node_backend_models
node_backend_storage -->|"falls back"| node_backend_public
node_ops_scripts -->|"maintains"| node_backend_storage

click node_frontend_app "https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/App.tsx"
click node_frontend_pages "https://github.com/dhananjaylab/memegpt/tree/main/frontend/src/pages"
click node_frontend_components "https://github.com/dhananjaylab/memegpt/tree/main/frontend/src/components"
click node_frontend_auth "https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/context/AuthContext.tsx"
click node_frontend_api "https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/lib/api.ts"
click node_frontend_canvas "https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/lib/canvas.ts"
click node_backend_main "https://github.com/dhananjaylab/memegpt/blob/main/backend/main.py"
click node_backend_core "https://github.com/dhananjaylab/memegpt/tree/main/backend/core"
click node_backend_routers "https://github.com/dhananjaylab/memegpt/tree/main/backend/routers"
click node_backend_services "https://github.com/dhananjaylab/memegpt/tree/main/backend/services"
click node_backend_models "https://github.com/dhananjaylab/memegpt/blob/main/backend/models/models.py"
click node_backend_migrations "https://github.com/dhananjaylab/memegpt/tree/main/backend/db/migrations/versions"
click node_backend_session "https://github.com/dhananjaylab/memegpt/blob/main/backend/db/session.py"
click node_backend_worker "https://github.com/dhananjaylab/memegpt/blob/main/backend/workers/meme_worker.py"
click node_backend_templates "https://github.com/dhananjaylab/memegpt/blob/main/backend/services/template_catalog.py"
click node_backend_storage "https://github.com/dhananjaylab/memegpt/blob/main/backend/services/storage.py"
click node_backend_cache "https://github.com/dhananjaylab/memegpt/blob/main/backend/services/cache.py"
click node_backend_trending "https://github.com/dhananjaylab/memegpt/blob/main/backend/services/trending.py"
click node_backend_auth "https://github.com/dhananjaylab/memegpt/blob/main/backend/services/auth.py"
click node_backend_integrations "https://github.com/dhananjaylab/memegpt/blob/main/backend/services/meme_ai.py"
click node_backend_public "https://github.com/dhananjaylab/memegpt/blob/main/backend/public/meme_data.json"
click node_ops_scripts "https://github.com/dhananjaylab/memegpt/tree/main/scripts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_pages,node_frontend_components,node_frontend_auth,node_frontend_api,node_frontend_canvas toneBlue
class node_backend_main,node_backend_core,node_backend_routers,node_backend_services,node_backend_models,node_backend_migrations,node_backend_session,node_backend_worker,node_backend_templates,node_backend_storage,node_backend_cache,node_backend_trending,node_backend_auth,node_backend_integrations,node_backend_public toneAmber
class node_ops_scripts toneMint
class node_ext_postgres,node_ext_redis,node_ext_r2,node_ext_gemini,node_ext_stripe toneRose
```

## Components

### Frontend

React/Vite SPA for meme creation, browsing, and authenticated user flows.

- [SPA](https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/App.tsx): React/Vite app · Single-page application entry and routing shell. · `frontend/src/App.tsx`
- [Pages](https://github.com/dhananjaylab/memegpt/tree/main/frontend/src/pages): page layer · Home, Gallery, Dashboard, and Synthesize screens. · `frontend/src/pages`
- [UI pieces](https://github.com/dhananjaylab/memegpt/tree/main/frontend/src/components): component library · Reusable editors, previews, selectors, sharing, and guarded UI. · `frontend/src/components`
- [Auth state](https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/context/AuthContext.tsx): context · Logged-in state and route gating. · `frontend/src/context/AuthContext.tsx`
- [API client](https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/lib/api.ts): client · Single backend API access layer used by the SPA. · `frontend/src/lib/api.ts`
- [Canvas](https://github.com/dhananjaylab/memegpt/blob/main/frontend/src/lib/canvas.ts): render utils · Client-side meme rendering helpers. · `frontend/src/lib/canvas.ts`

### Backend

FastAPI application plus data, services, and worker execution.

- [FastAPI app](https://github.com/dhananjaylab/memegpt/blob/main/backend/main.py): app entrypoint · Backend startup, routing, and application wiring. · `backend/main.py`
- [Core setup](https://github.com/dhananjaylab/memegpt/tree/main/backend/core): config/middleware · Shared configuration, CORS, and middleware. · `backend/core`
- [API routers](https://github.com/dhananjaylab/memegpt/tree/main/backend/routers): HTTP surface · Feature-oriented REST/API endpoints. · `backend/routers`
- [Services](https://github.com/dhananjaylab/memegpt/tree/main/backend/services): domain services · Auth, caching, rate limiting, storage, generation, and trends. · `backend/services`
- [Models](https://github.com/dhananjaylab/memegpt/blob/main/backend/models/models.py): SQLAlchemy models · Persistent application entities and schemas. · `backend/models/models.py`
- [Migrations](https://github.com/dhananjaylab/memegpt/tree/main/backend/db/migrations/versions): Alembic history · Schema evolution for app entities and feature changes. · `backend/db/migrations/versions`
- [DB session](https://github.com/dhananjaylab/memegpt/blob/main/backend/db/session.py): database session · Database connection/session setup. · `backend/db/session.py`
- [ARQ worker](https://github.com/dhananjaylab/memegpt/blob/main/backend/workers/meme_worker.py): async worker · Background execution boundary for expensive generation jobs. · `backend/workers/meme_worker.py`
- [Templates](https://github.com/dhananjaylab/memegpt/blob/main/backend/services/template_catalog.py): catalog pipeline · Template loading, seeding, and catalog management. · `backend/services/template_catalog.py`
- [Storage layer](https://github.com/dhananjaylab/memegpt/blob/main/backend/services/storage.py): storage service · Object storage abstraction, R2 config, local fallback, and cleanup. · `backend/services/storage.py`
- [Redis uses](https://github.com/dhananjaylab/memegpt/blob/main/backend/services/cache.py): cache/queue/rate limit · Caching, job coordination, and sliding-window quota enforcement. · `backend/services/cache.py`
- [Trending](https://github.com/dhananjaylab/memegpt/blob/main/backend/services/trending.py): ranking service · Trending score calculation and surfacing. · `backend/services/trending.py`
- [Auth & keys](https://github.com/dhananjaylab/memegpt/blob/main/backend/services/auth.py): security service · User auth and store-hash API key handling. · `backend/services/auth.py`
- [Integrations](https://github.com/dhananjaylab/memegpt/blob/main/backend/services/meme_ai.py): external adapters · Stripe, AI model access, and CDN/R2 configuration adapters. · `backend/services/meme_ai.py`
- [Public assets](https://github.com/dhananjaylab/memegpt/blob/main/backend/public/meme_data.json): static assets · Fonts, frames, and meme template seed data used by template rendering. · `backend/public/meme_data.json`

### Ops

Deployment and maintenance scripts.

- [Scripts](https://github.com/dhananjaylab/memegpt/tree/main/scripts): ops tooling · Deployment and storage maintenance scripts. · `scripts`

### External Systems

Third-party services and infrastructure the app integrates with.

- **PostgreSQL**: database · Primary relational persistence.
- **Redis**: cache/queue · Shared cache, queue, and rate-limit store.
- **Cloudflare R2**: object storage · Primary object store for generated media.
- **Gemini**: AI provider · Captioning/generation model integration.
- **Stripe**: billing · Subscription and entitlement integration.

## Connections

- SPA → Pages: composes · App routes render page views.
- SPA → UI pieces: uses · Pages assemble reusable UI components.
- SPA → Auth state: reads auth · App gates protected flows on auth state.
- Pages → API client: calls · Pages fetch data and trigger actions through the client.
- UI pieces → API client: calls · Interactive controls submit backend requests.
- UI pieces → Canvas: renders · Client-side preview/editor rendering helpers.
- Auth state → API client: authenticates · Auth context uses API-backed session state.
- API client → FastAPI app: HTTP · SPA talks to the backend API surface.
- FastAPI app → Core setup: boots · Application setup is centralized in core modules.
- FastAPI app → API routers: mounts · Main app exposes feature routers.
- API routers → Auth & keys: auth · Authentication and API key endpoints rely on auth services.
- API routers → Templates: templates · Template browsing and loading flows.
- API routers → Trending: trending · Trending endpoints expose ranking data.
- API routers → Storage layer: storage · Upload and asset-serving routes use storage services.
- API routers → Integrations: billing/ai · AI and Stripe-facing endpoints.
- API routers → Redis uses: jobs/rate · Job status and request throttling rely on Redis.
- Services → Redis: uses · Cache, queue, and rate limiting are Redis-backed.
- Services → Cloudflare R2: uploads · Primary object storage target.
- Services → Gemini: calls · AI captioning and generation provider.
- Services → Stripe: calls · Subscription and billing integration.
- Services → ARQ worker: queues jobs · Expensive generation work is offloaded to the worker.
- ARQ worker → Services: uses · Worker consumes shared generation, storage, and composition services.
- ARQ worker → Redis uses: pulls jobs · Worker coordination and job state use Redis.
- Models → PostgreSQL: persists · SQLAlchemy models map to PostgreSQL tables.
- DB session → PostgreSQL: connects · Database session binds to PostgreSQL.
- Migrations → PostgreSQL: evolves · Alembic migrations update the PostgreSQL schema.
- Templates → Public assets: loads · Template pipeline reads static assets and seed data.
- Templates → Models: stores · Curated templates are persisted in the database.
- Storage layer → Public assets: falls back · Local frames/fonts support offline or fallback rendering.
- Scripts → Storage layer: maintains · Cleanup and migration scripts operate on storage assets.
