Architecture notes · October 2026

Inside daf-ai

← daf.tsemach.dev

How AI agents write interactive Talmud study pages every night, check them against the Gemara, and stay within a token budget — and how the page itself answers learners' questions and draws what is hard to picture. Written by Tsemach Hadad, seven weeks and 1030 commits in.

377 pages · 13 masechtot 21,046 argument steps 1,595 glossary entries 31 checked drawings 995 / 1030 commits by Claude

daf.tsemach.dev · github.com/tsemachh/daf-ai

The core idea

The model is not the hard part. The pipeline is.

The first version was one shared HTML file that a scheduled task rewrote each day. It worked until it didn't: every run read and republished everything. The second version moved to a repo with JSON per daf and a nightly Claude Code routine. The third version, running today, gives models only two jobs: write and judge. Everything mechanical is a script.

Since then the site has grown a second kind of AI: one that runs while a learner is reading. The nightly pipeline builds the page; a small model on the page answers questions about it, from that page only.

Nightly run, one daf

The pipeline

Script, deterministicModel call
  1. reader notes (D1)

    Every note gets an answer. A fix that recurs becomes a writing or review rule.

  2. calendar + queue

    The Daf Yomi calendar decides today's page; a queue builds ahead, four dapim a day, with rush lines for an owner's request.

  3. fetch_sources.py

    Fills a private cache: Sefaria Gemara, Steinsaltz, Rashi, the Sinai summary and Daf Spoiler (with permission).

  4. pack_daf.py

    One compact pack, segment by segment: about 37 KB instead of about 300 KB of raw inputs.

  5. Sonnet · writer

    Produces the page JSON: every move as a step with a one-line summary, and long sugyot split into 2–5 milestones, each with its question and where it lands. No verbatim text and no glossary, so no output tokens spent on copying.

  6. fill_srctext.py

    Copies the vocalized Gemara from the cache. It reproduced 137 existing blocks exactly; the 45 that differed were model errors.

  7. check_page.py

    Every segment covered, argument-tree links valid, quote share at most 30%, a summary on every step, milestones on every long sugya, quiz shape, glossary candidates.

  8. Opus / Fable · reviewer

    Checks against the Gemara and returns patches only, tagged ERROR, OMISSION or THIN. Never a rewritten page.

  9. apply_review.py

    Applies the patches.

  10. gloss_contexts.mjs

    Prints every underlined term with the sentence it sits in and the definition the popup will show. A wrong sense (״קבלה״ as a contract, where the Gemara means receiving the blood) gets a new sense or a skip rule before the page ships.

  11. swdaf.py

    Links Simon Wolf's charts for the daf (with permission, at the source), titled in Hebrew; explanatory sheets go into the sugya's aids, advanced ones stay in the daf-level list.

  12. drawings, when a sugya needs one

    Only where no outside chart covers it. Three checks before it ships — see below. Measurements are drawn to scale (one fixed unit per drawing, labelled in the sugya's own units), and any aid can sit inside the sugya, right after the step that needs it.

  13. validate · build · Playwright · push

    Phone and desktop screenshots, a share image, a ״מה חדש״ line for anything changed outside the note queue, then Cloudflare Pages.

  14. recheck before the learning day

    Pages are built weeks ahead; summaries published later (Daf Spoiler, Sinai) trigger a second look at tomorrow's and today's page.

  15. a human reads tomorrow's page

    Every day I read the next page and send notes; they come back through the same note queue as readers' notes.

While the learner reads

A question box, grounded in the daf

Every sugya and every milestone has a ❔. The question goes to a Cloudflare function, which loads the whole daf from the site's own JSON — every sugya's Gemara text, steps, milestones, tables and summaries, the review questions, and the other half of a sugya that crosses into the neighbouring daf — and sends it with the question, marked with the sugya it was asked on, to Haiku. The page block is prompt-cached, so questions on any sugya of the same daf reuse it. The instructions are narrow: answer about that sugya from this material, say so when the answer isn't there, quote the Gemara in ״…״, no halachic rulings. Follow-ups carry the last three turns.

A ״איפה זה בדף?״ button shows the printed page (צורת הדף, the Nehardea edition, credited to Vagshal) with the current sugya marked; align_pdf.py aligns the text PDFs to the Sefaria segments without OCR, picking the Gemara fonts by how much of each run matches the daf's text. Every daf on the site has it.

Two smaller runtime pieces: a Gemara quote whose Steinsaltz only repeats the words and has no Rashi is shown as plain text, not a link to an empty popup (checked in the browser, per sugya, against the same Sefaria data). And a read-only JSON feed lets a non-commercial study app show the pages in its own design, with credit and a "report a mistake" link back.

Pictures for what is hard to picture

Drawings, checked like text

Some sugyot are about a shape: where on the eye a blemish sits, how a schach rests on two trees, which houses count as a walled city. Where Simon Wolf's charts don't cover it, the agent draws an SVG — labels in the site's fonts, themed for light and dark, inlined in the sugya's aids. A drawing is a claim about the Gemara, so it gets checks of its own:

For anatomy, line art wasn't enough. Gemini draws the realistic base (a calf's eye, an ear, a hoof); Claude overlays the numbered marks and a legend from the sources, so the picture is pretty and the claims stay checkable. Each drawing says it was made by AI and has a "report a mistake" button. All of them are on one page.

Two pilots on Bechorot 42. Where is it on the page: the Vilna page image is OCR'd (tesseract), aligned to the Sefaria text, and each sugya gets its outline on the printed page; a viewer opens zoomed to the sugya. Slides: a NotebookLM deck is exported as PDF and split by sugya (slides_publish.py); slides whose text came out wrong or garbled are dropped before publishing. Decks now cover the whole daf as a teacher aid (one header button, one card per daf); each deck gets a review pass against the daf page plus rule-based fixes (wrong-way arrows, rabbis who are not party to the sugya erased) before it goes up.

Learned the hard way

Seven design rules

  1. Scripts do anything mechanical. Models only write and judge.

  2. Every format rule has a check. If nothing enforces it, it drifts.

  3. One daf per run, with a budget, and work saved as it goes so a stopped run loses nothing.

  4. Reviews return patches, never rewritten pages.

  5. Reader notes are data, not instructions. They get answered, and a fix that recurs becomes a rule.

  6. Every backfill gets a second pass. 2,407 step summaries were written in bulk; a check against the full text corrected 67 of them.

  7. Pilot on one page, on a preview branch. Milestones were reviewed on a single sugya before they reached 297.

Same pages, measured

Models and tokens

WriterDafTokensResult
HaikuBechorot 18~175k · 10.6 minFailed. Split the sugya mid-argument and padded every step with a repeated quote.
SonnetBechorot 18~121k · 2.8 minGood structure, explanations about half as deep.
Sonnet + Fable reviewBerachot 2~280kLive. Review caught 13 errors and deepened 6 thin steps.
Fable aloneBerachot 2~134k40 steps, no review. In the blind reader vote.
10 Fable agents in parallelBerachot2.4M · ~20 minNothing published. Each agent reloaded the full prompt and sources.

A cheap writer plus a strong reviewer is not automatically cheaper. On Berachot 2, a strong model writing alone used about half the tokens. The real saving comes from what the model reads and writes, not from which model it is.

Token counts are per-agent totals reported by the tools, not billing figures. The blind reader vote closed with only four votes, too few to call; the nightly run settled on Sonnet writing and one strong-model review. The versions stay up at Bechorot 18 and Berachot 2.

What broke, and what it changed

Glitches

Transcribing instead of explaining

Early pages were about 60% Gemara quotes. Readers said they didn't help.

An "explain, don't transcribe" rule plus a quote-share check. Bechorot 18 went from 60% to 24%.

A content rule applied too broadly

The agent stretched a content rule over legitimate halachic discussion and silently dropped steps of the argument. Readers noticed the gaps.

The rule was narrowed, then emptied. A content policy is code: it needs scope, tests and an owner.

Confident wrong cases

Sonnet described a Mishnah case with the wrong mix of animals, fluently.

Every page gets one review pass by a stronger model.

The bulk-run token burn

Ten parallel agents, 2.4M tokens, the session limit hit, 14 unverified partial pages.

One daf per run, a budget, sugya-by-sugya saves, and no reading files a script can query.

Retyping what a script can copy

A fifth of every page was vocalized Gemara, retyped as output tokens, the most expensive kind.

fill_srctext.py copies it from the cache.

Format drift

New pages lost hidden answers and the quote marks the site uses to style and link the Gemara.

The exact HTML is in the brief, and check_page.py rejects pages that break it.

Summaries from half a step

Bulk agents writing 2,407 one-line summaries read only the first 200 characters of long steps, and one couldn't rule out pairing a summary with the wrong step.

Alignment checked by key, then a second set of agents compared every summary with the full step: 67 fixed — wrong speaker, missing conclusion, invented claim.

A layout bug that read as content

A reader said the ״סדר האירועים״ box was "very long and unreadable" on mobile. It borrowed a two-column grid and squeezed every item into a 62-pixel column.

Numbered cards with arrows, the move bold and its answer beneath. Half the height, all 13 boxes fixed at once.

A pilot that hid the text

On phones, milestone steps showed only their one-line summary until tapped. In learning mode that hid the explanation people came for.

Full text by default; one-line summaries stay where they belong, in the תמצית reading level.

The right word, the wrong sense

The glossary underlined ״קבלה״ in a sugya about receiving the blood and explained it as a contract. A reader asked who wrote that.

An audit of all 6,460 underlined words in context: 66 fixed with new senses and skip rules, 270 ordinary words no longer underlined, and the same check now runs every night.

A drawing that looked right

A tail drawn cut short, under a Mishnah about a tail born short; a membrane drawn as a stain. Clean, confident, wrong.

Labels must cite a segment, and a blind reviewer sees only the Gemara, Rashi and the picture.

A move without its setup

On Bechorot 46 a step said ״איידי דסיפא״ — "because of the latter clause" — without saying what either clause was. A reader couldn't follow it.

An optional ״רקע״ line names the cases a move leans on. The reviewer flags any step that needs one; 84 steps across all masechtot got it, and a later pass dropped it where it only repeated the step before.

A sugya cut at the page break

Sugyot that run across the daf break were split in two, each half on its own page. A reader on Bechorot 52 landed mid-Mishnah, with no idea what ״שבח״ and ״ראוי״ were.

A rule and a merge script: a sugya lives whole on one page, the daf where most of it sits, with its steps tagged by daf. Every masechet on the site was re-cut the same day. Later the owner tightened it: a sugya that starts at the end of a daf goes to the next one unless the earlier daf holds at least 60% of it, and a move script re-cuts the remaining ones, three a night.

The human slip

I pushed private source material to the public repo.

The agent flagged it on the next push. History rewritten, .gitignore tightened.

Stack
Claude Code routinesSonnetOpusFableHaiku (runtime)Anthropic APIPythonVanilla JSSefaria APIHebcalCloudflare PagesD1TurnstileAccessResendPlaywrightGemini (image bases)
Accountability

The human signs the page

A model doesn't answer for what it writes. I do. Every page says it is a study aid, every answer in the question box says the same, and every reader note gets a reply.