Engineering
Decoupling AI Infrastructure
(New backend. Same Android app.)
Migrating Shiv from Gemini Developer API to Vertex AI Without Changing the Product
Engineering Case Study • AI Infrastructure • Cloud Architecture
Built while developing Learnzy.
Executive Summary
Shiv originally used the Gemini Developer API through a Supabase Edge Function.
I kept the early architecture simple to validate Learnzy quickly.
flowchart TD
A[Android] --> B[Supabase Edge Function]
B --> C[Gemini Developer API]
Quota and billing constraints later made Vertex AI a better fit, particularly because Learnzy had Google Cloud credits available.
I introduced a Cloud Run gateway to migrate without changing Android's API or Shiv's product behavior.
Context
During the first versions of Learnzy, speed of iteration mattered more than infrastructure.
The simplest architecture was:
flowchart TD
A[Student] --> B[Android]
B --> C[Supabase Edge Function]
C --> D[Gemini Developer API]
D --> E[Response]
Shiv already had:
- Persona
- Context
- Guardrails
- Product Identity
I needed to change the infrastructure, not these product layers.
The Problem
Two independent systems existed.
The next two diagrams show the billing separation. Application requests still passed through Supabase.
System One
Shiv was running through the Gemini Developer API.
flowchart TD
A[Android] --> B[Gemini Developer API]
B --> C[Developer Billing]
System Two
Google Cloud Platform credits were available through Vertex AI.
flowchart TD
A[Google Cloud] --> B[Vertex AI]
B --> C[Startup Credits]
C --> D[Unused]
The product was paying through one billing system while another funding source remained unused.
I wanted to move the infrastructure without changing the product.
Design Constraints
Product Constraints
- Shiv’s personality should remain unchanged.
- Android should not require any code changes.
- Existing APIs should remain stable.
Infrastructure Constraints
- Use Google Cloud credits.
- Remove dependence on Gemini Developer billing.
- Keep API credentials off the client.
- Avoid downloaded service account JSON keys.
Engineering Constraints
- Migration should be reversible.
- Minimal downtime.
- Easy future migrations.
- Provider independence.
Existing Architecture
flowchart TD
A[Student] --> B[Android App]
B --> C[Supabase gemini-proxy]
C --> D[Gemini Developer API]
D --> E[Gemini Response]
Advantages:
- Simple.
- Fast to build.
- Minimal infrastructure.
Limitations:
- Developer API quotas.
- Separate billing.
- Limited infrastructure control.
- Difficult future provider changes.
Existing Solutions Considered
| Option | Decision | Reason |
|---|---|---|
| Remain on Gemini Developer API | Rejected | Limited quota and unused Google Cloud credits |
| Move Android directly to Vertex AI | Rejected | Android becomes infrastructure-aware and credential handling becomes harder |
| Introduce an AI Gateway | Selected | Product stays stable while infrastructure becomes replaceable |
Whiteboard Thinking
I asked which component needed to know that Vertex existed. It should be an infrastructure layer, not Android or Shiv's product logic.
Existing flow:
flowchart TD
A[Android] --> B[Supabase]
B --> C[Gemini]
Desired flow:
flowchart TD
A[Android] --> B[Supabase]
B --> C[Infrastructure Layer]
C --> D[Language Model]
The unknown layer became the AI Gateway.
The Insight
Android should call a stable Shiv API. Whether the backend uses Gemini Developer API, Vertex AI, OpenAI, Anthropic, or a future local model belongs behind that interface.
Final Architecture
flowchart TD
A[Student] --> B[Android Application]
B --> C[Supabase Edge Function]
C --> D[Authentication Layer]
D --> E[Gateway Request Validation]
E --> F[Cloud Run AI Gateway]
F --> G[Infrastructure Abstraction]
G --> H[Vertex AI Gemini]
H --> I[Language Response]
I --> A
Layer Responsibilities
Android
Responsible for:
- Chat UI
- User interaction
- Authentication token
Never responsible for:
- AI provider
- Billing
- Credentials
Supabase
Responsible for:
- User validation
- Session verification
- Product routing
- Gateway authentication
Supabase preserves the existing Android API.
Cloud Run Gateway
The gateway handles:
- Provider abstraction
- Vertex authentication
- Secret management
- Billing isolation
- Future provider switching
Cloud Run is now the only component aware of Vertex AI.
Vertex AI
Provides:
- Language generation
- Image understanding
- Model inference
Vertex handles inference behind the gateway; Android does not call it directly.
Authentication Architecture
The gateway uses a dedicated service account.
flowchart TD
A[Cloud Run] --> B[Attached Service Account]
B --> C[Vertex AI]
Google Cloud IAM handles authentication through the attached service account, without downloaded JSON keys or embedded API keys.
Request Flow
sequenceDiagram
participant Student
participant Android
participant Supabase as Supabase Edge Function
participant Gateway as Cloud Run AI Gateway
participant Vertex as Vertex AI
Student->>Android: Sends message to Shiv
Android->>Supabase: Existing API request
Supabase->>Supabase: Verify user
Supabase->>Gateway: Authenticated gateway request
Gateway->>Vertex: Model inference
Vertex-->>Gateway: Model response
Gateway-->>Supabase: Normalized response
Supabase-->>Android: Shiv response
Android-->>Student: Display response
Rollback Strategy
Rollback changes Supabase's routing back to the Developer API, without an Android release. I designed it to be reversible within minutes.
flowchart TD
A[Cloud Run Disabled] --> B[Supabase]
B --> C[Developer API]
C --> D[Application Continues]
Keeping the client unchanged limited the scope of rollback.
Engineering Decisions
| Decision | Reason |
|---|---|
| Keep Android unchanged | Applications should not know infrastructure details |
| Keep Supabase as the public API | Stable interface for all clients |
| Introduce Cloud Run | Separate infrastructure from product logic |
| Use attached IAM Service Accounts | Avoid long-lived credentials |
| Authenticate gateway requests | Prevent unauthorized access |
Architecture Comparison
Before
flowchart TD
A[Android] --> B[Supabase]
B --> C[Gemini Developer API]
After
flowchart TD
A[Android] --> B[Supabase]
B --> C[Cloud Run]
C --> D[Vertex AI]
D --> E[Gemini]
The new gateway sits behind the existing client-facing interface.
Benefits
The migration made Google Cloud credits usable, centralized provider authentication, and kept API keys out of Android. The stable interface also gives future provider changes a defined boundary.
Trade-offs
Advantages
- Infrastructure abstraction.
- Better security.
- Uses startup credits efficiently.
- Easier future migrations.
- Stable Android client.
Limitations
- Additional Cloud Run hop.
- Slight increase in latency.
- More infrastructure components to monitor.
Lessons Learned
The migration exposed a coupling problem: a provider change should not require an application rewrite. A dedicated gateway gave infrastructure decisions their own boundary.
Future Direction
Potential gateway extensions include:
- Multi-model routing
- Automatic fallback providers
- Cost-aware model selection
- Request logging and analytics
- A/B testing between models
- Streaming responses
- Unified text, vision, and voice endpoints
These remain future extensions of the gateway.
Project Outcome
Shiv moved from the Gemini Developer API to Vertex AI without changing the Android-facing API or its product behavior. Google Cloud credits became usable, and future provider changes now have a gateway boundary rather than requiring client changes.
Project Details
| Field | Detail |
|---|---|
| Project Type | Engineering Case Study |
| Category | AI Infrastructure · Cloud Architecture · Production Migration |
| Built For | Learnzy |
| Author | Himanshu Gupta |
| Status | Production |
| Part of | LearnzyLabs |