# w3cj/how-llms-work architecture

> Architecture diagram of the GitHub repository w3cj/how-llms-work, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/w3cj/how-llms-work
- Repository: https://github.com/w3cj/how-llms-work

## Overview

#### Purpose and Runtime

“How LLMs Work” is a local interactive educational application demonstrating five increasingly capable NLP/ML stages, from rule-based chat through training a decoder-only transformer without ML libraries. It runs on Node.js 20+ with Hono as the server/JSX runtime and Vite for development and bundling. Main server entry: `src/index.tsx`; client bootstrap and application root: `src/client/index.tsx`, `src/client/root.tsx`.

#### Application Composition and Routing

The client defines navigation/page composition in `src/client/routes.tsx` and renders the shared application shell in `src/client/components/app/index.tsx`. The server exposes POST endpoints under `src/routes/`; each corresponds to a learning stage and streams progressive results rather than returning only a final answer:
- `src/routes/simple-chat.ts`
- `src/routes/neural-net/index.ts`
- `src/routes/bpe-tokenize.ts`
- `src/routes/train-embed/index.ts`
- `src/routes/train-transformer/index.ts`

#### Shared Streaming Boundary

Server computation is decoupled from rendering through Server-Sent Events. Route handlers validate/process requests, emit typed incremental events, and terminate streams via `src/server/lib/sse.ts`. Client transport/parsing lives in `src/client/lib/sse.ts`, while `src/client/hooks/use-sse-chat.ts` supplies the common request, stream-consumption, loading, and error lifecycle. This SSE boundary enables long-running training to update visualizations incrementally.

#### Client State, Interaction, and Visualization

`src/client/context/chat-context.ts` and `src/client/context/chat-provider.tsx` hold conversation/demo state shared across the UI. Stage-specific hooks adapt streamed event payloads into view state, notably `src/client/hooks/use-neural-net-chat.tsx`, `src/client/hooks/use-bpe-tokenize-chat.tsx`, and `src/client/hooks/use-train-transformer-chat.tsx`. The app pairs these with specialized visual result renderers such as `src/client/components/neural-net-result/index.tsx`, `src/client/components/bpe-tokenize-result/index.tsx`, and `src/client/components/train-transformer-result/index.tsx`; `src/client/components/chat-input/index.tsx` is the common input boundary.

#### Educational Compute Stages

1. Rule-based ELIZA-style response generation: `src/routes/simple-chat.ts`.
2. XOR perceptron/network training, serialization, and backpropagation: `src/routes/neural-net/train.ts`, `src/routes/neural-net/serialize.ts`.
3. From-scratch BPE merge/vocabulary training: `src/routes/bpe-tokenize.ts`, `src/server/lib/bpe.ts`.
4. Word2Vec skip-gram embedding training with corpus preparation: `src/routes/train-embed/train.ts`, `src/routes/train-embed/corpus.ts`.
5. Handwritten GPT-style transformer training and generation: `src/routes/train-transformer/train.ts`, `src/routes/train-transformer/transformer.ts`.

#### Transformer Training Execution Boundary

Transformer internals include matrix operations and explicit parameter layout in `src/routes/train-transformer/matrix.ts` and `src/routes/train-transformer/weight-layout.ts`. Training is delegated to worker code (`src/routes/train-transformer/train-worker.ts`), with a generated worker artifact at `src/routes/train-transformer/.train-worker.mjs`. It uses multi-threaded data parallelism through `SharedArrayBuffer`, separating intensive numerical work from the request/SSE orchestration path.

#### Contracts and Shared Algorithm Primitives

Request contracts are stage-specific schemas: `src/schemas/chat-request.ts`, `src/schemas/neural-net-request.ts`, and `src/schemas/train-transformer-request.ts`. Shared message/event shape is defined in `src/shared/types/message.ts`. BPE is a cross-stage primitive used by tokenization, embeddings, and transformer training (`src/server/lib/bpe.ts`); foundational numeric helpers are centralized in `src/server/lib/math.ts`. No external AI model, managed ML service, database, or cloud infrastructure is indicated.

Main entry points: [`src/routes/train-transformer/index.ts`](https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-transformer/index.ts) (Stage API routes), [`src/client/routes.tsx`](https://github.com/w3cj/how-llms-work/blob/main/src/client/routes.tsx) (Client routes), [`src/client/hooks/use-sse-chat.ts`](https://github.com/w3cj/how-llms-work/blob/main/src/client/hooks/use-sse-chat.ts) (SSE client lifecycle), [`src/client/hooks/use-train-transformer-chat.tsx`](https://github.com/w3cj/how-llms-work/blob/main/src/client/hooks/use-train-transformer-chat.tsx) (Stage stream adapters)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client UI"]
  node_client_bootstrap["Client bootstrap<br/>client entry<br/>[index.tsx]"]
  node_app_root["Application root<br/>UI composition<br/>[root.tsx]"]
  node_routes_ui["Client routes<br/>page routing<br/>[routes.tsx]"]
  node_app_shell["Application shell<br/>shared UI<br/>[index.tsx]"]
  node_chat_state["Chat state provider<br/>client context<br/>[chat-provider.tsx]"]
  node_sse_client["SSE client lifecycle<br/>transport hook<br/>[use-sse-chat.ts]"]
  node_stage_hooks["Stage stream adapters<br/>client hooks"]
  node_result_views["Training result views<br/>visualization UI<br/>[index.tsx]"]
end

subgraph group_server["Server &amp; learning stages"]
  node_server_entry["Hono server<br/>server entry<br/>[index.tsx]"]
  node_stage_routes["Stage API routes<br/>Hono POST routes<br/>[index.ts]"]
  node_sse_server["SSE stream writer<br/>server transport<br/>[sse.ts]"]
  node_early_stages["Early learning stages<br/>rule, neural net, BPE<br/>[bpe-tokenize.ts]"]
  node_embedding_training["Word2Vec training<br/>skip-gram trainer<br/>[train.ts]"]
  node_bpe["BPE primitive<br/>tokenization algorithm<br/>[bpe.ts]"]
end

subgraph group_transformer["Transformer execution"]
  node_transformer_orchestrator["Transformer trainer<br/>request orchestration<br/>[train.ts]"]
  node_worker_training{{"Parallel training worker<br/>worker runtime<br/>[train-worker.ts]"}}
  node_transformer_core["Transformer core<br/>model implementation<br/>[transformer.ts]"]
end

subgraph group_shared["Shared contracts"]
  node_contracts["Requests &amp; events<br/>shared contracts<br/>[message.ts]"]
end

node_client_bootstrap -->|"mounts"| node_app_root
node_app_root -->|"composes"| node_routes_ui
node_routes_ui -->|"renders in"| node_app_shell
node_app_shell -->|"uses"| node_chat_state
node_routes_ui -->|"uses"| node_stage_hooks
node_stage_hooks -->|"extends"| node_sse_client
node_stage_hooks -->|"updates"| node_result_views
node_sse_client -->|"POST and consume SSE"| node_stage_routes
node_server_entry -->|"serves"| node_stage_routes
node_stage_routes -->|"streams through"| node_sse_server
node_stage_routes -->|"runs"| node_early_stages
node_stage_routes -->|"runs"| node_embedding_training
node_stage_routes -->|"runs"| node_transformer_orchestrator
node_early_stages -->|"trains with"| node_bpe
node_embedding_training -->|"tokenizes with"| node_bpe
node_transformer_orchestrator -->|"tokenizes with"| node_bpe
node_transformer_orchestrator -->|"delegates training"| node_worker_training
node_worker_training -->|"executes"| node_transformer_core
node_stage_routes -->|"validates and emits"| node_contracts
node_sse_client -.->|"parses"| node_contracts

click node_server_entry "https://github.com/w3cj/how-llms-work/blob/main/src/index.tsx"
click node_client_bootstrap "https://github.com/w3cj/how-llms-work/blob/main/src/client/index.tsx"
click node_app_root "https://github.com/w3cj/how-llms-work/blob/main/src/client/root.tsx"
click node_routes_ui "https://github.com/w3cj/how-llms-work/blob/main/src/client/routes.tsx"
click node_app_shell "https://github.com/w3cj/how-llms-work/blob/main/src/client/components/app/index.tsx"
click node_chat_state "https://github.com/w3cj/how-llms-work/blob/main/src/client/context/chat-provider.tsx"
click node_sse_client "https://github.com/w3cj/how-llms-work/blob/main/src/client/hooks/use-sse-chat.ts"
click node_stage_hooks "https://github.com/w3cj/how-llms-work/blob/main/src/client/hooks/use-train-transformer-chat.tsx"
click node_result_views "https://github.com/w3cj/how-llms-work/blob/main/src/client/components/train-transformer-result/index.tsx"
click node_stage_routes "https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-transformer/index.ts"
click node_sse_server "https://github.com/w3cj/how-llms-work/blob/main/src/server/lib/sse.ts"
click node_early_stages "https://github.com/w3cj/how-llms-work/blob/main/src/routes/bpe-tokenize.ts"
click node_embedding_training "https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-embed/train.ts"
click node_transformer_orchestrator "https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-transformer/train.ts"
click node_worker_training "https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-transformer/train-worker.ts"
click node_transformer_core "https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-transformer/transformer.ts"
click node_bpe "https://github.com/w3cj/how-llms-work/blob/main/src/server/lib/bpe.ts"
click node_contracts "https://github.com/w3cj/how-llms-work/blob/main/src/shared/types/message.ts"

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_client_bootstrap,node_app_root,node_routes_ui,node_app_shell,node_chat_state,node_sse_client,node_stage_hooks,node_result_views toneBlue
class node_server_entry,node_stage_routes,node_sse_server,node_early_stages,node_embedding_training,node_bpe toneAmber
class node_transformer_orchestrator,node_worker_training,node_transformer_core toneMint
class node_contracts toneRose
```

## Components

### Client UI

Interactive learning interface and streamed-result views.

- [Client bootstrap](https://github.com/w3cj/how-llms-work/blob/main/src/client/index.tsx): client entry · `src/client/index.tsx`
- [Application root](https://github.com/w3cj/how-llms-work/blob/main/src/client/root.tsx): UI composition · `src/client/root.tsx`
- [Client routes](https://github.com/w3cj/how-llms-work/blob/main/src/client/routes.tsx): page routing · `src/client/routes.tsx`
- [Application shell](https://github.com/w3cj/how-llms-work/blob/main/src/client/components/app/index.tsx): shared UI · `src/client/components/app/index.tsx`
- [Chat state provider](https://github.com/w3cj/how-llms-work/blob/main/src/client/context/chat-provider.tsx): client context · Shared conversation and demo state. · `src/client/context/chat-provider.tsx`
- [SSE client lifecycle](https://github.com/w3cj/how-llms-work/blob/main/src/client/hooks/use-sse-chat.ts): transport hook · Requests streams and manages loading and errors. · `src/client/hooks/use-sse-chat.ts`
- [Stage stream adapters](https://github.com/w3cj/how-llms-work/blob/main/src/client/hooks/use-train-transformer-chat.tsx): client hooks · Maps neural-net, BPE, and transformer events into view state. · `src/client/hooks/use-train-transformer-chat.tsx`
- [Training result views](https://github.com/w3cj/how-llms-work/blob/main/src/client/components/train-transformer-result/index.tsx): visualization UI · Stage-specific progressive visualizations. · `src/client/components/train-transformer-result/index.tsx`

### Server & learning stages

Hono routes orchestrate educational compute and SSE output.

- [Hono server](https://github.com/w3cj/how-llms-work/blob/main/src/index.tsx): server entry · `src/index.tsx`
- [Stage API routes](https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-transformer/index.ts): Hono POST routes · Simple chat, neural net, BPE, embedding, and transformer endpoints. · `src/routes/train-transformer/index.ts`
- [SSE stream writer](https://github.com/w3cj/how-llms-work/blob/main/src/server/lib/sse.ts): server transport · Emits typed incremental events and terminates streams. · `src/server/lib/sse.ts`
- [Early learning stages](https://github.com/w3cj/how-llms-work/blob/main/src/routes/bpe-tokenize.ts): rule, neural net, BPE · ELIZA responses, XOR training, and BPE vocabulary training. · `src/routes/bpe-tokenize.ts`
- [Word2Vec training](https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-embed/train.ts): skip-gram trainer · Prepares corpus and trains embeddings. · `src/routes/train-embed/train.ts`
- [BPE primitive](https://github.com/w3cj/how-llms-work/blob/main/src/server/lib/bpe.ts): tokenization algorithm · Shared tokenizer for BPE, embeddings, and transformer stages. · `src/server/lib/bpe.ts`

### Transformer execution

Parallel handwritten transformer training pipeline.

- [Transformer trainer](https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-transformer/train.ts): request orchestration · Coordinates handwritten GPT training and generation. · `src/routes/train-transformer/train.ts`
- [Parallel training worker](https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-transformer/train-worker.ts): worker runtime · SharedArrayBuffer data-parallel numerical training. · `src/routes/train-transformer/train-worker.ts`
- [Transformer core](https://github.com/w3cj/how-llms-work/blob/main/src/routes/train-transformer/transformer.ts): model implementation · Handwritten decoder-only transformer, matrix operations, and parameter layout. · `src/routes/train-transformer/transformer.ts`

### Shared contracts

- [Requests & events](https://github.com/w3cj/how-llms-work/blob/main/src/shared/types/message.ts): shared contracts · Stage schemas and common message/event shapes. · `src/shared/types/message.ts`

## Connections

- Client bootstrap → Application root: mounts
- Application root → Client routes: composes
- Client routes → Application shell: renders in
- Application shell → Chat state provider: uses
- Client routes → Stage stream adapters: uses
- Stage stream adapters → SSE client lifecycle: extends
- Stage stream adapters → Training result views: updates
- SSE client lifecycle → Stage API routes: POST and consume SSE
- Hono server → Stage API routes: serves
- Stage API routes → SSE stream writer: streams through
- Stage API routes → Early learning stages: runs
- Stage API routes → Word2Vec training: runs
- Stage API routes → Transformer trainer: runs
- Early learning stages → BPE primitive: trains with
- Word2Vec training → BPE primitive: tokenizes with
- Transformer trainer → BPE primitive: tokenizes with
- Transformer trainer → Parallel training worker: delegates training
- Parallel training worker → Transformer core: executes
- Stage API routes → Requests & events: validates and emits
- SSE client lifecycle → Requests & events: parses
