This Demo
Type What is 1234 plus 5678? in the browser and press Enter—below is the real streaming output (1234+5678 is not in the SFT training data, but the model learned to extend its capabilities via the calc tool):
A 124M model absolutely cannot get 4-digit addition right on its own (the chat-SFT version answers "13"). With the calc tool, it can. This is the essence of agents—model capability = knowledge + tool extension.
Eight-Layer Architecture
Each layer has < 300 lines of core code and runs independently. The entire stack runs from scratch on an RTX 5090 in ~70 seconds.
05_gpu/04_transformer/00_train/00b_sft/00c_agent_sft/03_model/01_app/02_agent/Key Numbers (Measured)
RTX 5090, three independent cold-start verifications. See full raw logs at reports/.
The Most Important Demo: 1234 + 5678 → 6912
calc(1234 + 5678), passing arbitrary numbers to a real Python tool.This validates the essential thesis of agents: the model learns the format contract of tool invocation ("when you see an arithmetic problem, call calc"), not the tool's capability itself (computation). This is the same essence as ChatGPT connecting to web_search, Cursor connecting to grep+edit, and Claude connecting to computer use.
| Query | Category | Output | |
|---|---|---|---|
| capital of France? | KB lookup, in-data | Paris. | ✓ |
| 23 plus 47? | calc, in-data | 70. | ✓ |
| 1234 plus 5678? | calc, OOD | 6912. | ✓ Key generalization |
| Who wrote Hamlet? | KB lookup, in-data | Shakespeare. | ✓ |
| chemical symbol of gold? | KB lookup, in-data | Au. | ✓ |
| capital of Mongolia? | KB miss | not found. | ✓ Honest |
| How are you today? | OOD conversational | hallucinate lookup | ✗ Known failure |
Starting from Scratch
Complete cold-start commands (CN region, tested three times):
# 1. Clone (direct GitHub access doesn't work in CN, use gh-proxy)
git clone https://gh-proxy.com/https://github.com/fxp/LLM-from-query-to-result.git
cd LLM-from-query-to-result
# 2. Configure pip mirror + install
mkdir -p ~/.pip
echo -e "[global]\nindex-url = https://mirrors.aliyun.com/pypi/simple/\ntrusted-host = mirrors.aliyun.com" > ~/.pip/pip.conf
pip install -r requirements.txt
# 3. Train base + SFT + agent SFT (~70 sec on 5090)
cd 00_train && python prepare.py && python train.py
cd ../00b_sft && python train.py && python train_from_gpt2.py # The latter downloads 124M weights on first run (HF mirror auto-fallback)
cd ../00c_agent_sft && python build_data.py && python train.py
# 4. Start server (agent.pt is the 124M model with tool-calling)
MODEL_PATH=$(pwd)/out/agent.pt python ../03_model/server.py &
AGENT_MODE=1 uvicorn 01_app.backend.main:app --port 8000 &
# 5. Open http://localhost:8000 in browser
# Type "What is 1234 plus 5678?" → tokens pop out one by one: "6912."
Deep Reading
| Article | Topic | |
|---|---|---|
| 📚 | Condensed Single-Post Version | The story of the entire project, 4000 words |
| 📊 | Experiment Report (HTML) · Markdown | Formal format: methods, results, discussion, reproducibility |
| 📖 | 11-Chapter Blog Series | One post per layer, ~30K words total |
| 🧪 | Raw Experiment Logs | Complete stdout from three independent cold-starts |
| 💻 | Full Source Code | ~10K lines Python + ~165 lines CUDA |
Differentiating Contributions
There are already many excellent "build GPT from scratch" tutorials in the industry (karpathy/nanoGPT, minbpe, etc.). This project's differentiators:
| Topic | Most Tutorials | This Project |
|---|---|---|
| GPT architecture | ✓ | ✓ |
| Pretraining loop | ✓ | ✓ |
| BPE tokenizer | Some (minbpe) | ✓ Verified bit-for-bit equivalent to tiktoken |
| SFT instruction tuning | Rarely | ✓ |
| Agent SFT + ReAct loop | Almost never | ✓ |
| Inference serving (KV cache + SSE) | Rarely | ✓ |
| Web frontend | Almost never | ✓ |
| GPU kernel internals | Some | ✓ Triton + CUDA comparison |
| End-to-end trace from browser to matmul | Almost never | ✓ |