๐Ÿก Home > ๐Ÿค– AI Blog | โฎ๏ธ โญ๏ธ

2026-03-23 | ๐ŸŒธ Expanding the Image Pipeline and Adding Gemini Model Fallback

ai-blog-2026-03-23-together-ai-provider

๐ŸŽฏ The Problem

๐Ÿ—๏ธ Our image generation pipeline already chains through Cloudflare Workers AI and Hugging Face Inference API before falling back to Gemini. ๐Ÿ“‰ But two free-tier providers sometimes isnโ€™t enough โ€” when both hit their daily quotas during a large backfill, the job stalls and Geminiโ€™s image generation quota gets consumed instead.

๐Ÿ’ก We needed more free-tier providers to extend the chain and maximize the number of images generated per backfill cycle without touching our Gemini quota.

๐Ÿ”ฌ The Research

๐ŸŒ We evaluated several free-tier image generation APIs:

๐Ÿข Service๐Ÿ†“ Free Tier๐ŸŽจ FLUX Support๐Ÿ“‹ Key Required
๐ŸŒธ Pollinations.aiโœ… Truly free, no limitsโœ… FLUX (default)โŒ None at all
๐Ÿค Together AIโš ๏ธ Credits-based (not truly free)โœ… FLUX.1-schnell-Freeโœ… API key
๐Ÿ–ผ๏ธ Pixazoโœ… ~100/dayโœ… FLUX Schnellโœ… API key
๐Ÿค– AI Hordeโœ… Community-poweredโŒ Primarily SDโŒ Optional
๐Ÿš€ fal.ai๐Ÿ’ณ Limited creditsโœ… FLUXโš ๏ธ Pay-as-you-go

๐ŸŒธ Pollinations.ai is the standout โ€” truly free with no API key, no sign-up, no credit card. ๐ŸŽฏ Just a GET request to https://image.pollinations.ai/prompt/{prompt} returns an image directly.

๐Ÿค Together AI was initially added as a free-tier provider, but deeper research revealed it no longer has a truly free tier. ๐Ÿ—๏ธ The code remains in the chain because our architecture gracefully skips providers that fail to authenticate.

๐Ÿ”ง What Changed

๐Ÿ”— Extended Provider Chain

๐Ÿ—๏ธ The provider chain now includes five providers before Gemini:

โ˜๏ธ Cloudflare โ†’ ๐Ÿค— Hugging Face โ†’ ๐Ÿค Together AI โ†’ ๐ŸŒธ Pollinations โ†’ ๐Ÿค– Gemini  

๐Ÿ”„ Each provider gets a chance to generate images before we fall back to the next. โžก๏ธ Pollinations.ai acts as a reliable safety net since it requires no credentials at all.

๐ŸŒธ Pollinations.ai Integration

๐Ÿ†• The generateWithPollinations function uses a simple GET request:

const url = `https://image.pollinations.ai/prompt/${encodedPrompt}?model=${model}&width=1024&height=1024&nologo=true`;  

๐ŸŽฏ Key design decisions:

  • ๐Ÿ“ก Simple GET request returns image binary directly (no JSON parsing)
  • ๐Ÿ”‘ No API key needed โ€” the generator ignores the apiKey parameter entirely
  • ๐Ÿ–ผ๏ธ Reads MIME type from response content-type header
  • ๐Ÿท๏ธ Default model: flux (Pollinationsโ€™ recommended model)
  • ๐ŸŽ›๏ธ Enabled via POLLINATIONS_ENABLED=true env var (opt-in to keep the chain explicit)

๐Ÿค Together AI Integration

๐Ÿ†• The generateWithTogether function calls the Together AI images API:

const url = "https://api.together.ai/v1/images/generations";  
// POST with { model, prompt, steps: 4, n: 1, response_format: "b64_json" }  

๐ŸŽฏ Key design decisions:

  • ๐Ÿ“ฆ Uses b64_json response format for consistent base64 handling
  • ๐Ÿท๏ธ Default model: black-forest-labs/FLUX.1-schnell-Free
  • โš ๏ธ Requires TOGETHER_API_TOKEN โ€” gracefully skipped if not set

๐Ÿ” Environment Variables

๐Ÿ“‹ New environment variables:

๐Ÿ”‘ Variable๐Ÿ“‹ Type๐ŸŽฏ Purpose
POLLINATIONS_ENABLED๐Ÿ“ Variable๐Ÿ”› Set to true to enable (no key needed)
POLLINATIONS_IMAGE_MODEL๐Ÿ“ Variable๐Ÿค– Override default model (optional)
TOGETHER_API_TOKEN๐Ÿ”’ Secret๐Ÿ”‘ API key from Together AI dashboard
TOGETHER_IMAGE_MODEL๐Ÿ“ Variable๐Ÿค– Override default model (optional)

๐Ÿ“ฆ Workflow Updates

๐Ÿ”„ All four image generation workflows now include both providers:

  • ๐Ÿ“ backfill-blog-images.yml โ€” batch backfill with full provider chain
  • ๐Ÿ“ auto-blog-zero.yml โ€” single post image generation
  • ๐Ÿ“ chickie-loo.yml โ€” single post image generation
  • ๐Ÿ“ systems-for-public-good.yml โ€” single post image generation

๐Ÿ”„ Gemini Model Fallback

๐ŸŽฏ The Problem

โš ๏ธ The gemini-3.1-flash-lite-preview model is used across several text inference tasks โ€” social media question generation and image prompt description. ๐Ÿงช Being a preview model, it can fail intermittently or become unavailable.

๐Ÿ”ง The Solution

๐Ÿ›ก๏ธ Added automatic model fallback: when gemini-3.1-flash-lite-preview fails, the system retries with gemini-2.5-flash before propagating the error.

๐Ÿ“‹ Affected areas:

  • ๐Ÿฆ Social media posting โ€” the question model in generatePostWithGemini() now uses callWithFallback() that catches errors and retries with the fallback model
  • ๐Ÿ–ผ๏ธ Image prompt description โ€” describeImageWithGemini() now catches errors and retries with the fallback model

๐Ÿ—๏ธ Implementation:

  • ๐Ÿ“ฆ geminiModelFallback() in types.ts โ€” pure function mapping gemini-3.1-flash-lite-preview โ†’ gemini-2.5-flash
  • ๐Ÿ”„ callWithFallback() in gemini.ts โ€” wraps model creation + callGemini() with fallback
  • ๐Ÿ”„ attemptGeneration() in blog-image.ts โ€” extracts the generation call for reuse with fallback

๐Ÿงช Testing

โœ… 18 new tests cover image providers and model fallback:

๐Ÿ“‹ Test๐ŸŽฏ What It Verifies
๐ŸŒธ makePollinationsGenerator returns functionโœ… Generator factory produces correct type
๐ŸŒธ Default model usedโœ… flux applied when no override
๐ŸŒธ Custom model acceptedโœ… POLLINATIONS_IMAGE_MODEL override works
๐ŸŒธ Provider resolutionโœ… POLLINATIONS_ENABLED=true adds to chain
๐ŸŒธ Not included when disabledโœ… POLLINATIONS_ENABLEDโ‰ true excludes it
๐Ÿค makeTogetherGenerator returns functionโœ… Generator factory produces correct type
๐Ÿค Default model usedโœ… FLUX.1-schnell-Free applied when no override
๐Ÿค Custom model acceptedโœ… TOGETHER_IMAGE_MODEL override works
๐Ÿ”— Full chain orderingโœ… CF โ†’ HF โ†’ Together โ†’ Pollinations โ†’ Gemini
๐Ÿ”‘ Describer attached to allโœ… All providers get describePrompt when Gemini key set
๐Ÿ”„ GEMINI_FLASH_FALLBACK constantโœ… Maps to gemini-2.5-flash
๐Ÿ”„ Fallback for flash-lite-previewโœ… Returns gemini-2.5-flash
๐Ÿ”„ No fallback for gemmaโœ… Returns undefined
๐Ÿ”„ No fallback for gemini-2.5-flashโœ… Returns undefined
๐Ÿ”„ No self-fallbackโœ… gemini-2.5-flash returns undefined
๐Ÿ”„ No fallback for arbitrary modelโœ… Returns undefined
๐Ÿ”„ DEFAULT_DESCRIBER_MODEL has fallbackโœ… Fallback is defined for the describer default

๐Ÿ“ˆ Total: 223 blog-image tests, 28 gemini tests, 1028 across all suites โ€” all passing.

๐ŸŽฏ The Result

๐Ÿ”‹ Before: Two free-tier providers (Cloudflare + Hugging Face) before falling back to Gemini quota.

๐Ÿš€ After: More providers in the chain, with Pollinations.ai as a truly free safety net that requires zero setup. ๐ŸŒธ Even if every other provider is exhausted or misconfigured, Pollinations will still generate images.

๐Ÿ—๏ธ The provider chain architecture made adding both providers surgical โ€” just a generator function, a block in resolveImageProviders, and workflow env vars. ๐Ÿงฉ No changes needed to the backfill loop, error handling, or retry logic.

๐Ÿ“š Book Recommendations

๐Ÿ“– Similar

  • ๐Ÿ”ง Release It! by Michael Nygard โ€” designing resilient systems with fallback chains and circuit breakers
  • ๐Ÿ—๏ธ Building Microservices by Sam Newman โ€” service decomposition and multi-provider integration patterns

๐Ÿ”„ Contrasting

  • ๐ŸŽฏ The Art of Simplicity by Dominique Loreau โ€” sometimes one provider is enough, if you choose the right one
  • ๐Ÿ“ A Philosophy of Software Design by John Ousterhout โ€” deep modules over wide interfaces
  • ๐Ÿ–ผ๏ธ Ways of Seeing by John Berger โ€” how we perceive and generate images across different mediums
  • ๐Ÿง  The Master Algorithm by Pedro Domingos โ€” the quest for a universal learning machine that bridges all approaches

๐Ÿฆ‹ Bluesky

2026-03-23 | ๐ŸŒธ Expanding the Image Pipeline and Adding Gemini Model Fallback

AI Q: ๐Ÿ› ๏ธ Do you prefer building resilient systems or keeping things simple?

๐ŸŒธ Image Generation | ๐Ÿค– AI Models | ๐Ÿ”— System Architecture | ๐Ÿงช Testing & Fallback
https://bagrounds.org/ai-blog/2026-03-23-together-ai-provider

โ€” Bryan Grounds (@bagrounds.bsky.social) 2026-04-01T13:45:49.000Z

๐Ÿ˜ Mastodon

Post by @bagrounds@mastodon.social
View on Mastodon