MVP web tool for AI provider failover. Gemini is the primary model. Claude is used when Gemini fails or when debug failover is enabled.
- Next.js App Router
- Tailwind CSS
- n8n workflow
- Supabase request logging
- Vitest unit tests
- Playwright E2E tests
Clone the repository and install dependencies:
git clone https://github.com/dxhod/Smart_ai_failover.git
cd Smart_ai_failover
npm install
copy .env.example .envFill .env:
N8N_WEBHOOK_URL=http://localhost:5678/webhook/smart-ai-failover
SUPABASE_URL=
SUPABASE_SERVICE_ROLE_KEY=
GEMINI_API_KEY=
ANTHROPIC_API_KEY=
GEMINI_MODEL=gemini-3.1-flash-lite
GEMINI_FALLBACK_MODEL=gemini-2.5-flash
ANTHROPIC_MODEL=claude-sonnet-4-5
ANTHROPIC_FALLBACK_MODEL=claude-haiku-4-5Create the Supabase table:
- Create or open a Supabase project at https://supabase.com/.
- Open SQL Editor.
- Run the SQL from
supabase/schema.sql. - Copy the project URL into
SUPABASE_URL. - Copy the service role key into
SUPABASE_SERVICE_ROLE_KEY.
To find SUPABASE_SERVICE_ROLE_KEY, open Supabase Project Settings, go to API, then copy the service_role key from Project API keys. Keep this key server-side only; do not expose it in browser code.
Start n8n:
docker compose up -d n8nImport the workflow:
- Open
http://localhost:5678. - Create the local n8n owner account if prompted.
- Import
n8n/workflows/smart-ai-failover.json. - Activate the imported workflow.
The production webhook URL must be:
http://localhost:5678/webhook/smart-ai-failover
npm run devOpen http://localhost:3000.
docker compose up --buildDocker Compose starts the web app and n8n runtime services in one command. First-time Supabase schema setup and n8n workflow import/activation are manual steps.
This starts:
- Next.js web app on
http://localhost:3000 - n8n on
http://localhost:5678
Supabase is expected to be hosted and configured through .env.
If Docker is used for n8n and local development is used for the web app, run:
docker compose up -d n8n
npm run devnpm run test
npm run test:e2ePlaywright uses E2E_MOCK_API=true, so E2E tests do not spend Gemini or Claude API quota.
- Standard path: Gemini answers, the UI shows a Gemini badge, and a log record appears in the last 5 requests.
- Fallback path: debug failover is enabled, Gemini is forced to fail, Claude answers, and the UI shows a Claude badge.
- Gemini model fallback: n8n tries
GEMINI_MODELfirst, thenGEMINI_FALLBACK_MODEL. - Claude model fallback: if Gemini is unavailable, n8n tries
ANTHROPIC_MODEL, thenANTHROPIC_FALLBACK_MODEL. - Provider outage: if both providers fail, the UI shows a friendly service error.
- Database outage: if Supabase logging fails after an AI response, the user still receives the response with
db_write_failedstatus.
- Open
http://localhost:3000. - Send
Explain what AI failover means in two sentences.with debug failover disabled. - Confirm that the answer badge is
Geminiand the history row hassuccess. - Enable
Debug failoverand send the same prompt. - Confirm that the answer badge is
Claudeand the history row hasfallback_success. - Open Supabase
ai_request_logsand confirm that the latest rows were persisted. - Open n8n and show the
Smart AI Failoverworkflow.
The browser does not call n8n directly. It calls Next.js /api/ask, which proxies the request to N8N_WEBHOOK_URL. This keeps the webhook URL server-side.
History is loaded through /api/history, which returns the latest 5 rows from Supabase.
The n8n workflow owns the provider failover behavior. It attempts GEMINI_MODEL first, then GEMINI_FALLBACK_MODEL, catches forced or real Gemini failures, calls Claude as the provider fallback, tries ANTHROPIC_MODEL before ANTHROPIC_FALLBACK_MODEL, persists the final answer to Supabase, and returns only the final answer to the web app.