← Blog

Teach the bot from a PDF, without burning tokens

September 2, 20262 min read

In How michi-chat works I described the knowledge base: markdown documents the assistant searches when a visitor asks a factual question. The honest gap was getting facts INTO it. Real businesses keep their facts in PDFs - a handbook, a price sheet, a flyer - and until this week the answer was "open the file and copy the text out by hand."

michi-chat 0.2.18 adds Import from a PDF, and the whole feature is built around one principle: the free work happens first, and the paid work shows its price before it runs.

The free analysis step: junk stripped, a token estimate, targeted suggestions

The staircase

  1. Analyze - free, no model. Reading text out of a PDF is plain parsing. Repeated headers and footers, page numbers, and table-of-contents dot lines are stripped deterministically - no AI needed for mechanical junk. You also get targeted warnings: a scanned PDF (photos of pages, nothing machine-readable), table-heavy content (prices belong in live tools, not in a knowledge base), or a file big enough to be worth splitting.
  2. A prediction, not a surprise. Token cost is driven by text length, and after extraction the length is known. So the button literally says what it will cost: Tidy with AI (~581 tokens) - with a plain-words verdict next to it ("cheap, about one conversation's worth"). Delete paragraphs in the text box and the estimate shrinks live. A green estimate means go ahead; the gauge exists to catch the accidental 300-page catalog, not to make small imports feel expensive.
  3. The one paid step - optional. The model reorganizes the surviving text into short ## sections, the shape retrieval likes. It invents nothing, and afterwards the UI reports the tokens actually used next to what it estimated, so the estimate has to earn your trust with every run.
  4. Save & embed. From there it is a normal document: chunked, embedded, searchable.

Why bother, if tokens are cheap?

Because tokens were never the real cost. A dirty PDF puts junk chunks into the knowledge base, and junk chunks get retrieved - every page number that lands in a chunk is a slightly worse answer, forever. The cleanup step is not about saving fractions of a cent; it is about what the assistant will say next week.

The docs walk through the whole flow, and the code is open - src/lib/pdf-import.ts is the deterministic triage, small enough to read with coffee.