AI Player Intelligence

An LLM pipeline that generates grammar metadata for a runtime engine. Part of the Box of Cards work.

TS · NestJS · Anthropic SDK · Claude · Prompt Engineering · NLP · Socket.IO · MongoDB · Mongoose · React · Redux Toolkit · RTK Query · Jest

Options stored in brackets

Deck editor — an answer card with bracketed options

A card stores its choices literally in brackets: [the,a,an] elephant, or a German [der,den,dem] Hausmeister. Alongside the text, a metadata array describes what each option means grammatically, so the engine can select the right one per bracket.

The metadata is bound to the card by a hash of its text, so an edited card is only ever paired with metadata that still matches it.

Grammatically correct card insertion

Winner screen — a card rendered in-place inside the sentence

In a fill-in-the-blank card game the answer is normally read off as a separate card. The goal here was to drop the winning card straight into the sentence's blank and make it fit grammatically — 'a elephant' becomes 'an elephant', a verb takes the correct tense, a German article declines to the case its verb requires.

It is implemented for both English and German — two grammars with partial overlap, each needing the grammatical role of every word on every card.

Two stages: ahead-of-time annotation, runtime selection

Architecture — Claude annotates, MongoDB stores, the rule engine selects

Metadata generation is a pipeline: whenever a sentence or answer is added or changed, the card text is sent to Claude (Sonnet 4.6), which labels every word with grammar — part of speech, case, tense, gender, and which case a verb requires. A follow-up Opus pass refines the harder sentences and answers. This annotation stage costs roughly $2–10 per deck and climbs with size. There is a cloud workflow for it, but since the API is pricey and it runs essentially for free on a personal Claude subscription, it is currently done locally on demand rather than in the cloud on every edit.

At game time there is no AI: a hand-written rule engine on the real-time server reads the stored labels and selects the correct word. That stage is fully deterministic — instant, free, and always the same answer.

The system prompt as grammar spec

The system prompt that encodes English and German grammar

This gets deeply grammatical — fully understanding it would mean studying English and German linguistics; without Claude it would have been a nightmare.

The entire LLM side is a single ~200-line system prompt. It defines every metadata field inline, enforces the position-ordering rule, and carries a large German block — article-case tables, two-way prepositions, verb government, weak nouns — plus about twenty worked examples.

It is versioned like code, so adding a field to the schema allows re-annotating a deck without reclassifying everything from scratch.

The answer head

IAnswerHead — the fixed text's character and rejection flags

Beyond the per-option tags, each card's fixed text carries a head — its part of speech, case, number, tense — plus a set of rejection flags.

A card that is a complete clause ('they are alive'), a saturated verb phrase ('hit me in the face'), a leading conjunction ('or juice'), or a proper noun after 'a' is removed before anything is selected.

The bracket group

IBracketGroup — the whole bracket, plus the ~30-signal tally

One flag applies to the bracket as a whole: massNoun, marking an uncountable noun so the engine suppresses its indefinite article.

Combined — the option fields, the head's character, the group's mass-noun bit — plus the slot's ten signals, the engine reasons over roughly thirty grammatical facts per card.

Compact storage with hash validation

Compact metadata strings, hash-gated against the card text

The verbose metadata is compressed into compact pipe-delimited strings before it reaches MongoDB, each one stamped with the first eight hex characters of a SHA-256 hash of the card text. At game load the engine re-hashes the card and only trusts metadata whose hash still matches.

If a card is edited, it drops its grammar rather than rendering the wrong word. This also fixed a bug where a stray carriage return from line-ending differences silently wiped a whole deck's grammar.

Step 1 · The fit-gate

Step 1 — the fit-gate: which cards qualify

Before any word is chosen, a gate removes cards that cannot work in the slot at all — complete clauses, leading conjunctions, gender or number mismatches. Each hand card is checked against 6 head flags and 10 slot roles; only the survivors continue.

This is what makes the bot play well: it plays the first card that genuinely fits, resolved inline in the same game loop the human players use — no separate bot process, no difficulty tiers.

Step 2 · Two passes

Step 2 — pick, learn the verb, re-decline

For each surviving card the engine iterates over the brackets twice. Pass one dispatches every bracket by category to one of six selectors — article, pronoun, verb form, determiner, adverb, noun variant.

It then determines whether a verb came out active and which case it governs, and pass two re-declines the articles and adjectives to match.

Step 3 · Surface fixes

Step 3 — final cleanups, then bracketChoices

A few final fixes run on the completed string: a doubled determiner collapses ('the a king' → 'a king'), a/an snaps to the following word, and a stray article is dropped before a predicate.

The result is bracketChoices — a list of indices the client substitutes into the sentence.

English and German declension

a/an correction and German case declension in action

In English: a/an is corrected to the following word, his/her/their is chosen from the subject's gender, and a doubled determiner like 'the a king' collapses to one. German is more demanding — article and adjective agreement by case, weak-noun endings ('Den Praktikant' becomes 'Den Praktikanten'), dative plurals, and verb government forcing the dative ('dem Hausmeister helfen', not 'den').

One card illustrates it: 'Bier [ ,trinken,trinke,getrunken,trinkt]' becomes '…wie man Bier trinkt' in a present clause, but 'Morgen werde ich Bier trinken' in the future. Same card, correct form.

The whole system came together in about two to three weeks of evenings and weekends; without agentic coding it would have taken several months.