← Case studies

dots-agent

An LLM video pipeline with human approval gates and an adversarial fact-checker

I wanted to make short “connecting the dots” explainer videos on economics and world affairs without letting a language model publish unchecked claims. dots-agent is a LangGraph pipeline (research, script, critic, voice, visuals, render) that stops twice for a human decision in Telegram, tags every sentence as sourced fact or opinion, and routes doubtful claims to me through a claim ledger. It shows how I handle orchestration state, structured LLM output, retries and cost control.

Type
Case study
Built with
Python, LangGraph, Claude and DeepSeek, Telegram, ElevenLabs, Remotion
Published
Reading time
12 min

The code is private. I'm happy to walk through it in an interview.

The problem

An explainer video that links a few separate developments (“this rate decision, this commodity price, this election”) into one story is mostly research and writing. Language models are good at both and also good at sounding certain about things they made up. In finance and politics a confident wrong number or an opinion stated as fact is the worst possible failure.

So the goal was not “generate a video”. It was: let the model do the slow work (finding sources, drafting, cutting Shorts, building charts) while a person keeps control of the two decisions that matter, which story to tell and which claims survive. Everything runs from a Telegram chat on my own machine, so a review takes a few taps on a phone.

Architecture

Two entry flows RSS radar ranked angles Thesis outline my notes + clippings Gate: pick angle or regenerate Gate: outline approve / revise / cancel Research web search, FRED, WB Script fact / inference tags Critic claim ledger Gate: claim review keep / suggestion / cut Voice TTS + word timings Visuals charts, maps, B-roll Render Remotion + ffmpeg Package + upload metadata, YouTube API Control and state Telegram bot invoke / resume SQLite checkpoints, metrics

Figure 1. The LangGraph pipeline. Highlighted boxes are the points where the graph pauses for a human decision; the Telegram bot resumes it.

The pipeline is a LangGraph StateGraph with twelve nodes. Every stage lives in its own module under dots/stages/ and writes a typed pydantic artifact as JSON into runs/<id>/, so each step can be inspected and cached on its own.

There are two ways in. The news flow scans a list of RSS feeds from the last 48 hours, deduplicates headlines and asks a model for ranked angles; I pick one in Telegram or ask for new ones. The thesis flow, the one I use most, starts from my own notes plus links I forwarded to the bot during the week (article text extracted with trafilatura). The model turns that into a one-page outline with beats, the strongest counterargument and scenarios with rough odds and signposts. I approve it, send revision notes (which loop back to the outline node) or cancel.

After that the path is shared:

  • Research returns a fact pack: each fact has a claim, source URL, publisher, date and snippet. Thesis episodes search only a curated list of domains (official data, major outlets, think tanks) from config.yaml. The model can request chart series by FRED series ID or World Bank indicator, and code fetches the numbers from those APIs.
  • Script writes sentences, each tagged fact (with fact IDs) or inference, plus a visual hint and Shorts cut points.
  • Critic reviews the script against the fact pack and returns a claim ledger: verdict (keep, soften, cut), confidence, reason, a competing explanation, suggested rewrite and risk flags.
  • Claim review is the second human gate. I get the full script as a file plus one Telegram message per doubtful sentence with Keep / Use suggestion / Cut buttons, or I reply with my own text.
  • Voice calls ElevenLabs with timestamps, so word timings drive captions and sentence timings drive scene cuts.
  • Visuals builds a Remotion timeline: FRED/World Bank charts, maps, stat cards, an animated “dots board”, and stock B-roll from Pexels/Pixabay with each licence logged.
  • Render shells out to Remotion for the 16:9 video, 9:16 Shorts and thumbnails, then normalises loudness with ffmpeg. Package writes titles, a description with sources and disclosures, and chapters; an optional step uploads to YouTube.

The render side is a small TypeScript/React project (Remotion) that only reads the timeline JSON; it has no logic of its own about content.

Key design decisions

LangGraph interrupt() with a SQLite checkpointer, instead of a hand-written state machine. The gates are plain interrupt() calls inside nodes, and the bot resumes them with Command(resume=...) keyed by run ID as the thread ID. Because state is checkpointed after every node, a crash in rendering does not lose a paid-for research pass: /retry <run> re-invokes the graph and it continues from the failed node. I also added /redo <run> <stage>, which uses update_state(..., as_node=...) to rewind a run to an earlier stage and deletes only the cached files downstream of it. The cost is a dependency on LangGraph’s checkpoint semantics, which I covered with offline tests that drive the whole graph through both gates.

Forced tool calls for structured output, plus a validate-and-repair loop. Every structured answer is requested through a single submit tool whose input schema is the pydantic model (with $refs inlined because one provider rejects them). The alternative, parsing JSON from free text, broke on truncated or fenced output. When the answer is cut off, fails validation, or fails a semantic check such as “fewer than 10 sourced facts”, the loop re-asks with the specific error. There are fallbacks for models that reject forced tool_choice: auto tool choice with an instruction, then JSON mode.

Two-phase research so a cheaper model can own a stage. Phase A uses Anthropic’s server-side web search to write cited notes as plain text; phase B has whichever model is configured for that stage structure the notes into the schema. That lets me run outline and script on DeepSeek and keep research and critic on Claude, set per stage in config.yaml. Without a DeepSeek key everything falls back to Claude. The search notes are cached per run and per prompt, so a failed structuring pass does not pay for the search again.

Facts and numbers are separated from the model’s prose. A script sentence marked fact that cites no fact ID, or an ID not in the pack, is demoted to inference, and with always_review_inferences on, the critic’s reviewed inferences go to me. Chart data is never written by the model: ResearchDraft has no datasets field at all, and before a chart is placed behind a sentence, chart_matches checks that any figure the sentence quotes appears in the series within 3%. Otherwise the scene falls back to a stat card or ambient motion.

A critic from a different model family, told to disagree. The critic’s system prompt rewards catching errors, and the prompt assumes the writer is biased toward the thesis. In the current config the script comes from DeepSeek and the critic is Claude, so the check is not a model grading its own work. Only items that are not a clean “keep”, carry a flag, fall under the confidence threshold (0.75) or are inferences reach me, sorted by lowest confidence first; everything else passes without my attention.

Inside the code

The review gate. The critic’s ledger is filtered down to what needs a human, the graph pauses, and the decisions that come back from Telegram are applied to the script:

# dots/stages/critic.py: what reaches the human
for r in ledger.reviews:
    s = by_id.get(r.sentence_id)
    if not s:
        continue
    if (r.verdict != "keep" or r.flags or r.confidence < th
            or (always_inf and s.kind == "inference")):
        out.append({"sentence_id": s.id, "kind": s.kind, ...})
out.sort(key=lambda x: x["confidence"])

# dots/graph.py: the node pauses until the bot resumes it
def n_review(s: State) -> State:
    # … load script and ledger
    items = critic.items_for_review(sc, led)
    resp = (interrupt({"type": "review", "items": items, "script": s["script"]})
            if items else {"decisions": {}})
    decisions = resp.get("decisions", {})
    # … log each decision next to the critic's verdict in metrics.db
    final = critic.apply_decisions(sc, decisions, suggestions)

Logging the critic’s verdict next to my decision in metrics.db gives me data on how often I overrule the critic, which is the obvious way to tune the threshold later.

The repair loop that every LLM call goes through. The budget is checked before each attempt, and each retry carries the concrete reason the last answer was rejected:

for _ in range(retries + 2 if check else retries + 1):
    _budget_check(run_id, stage)
    data, stop = structured(run_id, stage, system, prompt,
                            schema_json, st["model"])
    if stop == "max_tokens":
        prompt = user + "\n\nYour previous answer was cut off ..."
        continue
    try:
        result = schema.model_validate(data)
    except ValidationError as e:
        prompt = (user + "\n\nYour previous answer failed validation:\n"
                  f"{str(e)[:1500]}\nPrevious answer:\n"
                  f"{json.dumps(data)[:6000]}\nFix it.")
        continue
    problem = check(result) if check else None
    if not problem:
        return result
    # … same repair prompt, carrying the semantic problem
raise RuntimeError(f"{stage}: could not get valid output: {last_err}")

Around this sit the other reliability pieces: an httpx wrapper with exponential backoff on 429/5xx that honours Retry-After; B-roll downloads that are rejected when the byte count does not match Content-Length; a stage cache keyed by a hash of each stage’s input, so voice is only regenerated (and paid for) when the narration text changes; review taps persisted to disk so a bot restart mid-review loses nothing; and a YouTube upload that records each finished part, so tapping the button again skips what is already uploaded. Every LLM call and TTS request is priced from a table in config and written to SQLite, which is what the per-run dollar cap reads.

Running it

pip install -r requirements.txt
cd render && npm install && npx remotion browser ensure && cd ..
cp .env.example .env        # fill in API keys and the allowed Telegram chat
python -m dots doctor       # checks env vars and makes live calls to each API
python -m dots sample       # renders a 12 s offline sample, no API calls
python -m pytest -q         # offline tests (28 pass)
python -m dots bot          # long-polling Telegram controller

What I’d do next

  • Give the critic web search. The README says the critic spot-checks claims on the web, but the code does not: it only checks the script against the research pack. A claim that is wrong in the pack itself can only be caught by me.
  • Enforce critic coverage. The prompt asks for a review of every inference, but the code only routes sentences the critic actually returned. A sentence the critic skipped is kept without reaching me. The fix is to treat any unreviewed inference as a review item.
  • Gate TTS on the budget. The cap is checked before LLM calls only. ElevenLabs spend is logged and counted, but voice generation does not check the cap first, and a single LLM call can overshoot it.
  • Score the prediction ledger. Approved scenarios are saved with odds, signposts and horizons, but nothing scores them yet.
  • Offload rendering. Rendering is local; the README mentions Cloud Run or Remotion Lambda as a fallback, but that is not built.
  • The bot is single-user by design (one allowed chat ID), and the graph runs in a thread from the bot process, so there is no queue for multiple concurrent runs.

Skills this shows

  • AI engineering: LangGraph graph with conditional edges, interrupt() gates and checkpoint rewind; schema-forced tool output with validate-and-repair; per-stage model routing across two providers; two-phase search-then-structure research.
  • Responsible AI: fact/inference tagging with automatic demotion of uncited facts, a separate-family adversarial critic, a human claim-review gate, chart numbers only from official APIs, synthetic-voice and disclaimer text added to every description.
  • Backend reliability: retries with backoff and Retry-After, truncation checks, input-hash stage caching, idempotent resumable uploads, error reporting from the bot with full tracebacks to a log file.
  • Cost control and observability: per-call cost accounting in SQLite, a per-run budget cap, cached paid searches, stage timings and review decisions recorded for later analysis.
  • Integration: Anthropic, DeepSeek (OpenAI-compatible), FRED, World Bank, ElevenLabs, Pexels/Pixabay, YouTube Data API with OAuth, Telegram, and a TypeScript Remotion renderer driven by JSON.
  • Testing: 28 offline pytest tests with fake API clients, including a full graph run through both gates and a /redo rewind.

The code is private; I’m happy to walk through it in an interview.