Run it yourself
Everything in the book can be rebuilt from the files below. The source, including the tests, is in the supertj/comment-scraper-python-book repository on GitHub.
The files¶
Sample data, written for this book and in Comment Scraper’s export format:
sample_thread.json: the Reddit thread, “JSON, one row per comment”
sample
_thread _nested .json: the same thread, “JSON, replies nested” sample_thread.csv: the same thread as CSV
sample
_hn _thread .json and sample _hn _thread .csv: the Hacker News thread price_labels.csv: my reading of the 30 comments in the price section of Count the words people use
Code:
threadtools.py: loading, tree checks, the numbered Markdown and the quote checks
charts.py: the two charts
make
_sample _export .py: writes the sample data ask_model.py: sent the prompt to the model once
The model’s input and output:
prompt.md: the prompt, built by the last chapter’s code
answer.md: the answer, exactly as it came back
answer.json: which model answered, when, and how many tokens it used
Each notebook chapter can also be downloaded as a Jupyter notebook from the download menu at the top of its page.
Build the book¶
With Python 3.12, from the repository root:
python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python -m pytest -q # tests for threadtools.py
jupyter book build --html --execute # runs every notebook, writes _build/htmlThe build executes each notebook from the top, so the tables, charts and printed output come from the code above them. Some cells that are hidden in the book check the numbers the text relies on, and the build fails if the data changes under them.
The sample data¶
python scripts/make_sample_export.py writes the files in data/. Every comment is in the script, written by hand, and the IDs and timestamps come from a fixed random seed, so it writes the same files every time.
The model’s answer¶
scripts/ask_model.py sends data/llm/prompt.md to the Anthropic API once and saves the reply:
MODEL = "claude-haiku-5-5" # a small, cheap model, the kind you would run over many threads
LLM_DIR = Path(__file__).resolve().parent.parent / "data" / "llm"
def main():
prompt = (LLM_DIR / "prompt.md").read_text(encoding="utf-8")
client = anthropic.Anthropic()
response = client.messages.create(
model=MODEL,
max_tokens=16000,
# One pass, no extended thinking: the answer you would get from a quick API call.
thinking={"type": "disabled"},
messages=[{"role": "user", "content": prompt}],
)
if response.stop_reason != "end_turn":
raise SystemExit(f"stopped early: {response.stop_reason}")
answer = "".join(block.text for block in response.content if block.type == "text")
(LLM_DIR / "answer.md").write_text(answer, encoding="utf-8")
run = {
"model": response.model,
"thinking": "disabled",
"asked_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"input_tokens": response.usage.input_tokens,
"output_tokens": response.usage.output_tokens,
}
(LLM_DIR / "answer.json").write_text(json.dumps(run, indent=2) + "\n", encoding="utf-8")
print(f"saved {len(answer)} characters from {response.model}")
if __name__ == "__main__":
main()
The saved answer came from Claude Haiku 5.5 (claude-haiku-5-5) with extended thinking off, on 28 October 2026 (UTC). To try it with your own key, put the key in the ANTHROPIC_API_KEY environment variable, install the anthropic package and run the script. Your answer will differ from the saved one, and the last chapter’s text describes the saved one, so its hidden checks will fail until you update them. The test test_saved_prompt_matches_the_sample_thread fails if the sample data changes without a new answer.