Building Jarvis: A Resilient AI Chatbot with Auto-Failover
How I built a portfolio chatbot that stays online even when APIs fail.
The Problem: Unreliable APIs
When I decided to add an AI chatbot to my portfolio, I quickly ran into a frustrating reality:third-party APIs are unreliable. Free-tier services like Google's Gemini API regularly return 503 (Service Unavailable) and 429 (Rate Limit) errors during peak usage.
For a portfolio site, this is unacceptable. Imagine a recruiter visiting your site, trying to interact with your "impressive AI chatbot," only to see an error message. Not a great first impression.
The Solution: Resilient API Design
I implemented a multi-model fallback strategy. The architecture is simple but effective:
- Primary Model: Gemini 2.5 Flash - Fast, capable, and handles most requests
- Fallback Model: Gemini 2.5 Flash Lite - Lighter weight, used when primary fails
- Error Detection: Catch 503/429 errors and automatically switch models
try {
const result = await primaryModel.generateContent(prompt);
return result.response.text();
} catch (error) {
if (error?.status === 503 || error?.status === 429) {
// Fallback to backup model
const result = await backupModel.generateContent(prompt);
return result.response.text();
}
throw error;
}Key Design Decisions
1. Server-Side API Calls
The API key never touches the client. All Gemini calls happen in a Next.js API route, keeping credentials secure and allowing for proper error handling.
2. Rate Limiting
I implemented client-side rate limiting (10 requests per minute per IP) to prevent abuse and reduce the chance of hitting API limits in the first place.
3. Graceful Degradation
Even if both models fail, the user sees a friendly error message rather than a cryptic error code. The system never "breaks" - it just gracefully handles the situation.
The System Prompt: Personality Engineering
Jarvis isn't just a generic chatbot - it has a tuned personality. The system prompt defines its role as a helpful assistant that can discuss tech concepts while naturally connecting conversations back to my work.
Key personality traits:
- Chill and witty, not corporate
- Genuinely helpful - explains concepts, doesn't just redirect
- Bilingual - detects Swedish/English and responds appropriately
- Self-aware - knows it's an AI on a portfolio site
Results
Since implementing the fallback logic, Jarvis has maintained 99.9% uptime. The handful of failures that did occur were gracefully handled with user-friendly messages.
More importantly, this project demonstrates a principle I apply to all my work:production systems need to handle failure gracefully. Happy-path demos are easy; building resilient systems is the real challenge.
Tech Stack
- Frontend: Next.js 16, React, Tailwind CSS, Framer Motion
- AI: Google Gemini API (2.5 Flash + Lite)
- Deployment: Vercel with Edge Functions
Enjoyed this article?