Skip to article
endpoint51

Inside Briefing Engine

This document follows one article through Briefing Engine, from a publisher's feed to a reader's inbox, and explains why each step is built the way it is. How I built Briefing Engine covers what the service is for and why I built it.

Part 2 of 4: guide contents
On this page

Four jobs and one rule

Briefing Engine does its work as four kinds of job: fetch a feed, score the new articles, build the briefing and send it. Each job is a row in a PostgreSQL table, and a worker process claims the jobs that are due and carries them out. One rule holds the design together: every job saves its result in the same database transaction as the job that comes next. A crash can then never leave a saved result with nothing queued to act on it, and the next worker carries on from whatever the queue records.

The four jobs. Each one is claimed from PostgreSQL and saves its result there in the same transaction as the job that comes next, which is what the arrows between the jobs stand for.

One article, step by step

An article published on a feed the reader follows goes through five steps before it reaches their inbox.

  1. The worker fetches the feed. Before it sends a request, it takes the website's turn: a row for each host in PostgreSQL records when that site may next be contacted, two seconds apart by default and ten seconds for Reddit. Because the record is shared, the spacing holds however many workers are running, and a job that has to wait is put back without using up one of its attempts.

    If the feed hasn't changed since the last fetch, the work stops there. Otherwise the parser takes the longer of each entry's summary and content fields, because newsletters often put a one-line teaser in the summary and the whole post in the content, and the scorer can only judge the text it is given. Each link is normalised, with tracking parameters removed, so an article that arrives through two feeds is stored once.

  2. The worker saves the article and queues its scoring job in one transaction. If it stopped between the two, the article would sit in the database with nothing to say it still needed a score. Keeping the queue in PostgreSQL, rather than in a separate service such as Amazon SQS, is what makes a single transaction possible.

    Workers claim jobs with FOR UPDATE SKIP LOCKED, so two workers never claim the same job, and each claim is a time-limited lease. If a worker dies, its lease runs out and another worker picks the job up, and a worker that has lost its lease can't save results over the new owner's.

  3. Claude Haiku scores the article against the reader's interests. Up to ten articles that share a rubric, the reader's written description of what they want, go in one request, each as its title and up to 6,000 characters of text. Before paying for a call, the worker checks a cache keyed on everything the answer depends on: the text Claude is shown, the rubric, the model and the prompt version. A changed rubric therefore gets fresh scores, while the same text fetched again reuses the old one.

    The reply comes back as structured data, and the code checks that every article sent comes back exactly once with a score in range. An article Claude leaves out is recorded as a failure, not a zero, because a zero would claim the reader isn't interested when nobody judged the article at all.

  4. The code chooses the articles, and only then does Claude Sonnet write about them. A build job takes the articles found since the reader's last briefing, published within the profile's age limit and scoring at least its threshold. It ranks them by score multiplied by the feed's weight, with a small bonus for a story that several feeds carried, drops repeated content and stops at the item limit.

    Sonnet, which costs twenty times as much per token as Haiku at the prices the project records, then writes a headline, an introduction and a short paragraph for each chosen article. Because the choice is already made, Sonnet can only write about what it is given.

    If writing fails, the briefing still goes out with Haiku's reasons in place of the paragraphs, marked as incomplete. The briefing is saved with its delivery job, and a database rule allows one briefing per reader, profile and day, so a second build on the same day sends nothing.

  5. The worker renders the email and sends it. Sonnet returns plain text, never HTML: the layout comes from templates that escape each value exactly once, and a test compares the output with a known-good copy.

    Before sending, the worker marks the delivery as in flight and commits that; once the mail server accepts the message, it marks the delivery as sent. A crash between the two leaves the delivery in flight, and the next attempt records the first outcome as unknown before sending again. This is the one place Briefing Engine can't promise exactly one email: it accepts a rare duplicate rather than risk losing a briefing. Over SMTP the retry reuses the message's ID, which some mail clients recognise as the same message, but Amazon SES assigns its own ID, so that protection doesn't carry over.

The same jobs as a service

On a PC, the daily command queues a fetch job for each feed, works through the fetching and scoring, then queues the builds, and exits when nothing is left. As a service, the same jobs come from two places. The API, which readers use to manage their profiles and feeds, records a build job when someone asks for a briefing and replies at once with the job's ID, because building and sending take longer than a web request should stay open. A scheduler in the worker checks every 30 seconds for feeds that need fetching and briefings that are due, and starts each reader's briefing five minutes before their send time.

On AWS, the API and the worker run as two services from one Docker image on ECS Fargate, with PostgreSQL on Amazon RDS and email through Amazon SES. The jobs, and the rule that each one saves its result with the next, are unchanged.

Where to find it in the code

Each step lives in a small number of files in the public repository.

StepFiles
Fetching and spacing requestsingest/fetch_job.py, ingest/politeness.py, ingest/feeds.py, ingest/urls.py
The queue and leasesqueue/jobs.py, queue/worker.py
Scoringllm/score_job.py, llm/scorer.py, llm/scoring_input.py
Choosing and writingbuild.py, llm/writer.py
Rendering and sendingdelivery/render.py, delivery/deliver_job.py
The scheduler and the APIscheduler.py, api/app.py

What's next

You can run Briefing Engine on your own computer and watch each of these steps happen. The next document, Running Briefing Engine locally, takes you from a Windows PC without the project to a briefing in Mailpit, a test inbox that keeps mail on your machine.

You will:

  1. install Git, uv and Docker Desktop, and get an Anthropic API key, because scoring is a paid call to Claude;
  2. download the project from GitHub and install its Python packages;
  3. start PostgreSQL and Mailpit in Docker, and give the database its tables;
  4. set up one reader with one feed;
  5. run the daily command and open the email it sends.

By the end, you will have Briefing Engine running on your own computer and a briefing of your own to read.