● ● ● ● ● ● ○ ○ ○   Lesson 6 of 9

Build a docs Skill · A brown-bag

Raw counts lie

Your search works. So why does a giant changelog keep beating the page you actually need?

Why · the problem

We shipped TF-IDF in Lesson 5. It passes the body-only evals. Ship it, done — right?

Then someone searches machine id tbot. The right answer is the focused tbot reference page. But look what ranks near the top: the Teleport Changelog — a single, enormous page that lists every change in every release. It is almost never the page you want. Yet it keeps muscling in above the pages that are.

Why? Because our score is weight = TF × IDF, summed over the query's words — and a huge page contains every word many times. The Changelog has 2,891 terms. Of course it racks up a big total. It wins by size, not by relevance.

"the multi-hundred-page Changelog (2,891 terms) outranks the focused tbot reference for machine id tbot." — CORPUS.md, Act E (the review)

Here is the twist that makes this the most important lesson in the course. The agent that built TF-IDF knew about this risk. It even watched the Changelog rank high and talked itself into it:

"The changelog shows up a lot (it's a very long page with many terms), but the weights naturally push the targeted pages to the top." — CORPUS.md, Act D (the agent, eyeballing the output)

A capable agent made a reasonable call and eyeballed the result as fine. It wasn't. That's the lesson: a good guess is still a guess.

Eyeballs aren't evals The flaw wasn't caught by a smarter model. It was caught by a measurement — running the query and reading the ranks. Frequency is not relevance, and the only way to know your score is fooled is to score it and look.

Your turn — propose the fix (before you read on).

A page wins because it's long: every word appears many times, so its total is bloated. What would you change about the score to stop big pages from dominating?

Reveal the idea

Two fixes, and you want both. (1) Stop rewarding repetition without limit — the 20th time a word appears shouldn't count as much as the 1st. (2) Divide by page length — judge a word's density, not its raw count, so a long page gets no free boost. Together, those two ideas are BM25.

What · the solution approach

The fix is a small upgrade to TF-IDF called BM25. Same family, still just arithmetic — but it adds length normalization. It has two knobs:

That second knob is the whole cure for the Changelog. Its size stops paying.

Frequency ≠ density ≠ relevance Raw counts reward length. BM25 asks the better question: how dense is this word here, with diminishing returns for repetition? That's much closer to what "relevant" actually means.
How · the mechanics

Here's the score, with each piece labeled. You don't need to compute it — just see what k1 and b do:

            IDF  ×  TF × (k1 + 1)
score =  ─────────────────────────────────────
         TF + k1 × (1 − b + b × doc_len / avg_doc_len)
                                  └──────────────────┘
                          this is the new part: page length ÷ average
                          long page → bigger denominator → lower score

And the payoff, same query, before and after:

query: "machine id tbot" RAW TF×IDF (no length norm) BM25 (k1 = 1.2, b = 0.75) ───────────────────────────── ───────────────────────────── Deploy tbot ............ 27.7 top 4 = all Machine / Getting Started ........ 33.9 Workload Identity pages Changelog .............. 38.1 ◀ Changelog ......... gone (long page wins by size) (size no longer pays)
Length normalization dethrones the Changelog; the focused pages rise to the top.
"for machine id tbot the top 4 are now all Machine/Workload Identity pages; the Changelog is gone from the top." — CORPUS.md, Act G (BM25 lands)

One more thing, and it's the reason this costs nothing extra: we compute the BM25 weights at build time and bake them into the index (it just grows one number, the average page length). The shipped skill still does only lookups and addition — no new work at query time. Same shippable file, smarter ranking.

🔴 → 🟢   The regression we locked in

Red: raw TF × IDF let the 2,891-term Changelog outrank the focused tbot page — measured live (for tbot github actions deploy the Changelog hit #2 at 38.1, beating "Deploy tbot" at 27.7).

Green: BM25 (k1=1.2, b=0.75) → top results all become Machine/Workload Identity pages, Changelog gone. The body-only suite (evals 9–13) stays in place to confirm the ranking holds after the change.

That's Agile iteration again: title → body → ranked by density, not size. Each short round — built, tested, improved — makes the data the agent grounds on more accurate.

But now try searching for mysqld, or pg_hba.conf. The page you need exists… and the index can't find it at all. Not ranked low — completely absent. What did we forget to index?

That's Lesson 7.

Quick self-check (recall before you peek):

1. Why did the Changelog outrank the right page under raw TF × IDF?

It's huge — every word appears many times, so its summed score is bloated. It won by length, not relevance. Frequency ≠ relevance.

2. What do BM25's two knobs, k1 and b, each do?

k1 saturates repetition (the 20th hit adds little); b normalizes by page length (a long page gets no free boost). b is what fixes the Changelog.

3. Why was the flaw caught — and what's the lesson?

By a measurement (running the query, reading the ranks), not a smarter model. Eyeballs aren't evals — a capable agent's reasonable guess was still wrong.