How I Built an AI Pantry Recipe Generator Using Firebase AI Logic

As an indie app developer managing a portfolio of local-first mobile apps, my core philosophy has always been speed, utility, and cost efficiency. With TinyRecipe, the goal was simple: help users eliminate food waste by generating practical, step-by-step recipes based strictly on ingredients currently sitting in their fridge or pantry.

Adding generative AI capabilities to a mobile client can quickly spiral into slow response times, unpredictable JSON payloads, and spiraling API costs if not architected carefully. Here is a breakdown of how I integrated Firebase AI Logic (Gemini on Vertex AI) into TinyRecipe to deliver fast, structured recipe generation while keeping running costs minimal.

The Core Challenge: Restraining the LLM

If you give an LLM open-ended freedom to construct a recipe, three problems immediately emerge:

  1. Hallucinations & Weird Pairings: It might suggest combining olive oil, soy sauce, and a lemon into a "soup."

  2. Schema Inconsistency: Parsing free-form text or unstructured markdown back into native mobile UI components causes constant client-side runtime crashes.

  3. High Token Consumption: Unbounded LLM responses drain your token budget rapidly on multi-step instructions.

To solve this, I leveraged Firebase's backend integrations with Vertex AI to enforce strict system instructions and structured output parsing.

1. System Prompt Guardrails & JSON Schema Enforcements

Rather than asking the model for "a good recipe," the system prompt forces the model into a strict JSON schema contract.

{
  "title": "String",
  "prep_time_minutes": "Number",
  "difficulty": "Easy | Medium | Hard",
  "matched_pantry_items": ["Array of Strings"],
  "missing_staples_required": ["Array of Strings"],
  "instructions": [
    {
      "step_number": 1,
      "text": "String"
    }
  ]
}

Key Guardrails Implemented:

  • The "Basic Staples" Exemption: Users shouldn't have to check off salt, pepper, tap water, or cooking oil in their pantry list. The system prompt explicitly instructs the AI that basic kitchen staples can be assumed, keeping the client-side selection UI frictionless.

  • Strict Ingredient Isolation: The model is constrained to prioritize ingredients marked as "Available" in the user's digital pantry before suggesting external additions.

2. Client-Side Optimizations: Frictionless UX

From a UX perspective, nobody wants to type out "250g of diced chicken breast" on a mobile keyboard in the kitchen.

[ User Taps Pantry Tags ] ➔ [ Client Assembles Array ] ➔ [ Firebase AI Request ] ➔ [ Native UI Render ]

  • Quick-Select Category Tags: The UI presents categorized, quick-toggle tags for common fridge and pantry items.

  • Local Caching Layer: Before firing a request to Firebase AI Logic, the app hashes the selected pantry array. If a user requests a recipe with the exact same core ingredients within a short window, TinyRecipe serves the cached result instantly without making an expensive API call.

3. Lessons Learned & Next Steps

Integrating Firebase AI Logic made scaling generative AI features remarkably straightforward for a solo developer. By enforcing strict JSON schemas at the model boundary and handling state locally, TinyRecipe achieves:

  • Sub-2 second generation times

  • Predictable client-side rendering without parsing failures

  • Negligible API token waste

The AI Pantry feature is now live on both iOS and Android!

2 views

Add a comment

Replies

Be the first to comment