Building consumer AI applications in healthcare and nutrition carries zero margin for error. If a creative writing assistant hallucinates a fictional fact, it is amusing. If a dietary planning AI hallucinates that peanuts are safe for a child with anaphylaxis, the consequence is catastrophic.
When Binary Froster engineered the proprietary AI Diet Planner, we architected a hybrid engine that decouples linguistic reasoning from mathematical nutrition calculations.
1. The Three-Phase Pipeline Architecture
Rather than asking an LLM to "generate a 2,200 calorie meal plan", which almost always results in incorrect calorie totals and hallucinated serving sizes, our engine executes three deterministic phases:
- Phase 1: Biometric & Restriction Extraction: An LLM parses freeform user input (e.g. "I work night shifts, I'm lactose intolerant, and I want 160g protein daily on a £50 weekly budget") into a strongly-typed schema validated with Zod.
- Phase 2: Deterministic Constraint Optimization: A mathematical solver queries our PostgreSQL USDA food database to construct candidate meal ingredient matrices that strictly satisfy the protein, carb, and fat targets to within ±2%.
- Phase 3: Culinary Synthesis: The LLM takes the mathematically verified ingredients and writes appetizing recipes with clear preparation instructions.
2. The Allergen Safety Verification Gate
Before any meal plan is saved or displayed to the user, an automated cryptographic safety gate verifies that none of the user's declared allergens appear anywhere in the ingredient taxonomy:
// lib/safety/allergenGate.ts - Zero-Tolerance Allergen Verification
export function verifyAllergenSafety(mealPlan: MealPlan, userAllergens: AllergenEnum[]): SafetyAuditResult {
const violations: string[] = [];
for (const meal of mealPlan.meals) {
for (const ingredient of meal.ingredients) {
// Check ingredient against canonical allergen taxonomy
const matchedAllergens = ingredient.allergens.filter(a => userAllergens.includes(a));
if (matchedAllergens.length > 0) {
violations.push(`CRITICAL: Meal "${meal.name}" contains ${matchedAllergens.join(', ')} via ingredient ${ingredient.name}`);
}
}
}
if (violations.length > 0) {
// Hard rejection - triggers fallback to clinically vetted static recipe database
return { passed: false, violations, action: 'REJECT_AND_FALLBACK' };
}
return { passed: true, violations: [], action: 'APPROVE' };
}
Key Takeaway for AI Founders
Do not rely on prompt engineering alone to guarantee safety in regulated domains. Use LLMs for what they excel at — parsing unstructured human intent and generating pleasant prose — and use deterministic code and relational databases for mathematical and clinical safety guarantees.
Ready to build intelligent health tech?
Binary Froster builds personalized AI-driven applications. Let's discuss your project.
Get a Free Consultation


