← Statpro dev log

Building a sports Q&A engine without an LLM

#ai-agents#data#product#web

A row of word blocks where three amber ones feed curved lines into a single amber diamond, and a dashed leftover block drops to an empty circle.

My first "ask a question" feature gave wrong answers, and I couldn't work out why. The stack behind it was too flexible. A wrong answer could come from anywhere in it, and nothing left a trail I could follow.

So I rebuilt the ask bar in Statpro, my sports stats site, around a parser with no language model inside. You type a question in plain English, and the answer is computed from the stats database. It parses a question in 0.05 milliseconds, and when it doesn't understand one, it says so.

Why I didn't reach for an LLM

I had three reasons, and the first carries the most weight. The question space is closed. Sports stat questions come in about a dozen shapes: who leads a category, how a player did, how two players compare, what a team's record is. A space that small can be written down.

Second, I can't audit a model's mistake. When a parser built from rules gets a question wrong, there's a rule or a lexicon entry to point at. When a model gets it wrong, I'm back where the old stack left me, with a wrong answer and no trail.

Third, cost and latency. A model call takes 200 to 500 milliseconds and a fraction of a cent. That's fine for one question and too much for a search bar that people type into all day.

Four pure stages

The parser is a closed grammar. Every phrase it understands is an explicit lexicon entry, and it runs as four pure functions in a row.

  • Normalize. "Who leads the MLB in HR's?" becomes "who leads the mlb in hrs".
  • Gazetteer. A gazetteer is a list of known names. This one finds players and teams by longest match, accepts a last name alone only when it's unambiguous, and never swallows a reserved word into a name.
  • Lexicon. Per-league metric synonyms, so homers, dingers, long balls and hrs all resolve to home runs. It also holds the fragments used to phrase an answer.
  • Intent matcher. Rules tried in a fixed priority order, which return either a typed intent or a typed refusal.
ONE QUESTION, FOUR PURE STAGES Normalize Gazetteer Lexicon Intent matcher Typed refusal, logged Typed intent HR's → hrs longest-match names dingers → home runs refusal reasons: ambiguous-entity, no-parse, ambiguous-league 0.05 ms per parse · 57 golden questions · bench gate at 10 ms
Each stage is a pure function, so every exit is either an answerable intent or a refusal with a reason.

The matcher's priority order is the whole dispatch, and it reads like the product spec:

function matchIntent(tagged, league): MuseParseOutcome | null {
  return (
    compareIntent(tagged, league) ??   // "Judge vs Ohtani"
    leadersIntent(tagged, league) ??   // "Who leads MLB in home runs?"
    playerIntent(tagged, league) ??    // "Judge stats"
    teamIntent(tagged, league)         // "Yankees record" / "next game"
  );
}

Refusing beats half-answering

The part I'm proudest of runs before any intent is matched. After tagging, if content words are left over that no stage consumed, the parser refuses. "Who leads the MLB in dingers and RBIs" (runs batted in) has two metrics competing for one slot. The parser doesn't pick one and hope.

The gate is a few lines, and all of the strictness lives in it:

function preflightRefusal(tagged: TaggedQuestion): MuseRefusal | null {
  if (tagged.ambiguousEntity && !hasEntities(tagged)) {
    return refusal("ambiguous-entity", tagged.unmatched);
  }
  if (tagged.unmatched.length > 1) return refusal("no-parse", tagged.unmatched);
  return null;
}

Every refusal carries a typed reason: ambiguous-entity, no-parse or ambiguous-league. Every question is logged along with the words that went unmatched, so the lexicon grows from real misses.

What a closed grammar buys in tests

Because each stage is pure, the whole parser is testable with plain inputs and outputs. A golden corpus, a fixed set of 57 example questions with their expected results (refusals included), pins the behaviour. A benchmark check fails the build if a parse takes longer than 10 milliseconds, which leaves plenty of room above 0.05. The package has zero runtime dependencies and 71 tests in total.

What shipped, and the bug I caught live

The ask bar replaced the search bar. It handles six families of question: leaders, player stat lines, player comparisons, team records, team schedules and champions.

Champions gave me the one bug I caught live. A championship year maps to the calendar year the season ends in, so the "2016 NBA Finals" belong to the 2015-16 Cavaliers. My first version had that backwards. The wrong answer came from one rule, so the fix was one rule.

Write the question space down first

If the set of questions your users ask is small enough to list, list it before you pay a model to guess. You get speed, a refusal you can read, and bugs with an address.

This is the build log for Statpro (statpro.io) · All entries · Weekly essays