← Back to Blog

Article

How I Built an AI Pantry with Firebase AI Logic and Gemini

How TinyRecipe generates fast, structured recipes from the ingredients already in your fridge or pantry while keeping costs low.

TinyRecipe AI Pantry powered by 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 the ingredients currently sitting in their fridge or pantry.

Adding generative AI to a mobile client can quickly spiral into slow response times, unpredictable JSON payloads, and rising API costs when it is not architected carefully. Here is how I integrated Firebase AI Logic, powered by Gemini on Vertex AI, 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 and weird pairings: it might suggest turning 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 leads to fragile, client-side failures.
  3. High token consumption: unbounded responses burn through the token budget rapidly for multi-step instructions.

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

1. System Prompt Guardrails and JSON Schema Enforcement

Rather than asking the model for a good recipe, the system prompt forces it 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"
    }
  ]
}

Two guardrails made the feature much more useful in practice:

  • The basic staples exemption: users should not have to check off salt, pepper, tap water, or cooking oil in their pantry list. The system prompt explicitly allows those basics, which keeps the selection UI frictionless.
  • Strict ingredient isolation: the model prioritizes ingredients marked as available in the user's digital pantry before suggesting external additions.

2. Client-Side Optimizations: Frictionless UX

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: before sending a Firebase AI Logic request, the app hashes the selected pantry array. When a user requests a recipe with the same core ingredients within a short window, TinyRecipe serves the cached result instantly without another API call.

3. Lessons Learned and Next Steps

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

  • sub-two-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.