Why a Single Few-Shot Example Made Our LLM Hallucinate User History — And 3 Prompt & Regex Bugs We Fixed
When you ship an LLM-powered product to real users, the bugs that destroy trust rarely look like 500 errors. They look like fluent, confident sentences that quietly fabricate a fact the user never mentioned—or misroute a
When you ship an LLM-powered product to real users, the bugs that destroy trust rarely look like 500 errors. They look like fluent, confident sentences that quietly fabricate a fact the user never mentioned—or misroute a nuanced question into the wrong UX flow.
While expanding our interactive tarot app with focused relationship pages—including our will he contact me tarot and does he miss me tarot flows—we ran an 11-scenario live E2E pass alongside 256 adversarial unit tests. That review exposed four subtle bugs in our TypeScript routing and LLM prompting stack. None of them threw an exception, yet every single one hurt output quality.
Here is what broke, why it happened, and the exact fixes that held up in production.
Why Did Our LLM Keep Inventing a "Warm Message" the User Never Mentioned?
During E2E testing on our silence-focused reading flow, we sent a minimal prompt with zero backstory:
"Will he contact me again after this silence?"
Our model opened with:
"The pull you're feeling is between a short, warm contact that came out of nowhere and a silence that followed it..."
Wait—what warm contact? The user never mentioned receiving a message. In any personalized AI reading or coaching app, asserting an unmentioned concrete event immediately breaks credibility if it never happened.
The Root Cause: Few-Shot Example Leakage
We had explicitly written "Never invent events or backstory they did not mention" in our system prompt. So why did the model ignore it?
Because ten lines earlier, we had included this "good style" few-shot example to encourage emotionally specific openings:
Example GOOD opening:
"That brief text wasn't random, and the silence right after feels like someone cracking a door an inch..."
Even when a rule forbids inventing facts, models imitate concrete nouns inside positive few-shot examples far more strongly than they obey abstract negative instructions. Because our example started with "That brief text", the model kept injecting a phantom "warm contact" or "warm message" whenever the user prompt only mentioned "silence".
The Fix: Event-Free Examples + Inclusive Framing
We replaced every concrete unmentioned event in our few-shot examples with purely emotional tension and added an explicit zero-invented-event contract:
// Before (leaked phantom "brief text" into unrelated silence questions):
// "That brief text wasn't random, and the silence right after feels like..."
// After (zero unmentioned events, high emotional specificity):
const ZERO_INVENTED_EVENT_RULE = `
- ZERO-INVENTED-EVENT RULE: Never assert a specific past event (a brief text,
warm message, phone call, meetup, or timeframe) unless the user wrote it.
If the question is brief, stay strictly on the tension they named or use
inclusive phrasing ("whether this quiet followed a warm stretch or drifted in").
- Example GOOD opening:
"The hardest part of this silence isn't just not hearing from him — it's
feeling like the door stayed cracked an inch without ever giving you a straight answer."
`;
Re-running the exact same bare silence scenarios eliminated the fabricated "warm message" completely while keeping the opening sharp and specific.
Why Did Banning "I Cannot Read Minds" Make the Model Output That Exact Disclaimer?
On follow-up questions like "What does he regret most about how things ended?", the model frequently opened Sentence 1 with a cold refusal:
"The cards can't reveal his private regrets, and I can't see inside his head. What they do show is..."
Our first patch tried to forbid those exact phrases and explain the fallback:
STRICTLY FORBID "I can't see inside his head" or "I cannot read his mind".
If you note that tarot reads patterns rather than verbatim thoughts, tuck that boundary in the middle.
That barely cut the rate in half! On our next live test on the follow-up lane, the model still opened with "The cards can't reveal his private regrets, but...".
The Root Cause: The Pink-Elephant Effect in System Prompts
Repeating the exact phrasing you do not want—especially paired with an "if you note that..." escape hatch—keeps those disclaimer concepts active in the model's context window. Small fast models like Haiku treat "if you note X" as permission to keep outputting a paraphrased version of X.
The Fix: Delete the Disclaimer Priming Completely
We deleted every mention of mind-reading and limitations from the follow-up system prompt, replacing it with a purely positive structural instruction:
const FOLLOWUP_OPENER_RULE = `
Cut dry robotic openings ("The cards show...") and skip all meta-disclaimers
about tarot's limits. Always open Sentence 1 directly with the sharpest
emotional or behavioral truth the drawn cards expose for what they just asked —
vary your phrasing naturally and anchor straight into the exact drawn card and position.
`;
After removing the negative priming, 4 out of 4 live follow-up tests opened straight into grounded card insights with zero cold-water disclaimers.
How Did 256 Adversarial Tests Catch Two TypeScript Intent-Routing Regex Bugs?
Our backend classifies user questions deterministically in TypeScript before calling the LLM so each intent line gets its own prompt, follow-up suggestions, and safety guardrails (for instance, keeping trivial yes/no queries completely free of upsells).
Everything passed our standard 200+ happy-path tests—until an adversarial sweep flagged 28 failures across two real user phrasings:
-
Precedence bug:
"Is he cheating on me with his ex?"routed toex_reconciliationinstead ofcheating_trustbecause our bare\bex\bmatcher ran before our cheating matcher. -
Morphology & punctuation bug:
"After our break-up, will he contact me?"and"We have broken up. Does he miss me?"missedbreak\s*up|broke\s+upcompletely because of the hyphen (break-up) and past participle (broken up).
The Fix: Explicit Precedence + Override Guard + Flexible Word Forms
const EX_QUESTION_RE =
/\b(ex|break[\s-]*up|brok(?:e|en)\s+up|reconcil\w*|get\s+back\s+together|come\s+back|no\s+contact)\b/i;
const CHEATING_QUESTION_RE =
/\b(cheat(?:ing|ed)?|unfaithful|affair|infidelity|seeing\s+someone\s+else)\b/i;
// Keep "My ex cheated on me, will he come back?" in ex_reconciliation,
// while routing "Is he cheating on me with his ex?" to cheating_trust.
const EX_RECONCILE_OVERRIDE_RE =
/\b(come\s+back|coming\s+back|get\s+back\s+together|reconcil\w*|return\s+to\s+me)\b/i;
export function classifyLoveQuestion(q: string) {
if (CHEATING_QUESTION_RE.test(q) && !EX_RECONCILE_OVERRIDE_RE.test(q)) {
return 'cheating_trust';
}
if (EX_QUESTION_RE.test(q)) {
return 'ex_reconciliation';
}
return 'ambiguous_crush';
}
That four-line precedence guard separates "Is he cheating on me with his ex?" (cheating_trust) from "My ex cheated on me, will he come back?" (ex_reconciliation) cleanly with zero extra LLM calls.
Why Did Naive Sentence Splitting Truncate Teasers Mid-Quote?
To display a clean one-sentence preview for each section of a structured LLM report, we had a helper that extracted the first sentence with a classic regex:
// Buggy: splits right at the '?' inside quoted speech!
const sentences = cleaned.match(/[^.!?]+[.!?]/g);
In live testing, the model produced:
Silence protects you now, because texting carries the question "do you still want me?" before he is ready.
Our helper sliced right after me?, rendering an awkward broken fragment with an unclosed quote:
Silence protects you now, because texting carries the question "do you still want me?
The Fix: Quote-Aware Sentence Matching
Matching quoted spans atomically before checking sentence-ending punctuation fixed the truncation in one line:
export function firstSentence(text: string): string {
const cleaned = text.trim().replace(/\s+/g, ' ');
const sentences = cleaned.match(/(?:[^.!?"“”]|"[^"]*"|“[^”]*”)+[.!?]["”']?/g);
if (!sentences || sentences.length === 0) {
return cleaned.slice(0, 160).trim();
}
const first = sentences[0].trim();
if (first.length < 60 && sentences.length > 1) {
const combined = `${first} ${sentences[1].trim()}`;
return combined.length <= 240 ? combined : first;
}
return first;
}
Frequently Asked Questions
How do you stop an LLM from hallucinating details that appear in few-shot examples?
Audit every positive few-shot example in your system prompt and strip out domain nouns you don't want repeated (such as specific messages, dates, or events). Models treat concrete details inside few-shot examples as templates to copy, even when a separate rule tells them not to invent backstory.
Why do negative prompt rules ("never say X") sometimes increase the chance of X appearing?
Spelling out the exact phrase you want to avoid—especially alongside conditional phrasing like "if you mention X, put it at the end"—primes those tokens in the model's attention window. Replacing negative rules with positive structural examples and removing disclaimer vocabulary altogether works much more reliably.
Should intent classification use deterministic TypeScript regexes or an LLM router?
For latency-sensitive routes and strict safety/paywall boundaries, deterministic TypeScript matchers run in microseconds, cost $0, and are 100% unit-testable against adversarial edge cases. The key is testing compound phrasings (like overlapping keywords and hyphenated variants) rather than only single-intent happy paths.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.