MacroMath
Architecture

AI and automation

MacroAI under the hood — dynamic context, action markers, structured output and deterministic normalization.

MacroAI is more than a chat: it's an AI nutritionist woven into the app's flow. All interactions with generative models pass through a single AiService, configured with a rigorous system prompt that fixes the persona of a certified clinical nutritionist.

Dynamic context injection

Before each request, the service consolidates in parallel (Future.wait) a structured object — the NutritionContext — that compiles:

  • The user's biometric profile.
  • Current targets.
  • Pantry inventory.
  • Food history from the last 7 days.
  • Weight progress.

The asynchronous design ensures that partial failures fetching peripheral data don't block the model's response.

Action markers

The model is instructed to embed text markers in its natural-language response. The presentation layer intercepts those delimiters and renders functional buttons:

MarkerInterface action
[DIET_JSON]Validates and saves a structured meal plan.
[MACROS_JSON]Updates the macro targets in the profile.
[SHOPPING_JSON]Adds products to the shopping list.
[FOOD_JSON]Adds the food and quantity to the diary.
[PANTRY_JSON]Updates pantry stock from receipts/photos.

Structured output and optimization

  • For operational tasks (inventory analysis, OCR), the model is forced into strict JSON mode with the thinking budget disabled (thinkingBudget: 0), minimizing latency.
  • The temperature is calibrated per task:
TaskTemperature
Deterministic (image analysis)0.15 – 0.20
Free-form conversation0.60 – 0.70

Deterministic normalization pipeline

Language models tend to produce small numerical hallucinations (the sum of macros diverges from the stated calorie value). To eliminate this, every response goes through a corrective algorithm in Dart, in four stages:

  1. Sanitization — removes items with null, missing or inconsistent data.
  2. Recalculation — applies the calorie formula: kcal = 4×P + 4×C + 9×F.
  3. Scaling — derives the factor between the generated total and the user's target (factor = target ÷ total), with a safety constraint in the range [0.6, 1.6] to prevent severe portion distortions.
  4. Rounding — adjusts to whole numbers or easy-to-read fractions.

The result: mathematical rigor independent of the generative model's fluctuations.

Vision — photos and receipts

The same service handles vision with Gemini:

  • Meals — recognizes foods in a photo and estimates macros.
  • Receipts — reads the shopping receipt (OCR) and extracts items into the pantry.

Limits and free plan

The free plan includes up to 5 AI conversations per day; heavier features are part of Premium. See Profile and settings.

AI suggestions are a support tool, not medical advice. For specific needs, consult a health professional.

On this page