Welcome to the definitive learning guide for Free-AI Gateway! Whether you are an AI engineer, a systems architect, or a developer looking to integrate high-performance zero-cost AI into your applications and agent workflows, this guide will take you step-by-step through the design, implementation, and extension of the system.
- Architectural Philosophy
- Monorepo Package Boundaries
- Core Concepts: Capability-First Routing
- The Resilience & Fault-Tolerance Engine
- The HTTP Gateway & OpenAI Compatibility Layer
- Model Context Protocol (MCP) Server
- Agentic Skills Architecture
- Hands-On Code Tutorials
- Advanced Patterns & FAQ
Modern generative AI development often suffers from two major pain points:
- High or unpredictable API costs during prototyping and automated agentic testing.
- Brittle provider lock-in, where code is tied directly to OpenAI, Anthropic, or Gemini SDKs.
Free-AI Gateway solves this by aggregating 19+ free-tier AI providers (Groq, Google AI Studio, OpenRouter, SambaNova, NVIDIA NIM, Cohere, HuggingFace, Cloudflare Workers AI, Jina, Tavily, Exa, and more) into a single, unified, resilient routing layer.
- Capability-First, Not Model-First: Applications and AI agents request what they need (e.g.,
["text", "reasoning"]or["structured_output"]), and the gateway dynamically routes to the best healthy, available provider. - Strict Protocol Neutrality: The core orchestration engine (
@free-ai-gateway/core) is completely decoupled from HTTP frameworks. It works exclusively with internalUnifiedRequestandUnifiedResponsecontracts. - Proactive Resilience: Sliding-window rate limiters prevent 429 quota exhaustion before requests are sent, and circuit breakers fail over instantly if an upstream endpoint degrades.
- Agentic Native: First-class support for MCP (Model Context Protocol) and IDE agent skills for Cursor, Claude Code, Antigravity, and GitHub Copilot.
Free-AI Gateway is structured as an enterprise TypeScript monorepo with strict architectural boundaries:
free-ai-gateway/
├── packages/
│ ├── core/ → @free-ai-gateway/core
│ │ Pure, protocol-neutral AI capability router, resilience engine,
│ │ and provider adapters. Zero HTTP server dependencies.
│ │
│ ├── mcp/ → @free-ai-gateway/mcp
│ │ Model Context Protocol (MCP) server providing stdio tools
│ │ and resources for AI IDEs and autonomous agents.
│ │
│ ├── skills/ → @free-ai-gateway/skills
│ │ Agentic IDE skill aggregator and CLI installer for Antigravity,
│ │ Claude, Cursor, and Copilot.
│ │
│ └── cli/ → @free-ai-gateway/cli
│ Terminal AI client, interactive REPL, system diagnostics (doctor),
│ and provider catalog inspector.
│
├── apps/
│ └── gateway/ → @free-ai-gateway/gateway
│ High-throughput Fastify HTTP server exposing OpenAI-compatible
│ v1 endpoints, streaming SSE, and background health reverification.
│
└── tests/
└── e2e/ → Cross-package integration tests validating end-to-end flows.
- Rule 1:
@free-ai-gateway/coremust never import Fastify, Express, or any HTTP server library. - Rule 2: Downstream packages (
mcp,skills,cli,gateway) only import from@free-ai-gateway/corepublic exports (packages/core/src/index.ts). Internal deep imports are disallowed. - Rule 3: Background timers (such as periodic health probes) belong exclusively in
apps/gateway, keeping Core deterministic and testable.
Rather than hardcoding provider-specific model IDs in your application, Free-AI Gateway categorizes models by standardized lowercase snake_case capabilities:
| Capability Token | Description | Example Free Providers |
|---|---|---|
text |
Standard text completion and general LLM chat | Groq, Google AI Studio, SambaNova |
code |
Coding, syntax generation, and code review | Groq (Qwen-2.5-Coder), OpenRouter |
reasoning |
Deep chain-of-thought and mathematical reasoning | Groq (DeepSeek-R1), OpenRouter |
tool_calling |
Function calling and structured tool execution | Groq, Google AI Studio, SambaNova |
structured_output |
Guaranteed JSON schema adherence | Groq, Google AI Studio |
vision |
Multimodal visual understanding and OCR | Google AI Studio, Groq (Llama-3.2-Vision) |
embedding |
Vector representation for RAG and search | Cohere, Cloudflare, Voyage AI |
rerank |
Semantic search re-ranking for dense retrieval | Cohere, Jina AI, Voyage AI |
web_search |
Real-time web search and content retrieval | Tavily, Exa, Jina Reader |
document_processing |
PDF, Markdown, and document chunking | Unstructured, Jina Reader |
speech_to_text |
Audio transcription and speech recognition | Groq (Whisper-large-v3), Cloudflare |
text_to_speech |
High-fidelity voice synthesis | Cloudflare Workers AI |
translation |
Specialized multi-language translation | MyMemory, Cloudflare |
content_moderation |
Safety and moderation guardrails | AIMLAPI (Llama-Guard) |
When a request enters the engine, it is normalized into a UnifiedRequest:
import { UnifiedRequest, UnifiedResponse } from "@free-ai-gateway/core";
const request: UnifiedRequest = {
capabilities: ["text", "reasoning"],
messages: [
{ role: "system", content: "You are an expert algorithm designer." },
{ role: "user", content: "Explain Dijkstra's algorithm with time complexity." }
],
temperature: 0.2,
maxTokens: 2048,
preferredProvider: "groq" // Optional hint
};The gateway achieves 99.9% uptime across free-tier providers using a 3-pillar resilience architecture:
┌──────────────────────────────┐
│ Incoming Request │
└──────────────┬───────────────┘
│
▼
┌──────────────────────────────┐
│ Sliding Quota Tracker │
│ (Checks RPM, RPD, TPM scope) │
└──────────────┬───────────────┘
│ Quota OK?
▼
┌──────────────────────────────┐
│ Circuit Breaker │
│ (CLOSED / OPEN / HALF_OPEN) │
└──────────────┬───────────────┘
│ Healthy?
▼
┌──────────────────────────────┐
│ Adaptive Health Strategy │
│ (Weights latency & successes)│
└──────────────┬───────────────┘
│
▼
┌──────────────────────────────┐
│ Execute Primary Adapter │
└──────────────┬───────────────┘
│
Success? │ Fail?
┌─────────┴─────────┐
▼ ▼
┌─────────────┐ ┌─────────────┐
│ Return │ │ Failover │
│ Response │ │ Next Match │
└─────────────┘ └─────────────┘
Tracks request timestamps within sub-minute and daily sliding windows:
- RPM (Requests Per Minute): Blocks bursts before exceeding free-tier limits.
- RPD (Requests Per Day): Protects 24-hour daily quotas.
- Scopes: Tracks quotas at both account-level (
groq) and per-model level (groq:llama-3.3-70b-versatile).
Each provider model instance is shielded by a circuit breaker:
CLOSED: Requests flow normally. Failures increment an error counter.OPEN: After 3 consecutive failures, the circuit opens for 60 seconds. Requests fail fast without hitting the degraded provider.HALF_OPEN: After cooldown, trial requests test if upstream service has recovered.
Calculates a live health score (
- Historical Success Rate: Percentage of successful completions over recent executions.
- Average Latency: Lower latency yields higher preference.
- Verified State: Periodic background health checks mark verified active providers.
The Fastify gateway server (apps/gateway) acts as a drop-in replacement for OpenAI SDKs, LangChain, LlamaIndex, LiteLLM, and any HTTP client:
GET /v1/models— Returns dynamic list of all registered models and active capabilities.POST /v1/chat/completions— Full streaming (stream: true) and non-streaming chat completions.POST /v1/embeddings— Text embeddings with array input.
POST /v1/audio/transcriptions— Whisper speech-to-text audio processing.POST /v1/rerank— Semantic document ranking for search and RAG.POST /v1/documents/process— Extraction and parsing of documents and URLs.POST /v1/search— Web search query routing.POST /v1/moderate— Text content safety classification.GET /health— Real-time health status of all registered providers and circuit breakers.
The @free-ai-gateway/mcp package enables any MCP-compatible client (Claude Desktop, Cursor, LibreChat) to use Free-AI Gateway as an AI toolset via stdio:
freeai_generate: Execute text/code/reasoning generation with automatic capability routing.freeai_search: Perform live web searches via free-tier search providers.freeai_rerank: Rerank document search candidates by relevance score.freeai_embed: Generate dense vector embeddings for input texts.freeai_process_doc: Extract clean markdown and metadata from URLs or documents.
freeai://capabilities— Live catalog of supported capability tokens.freeai://models— Full list of active models across all 19 providers.
The @free-ai-gateway/skills package provides curated agentic instructions and prompts for developer AI workflows:
free-ai-gateway: How AI agents can route prompts, choose capabilities, and query the local HTTP server.provider-scaffolding: Guidelines for implementing and registering new provider adapters.mcp-integration: Setup instructions for connecting agents to the gateway MCP server.
# Install to current project workspace
npx free-ai-skills install --target .
# Install to Claude Code global directory
npx free-ai-skills install --runtime claude
# Install to Google Antigravity IDE global config
npx free-ai-skills install --runtime antigravityYou can embed @free-ai-gateway/core directly into your TypeScript/JavaScript projects without running the HTTP server:
import {
CapabilityRouter,
Registry,
AdaptiveHealthStrategy,
UnifiedRequest
} from "@free-ai-gateway/core";
async function main() {
// 1. Initialize Registry with built-in provider configurations
const registry = new Registry();
await registry.initialize();
// 2. Instantiate Capability Router with Adaptive Health strategy
const router = new CapabilityRouter(registry, new AdaptiveHealthStrategy());
// 3. Dispatch a capability-routed prompt
const request: UnifiedRequest = {
capabilities: ["text", "reasoning"],
messages: [
{ role: "user", content: "Solve: If a train leaves Chicago at 60mph..." }
],
temperature: 0.3
};
const response = await router.route(request);
console.log(`Provider Used: ${response.providerId} (${response.modelId})`);
console.log(`Response: ${response.content}`);
console.log(`Latency: ${response.latencyMs}ms`);
}
main().catch(console.error);Adding a new provider takes just 3 simple steps:
Create packages/core/src/providers/my-provider.ts:
import { BaseProvider } from "./base-provider.js";
import { UnifiedRequest, UnifiedResponse } from "../types/contracts.js";
export class MyProviderAdapter extends BaseProvider {
public async execute(request: UnifiedRequest): Promise<UnifiedResponse> {
const startTime = Date.now();
const apiKey = this.getApiKey();
const response = await this.httpClient.post(
"https://api.myprovider.com/v1/chat/completions",
{
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json"
},
body: {
model: request.model || this.defaultModel,
messages: request.messages,
temperature: request.temperature
}
}
);
return {
id: response.id || `myprov-${Date.now()}`,
providerId: this.id,
modelId: request.model || this.defaultModel,
content: response.choices[0].message.content,
finishReason: response.choices[0].finish_reason || "stop",
latencyMs: Date.now() - startTime
};
}
}{
"id": "my_provider",
"name": "My AI Provider",
"auth": {
"type": "api_key",
"headerName": "Authorization",
"headerPrefix": "Bearer",
"envVar": "MY_PROVIDER_API_KEY"
},
"models": [
{
"id": "my-fast-model",
"capabilities": ["text", "code"],
"contextWindow": 32768,
"maxOutputTokens": 4096
}
]
}export { MyProviderAdapter } from "./my-provider.js";Point Cursor's custom OpenAI endpoint to your local gateway:
- Base URL:
http://localhost:3000/v1 - API Key:
sk-free-ai-gateway-local(or any string) - Model:
groq/llama-3.3-70b-versatileorauto
{
"mcpServers": {
"free-ai-gateway": {
"command": "npx",
"args": ["-y", "@free-ai-gateway/mcp"]
}
}
}The @free-ai-gateway/cli provides instant terminal AI:
# Ask a single question with reasoning
free-ai prompt "Explain raft consensus algorithm" --capability reasoning
# Start an interactive multi-turn chat session
free-ai chat --model groq/llama-3.3-70b-versatile
# Run system health diagnostics across all configured API keys
free-ai doctor
# Search available models for structured output
free-ai models --capability structured_outputCreate a .env file in the root of your project or gateway directory:
GROQ_API_KEY=gsk_...
GOOGLE_AI_STUDIO_API_KEY=AIzaSy...
OPENROUTER_API_KEY=sk-or-v1-...
SAMBANOVA_API_KEY=...
NVIDIA_NIM_API_KEY=nvapi-...
COHERE_API_KEY=...
TAVILY_API_KEY=tvly-...The Registry gracefully detects missing environment variables during initialization, logging a debug notice and excluding unavailable providers without throwing errors. The gateway continues operating with all configured keys.
# Run complete test suite across all packages and E2E
npm test
# Run typechecks across all monorepo packages
npm run typecheckWe welcome contributions of new provider adapters, skills, and routing strategies! Check out:
- CONTRIBUTING.md — Step-by-step contribution guide.
- AGENTS.md — Instructions for AI coding assistants.
- GitHub Issues — Bug reports and provider requests.