How w3cj/how-llms-work works
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: index.ts Stage API routes · routes.tsx Client routes · use-sse-chat.ts SSE client lifecycle · use-train-transformer-chat.tsx Stage stream adapters
Components 18
Client UI
Interactive learning interface and streamed-result views.
- Client bootstrap
src/client/index.tsx - Application root
src/client/root.tsx - Client routes
src/client/routes.tsx - Application shell
src/client/components/app/index.tsx - Chat state provider · Shared conversation and demo state.
src/client/context/chat-provider.tsx - SSE client lifecycle · Requests streams and manages loading and errors.
src/client/hooks/use-sse-chat.ts - Stage stream adapters · Maps neural-net, BPE, and transformer events into view state.
src/client/hooks/use-train-transformer-chat.tsx - Training result views · 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
src/index.tsx - Stage API routes · Simple chat, neural net, BPE, embedding, and transformer endpoints.
src/routes/train-transformer/index.ts - SSE stream writer · Emits typed incremental events and terminates streams.
src/server/lib/sse.ts - Early learning stages · ELIZA responses, XOR training, and BPE vocabulary training.
src/routes/bpe-tokenize.ts - Word2Vec training · Prepares corpus and trains embeddings.
src/routes/train-embed/train.ts - BPE primitive · Shared tokenizer for BPE, embeddings, and transformer stages.
src/server/lib/bpe.ts
Transformer execution
Parallel handwritten transformer training pipeline.
- Transformer trainer · Coordinates handwritten GPT training and generation.
src/routes/train-transformer/train.ts - Parallel training worker · SharedArrayBuffer data-parallel numerical training.
src/routes/train-transformer/train-worker.ts - Transformer core · Handwritten decoder-only transformer, matrix operations, and parameter layout.
src/routes/train-transformer/transformer.ts
Shared contracts
- Requests & events · Stage schemas and common message/event shapes.
src/shared/types/message.ts
Connections 20
- 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