Skip to content

Complete Diagram Architecture

Navigation: README | Diagram Architecture | How It Works | Database Schema | Challenge Requirements | Deployment

This document visualizes the architecture and data flows of KidStory, built for the Google for Startups AI Agents Challenge. The application uses an Agentic Workflow to orchestrate specialized Gemini models into a cohesive multilingual, multimodal user experience.

Challenge Development

KidStory was started as a basic prototype in March 2026 and substantially upgraded during the Google for Startups AI Agents Challenge (April 25 – June 12, 2026) — migrating to Google ADK v1.0, growing from 2 raw API calls to 9 specialized agents, adding simulation testing, observability, 21 languages, RAG agents, and a full credit system.

For the complete development journey with commit links and what was built during the challenge period, see Challenge Requirements.

Complete Diagram Architecture

Click here to view the full diagram in a new tab.

KidStory - Complete Diagram Architecture


1. High-Level System Architecture


2. Story Generation Pipeline (3-Step Sequential)

The story is generated in three clean sequential steps: text first, then images, then audio — no interleaved mixing.


3. Magic Quiz — Multi-Type Multilingual Flow

The quiz generates different question types per round and always uses the story's stored language field.


4. Magic Quiz State Machine


5. Language Flow — End to End


6. Deployment Architecture

Key Infrastructure Components

ComponentPurposeConfiguration
Cloud RunHosts Next.js appMin instances: 1, Region: us-central1
Cloud StorageStores media filesCORS enabled, public read
FirestoreUser data & storiesNative mode, language field on stories
Vertex AIAI model accessGemini 2.5 Pro, Gemini 2.5 Flash Image, TTS
Firebase AuthUser authenticationGoogle OAuth
Cloud BuildCI/CD pipelineAutomatic container builds
Artifact RegistryContainer storageDocker images

7. Credit System Flow

Credit Tiers

PlanMonthly CreditsStory CostQuiz CostPrice
Free305 credits1 credit$0
Pro1505 credits1 credit$4.99/month

Credits reset automatically on the 1st of each month. Deduction happens server-side via lib/credits/serverCredits.ts using Firebase Admin SDK — only after the safety check passes.

Environment Variables

Runtime (cloudbuild.yaml / Cloud Run):

VariableDescriptionDefault
GOOGLE_CLOUD_PROJECTGCP project IDstorybook-for-kids
GOOGLE_CLOUD_REGIONVertex AI regionus-central1
GCS_BUCKET_NAMEStorage bucketstorybook-for-kids-media
GEMINI_STORY_MODELStory text generation modelgemini-2.5-pro
GEMINI_IMAGE_MODELIllustration generation modelgemini-2.5-flash-image
GEMINI_TTS_MODELText-to-speech modelgemini-2.5-flash-preview-tts
GEMINI_QUIZ_MODELQuiz generation modelgemini-2.5-pro

8. Feedback System Flow

Firestore path: feedback/{feedbackId}

FieldTypeDescription
userIdStringFirebase Auth UID
ratingNumber1-5
messageStringOptional text
languageString"en" or "tet"
createdAtStringISO timestamp

Security: authenticated users can create only. Admin reads via Firebase Console.


9. Multi-Agent Collaboration Graph

This diagram shows how KidStory's 9 specialized agents collaborate — not as a linear pipeline, but as a network where agents hand off results, run in parallel, and feed each other's context.

Agent Collaboration Highlights

CollaborationTypeDetail
StoryOrchestrator → SafetyGuardianSequential gateStory only proceeds if approved; 0 credits charged on block
StoryOrchestrator → StoryWriterSequential handoffPrompt + character refs → structured title + pages + imagePrompts
StoryOrchestrator → Illustrator + NarratorMixedNarrator: all pages parallel. Illustrator: sequential with 32s start-to-start gap (2 RPM quota). Pages assembled as results arrive.
QuizMaster → 5 questionsParallelAll 5 questions generated concurrently via Promise.all; pre-generates encouragement/correction text
Firestore → LearningAdvisorRAG (private data)Last 10 quiz scores retrieved at query time → grounded difficulty + theme recommendation
Firestore → ParentInsightsRAG (private data)Full story + quiz history retrieved → grounded child progress report
Firestore → StoryAdaptationRAG (private data)Last 3 quiz records retrieved → adapted prompt + vocabulary level + page count; closes learning loop
StoryAdaptation → StoryWriterAdaptive loopAdapted prompt passed to /create → StoryWriter generates story at the calibrated level

10. Safety Check — Detailed Flow

The safety popup shows:

  • Category pills (violence, adult content, drugs, etc.) from client-side detection
  • AI explanation from Gemini's flag_unsafe_content tool call reason field
  • "Edit My Idea" and "Start Over" buttons

Released under the MIT License.