OptiFood AI: Nutrition Tracking Engine & Anti-Hallucination by Serhii LukashOptiFood AI: Nutrition Tracking Engine & Anti-Hallucination by Serhii Lukash

OptiFood AI: Nutrition Tracking Engine & Anti-Hallucination

Serhii Lukash

Serhii Lukash

The Problem

People who track nutrition face two brutal realities: manually logging food takes 10+ minutes per meal, and generic AI nutrition apps hallucinate calorie data — giving you 200 kcal for a meal that's actually 800. For anyone managing weight, fitness, or medical conditions, that's not an inconvenience — it's dangerous.
The Cost: Missed goals, broken trust in AI tools, and zero behavior change.

What I Built

A production-grade Telegram bot that logs any meal in seconds — text description, voice message, photo, or barcode scan — powered by a five-layer AI recognition pipeline with a built-in anti-hallucination validator. The system doesn't just identify food; it mathematically verifies every nutritional value before writing it to your diary.
OptiFood AI Demo

Business Impact & ROI

Input modalities: 4 (text, voice, photo, barcode)
AI pipeline layers: 5 (cache → fuzzy → vector → LLM → background index)
Validation method: Atwater formula + physical nutrient limits
Test coverage: 248 async tests across all layers
Worker memory footprint: 50–80 MB per container
API response latency: < 500ms (immediate task offload)
Stats
Stats

How It Works (The Value Pipeline)

1. Five-Layer Recognition — Speed Without Hallucination

Every food query travels through an explicit fallback chain: Redis cache hit (~0ms) → Rapidfuzz local DB match (~2–5ms) → Pinecone semantic vector search (~50–100ms) → GPT-4o structured JSON fallback (~1–3s) → automatic background indexing back into Redis + Pinecone. The system always finds an answer and always learns from it.

2. Anti-Hallucination Validator

Before any nutrition data reaches your diary, it passes two checks: physical limits (protein + fat + carbs ≤ 100g/100g; calories ≤ 950 kcal/100g) and the Atwater formula cross-check. If the deviation between reported calories and the calculated formula exceeds 30%, the system auto-recalculates. No phantom macros, ever.
System Architecture
System Architecture

3. Multi-Modal Input Processing

Voice messages are transcribed via OpenAI Whisper. Photos are analyzed via GPT Vision with OCR fallback. Barcodes are decoded via zxing-cpp and cross-referenced with the Open Food Facts API. Text is pre-parsed by a custom NLP pipeline (pymorphy3 + NLTK) that normalizes quantities, units, and detects complex multi-item meals.
Text Input
Text Input
Voice Input
Voice Input
Photo Recognition
Photo Recognition
Barcode Scanning
Barcode Scanning

4. Intelligent AI Mentor

Beyond logging, the system analyzes daily macro patterns and delivers contextual coaching — not generic advice. GPT generates personalized recommendations based on the user's actual intake history, calorie goals, and trend data across 7 and 30-day windows.
Settings & Personalization
Settings & Personalization

Technical Foundation

API Architecture: Async Python 3.13, FastAPI + aiogram 3.x webhook, SQLAlchemy 2.0 asyncpg
Task Management: TaskIQ async worker pool over Redis Pub/Sub — no polling, no event loop blocking, 50–80 MB memory per worker
AI Stack: OpenAI GPT-4o (structured output) + Whisper + Embeddings, all routed via litellm
Vector Search: Pinecone with 30-day embedding cache via httpx async client
Quality Assurance: 248 async tests, pytest-asyncio, full coverage across presentation, domain, and infrastructure layers
Observability: Sentry initialized independently in both API and worker processes; structured logging throughout
Architecture Pattern: Port/Adapter (DIP), Unit of Work, Repository, Orchestrator Facade — zero cross-layer imports enforced via pre-commit hooks

Who Benefits From This System

Fitness & Health Apps: Embed accurate, hallucination-proof food recognition as a core feature without building the AI stack from scratch.
Nutrition Platforms: Replace manual food databases with an adaptive pipeline that learns from every query and self-indexes new entries.
Individual Users: Log any meal in any format — text, voice, photo, barcode — in under 5 seconds, with mathematically verified macros.
Like this project

Posted Jun 5, 2026

Telegram nutrition bot with a 5-layer AI pipeline (cache → fuzzy → vector → GPT-4o → auto-index) and Atwater-based anti-hallucination macro validator.