Platform

AI Gateway

Call top AI models (Gemini, GPT-5) from edge functions without your own API keys. Master pays the provider, you just call a unified OpenAI-compatible API.

Introduction

AI Gateway is a proxy that accepts OpenAI-compatible requests and routes them to the selected model. The MASTER_API_KEY token is available automatically in every edge function.

Available models

Google Gemini

  • google/gemini-2.5-pro — top reasoning, multimodal, large context
  • google/gemini-2.5-flash — balanced, multimodal, cheaper
  • google/gemini-2.5-flash-lite — fastest and cheapest
  • google/gemini-3-pro-image-preview — image generation
  • google/gemini-3.1-flash-image-preview — fast image generation and editing

OpenAI GPT-5

  • openai/gpt-5 — most powerful all-rounder (gen 1)
  • openai/gpt-5-mini — performance/price compromise (gen 1)
  • openai/gpt-5-nano — high volume, low price (gen 1)
  • openai/gpt-5.2 — reasoning model for complex tasks
  • openai/gpt-5.4-nano — fastest + cheapest ChatGPT
  • openai/gpt-5.4-mini — balanced reasoning, cheap
  • openai/gpt-5.4 — advanced reasoning, code, analysis
  • openai/gpt-5.4-pro — premium reasoning for the hardest tasks
  • openai/gpt-5.5 — state-of-the-art (reasoning, code, instructions)
  • openai/gpt-5.5-pro — extended reasoning, top-tier

How to choose a model

  • Classification, short tasksgpt-5.4-nano or gemini-2.5-flash-lite
  • Most chatbotsgpt-5.4-mini or gemini-2.5-flash
  • Reasoning, planninggpt-5.2 or gpt-5.4
  • Highest qualitygpt-5.5 / gpt-5.5-pro
  • Multimodal + large contextgemini-2.5-pro
  • Image generationgemini-3.1-flash-image-preview

Calling from an edge function

ts
Deno.serve(async (req) => {
  const { prompt } = await req.json();

  const response = await fetch(
    "https://ai.gateway.master-magic.dev/v1/chat/completions",
    {
      method: "POST",
      headers: {
        Authorization: `Bearer ${Deno.env.get("MASTER_API_KEY")}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        model: "google/gemini-2.5-flash",
        messages: [
          { role: "system", content: "You are a helpful assistant." },
          { role: "user", content: prompt },
        ],
      }),
    }
  );

  if (!response.ok) {
    const err = await response.text();
    return new Response(err, { status: response.status });
  }

  const data = await response.json();
  const text = data.choices[0].message.content;

  return Response.json({ text });
});

Streaming responses

For chatbots, you want the text gradually. Add stream: true and stream the response back:

ts
const upstream = await fetch(GATEWAY_URL, {
  method: "POST",
  headers: { /* ... */ },
  body: JSON.stringify({
    model: "openai/gpt-5-mini",
    messages,
    stream: true,
  }),
});

return new Response(upstream.body, {
  headers: {
    "Content-Type": "text/event-stream",
    "Cache-Control": "no-cache",
  },
});

Multimodal input

ts
{
  model: "google/gemini-2.5-pro",
  messages: [{
    role: "user",
    content: [
      { type: "text", text: "What is in the image?" },
      { type: "image_url", image_url: { url: "data:image/png;base64,..." } },
    ],
  }],
}

Tool calling

ts
{
  model: "openai/gpt-5",
  messages,
  tools: [{
    type: "function",
    function: {
      name: "get_weather",
      description: "Get current weather for a city",
      parameters: {
        type: "object",
        properties: { city: { type: "string" } },
        required: ["city"],
      },
    },
  }],
}

Pricing and limits

  • Free tier: $5 credit monthly
  • Then usage-based — markup ~10% above the provider price
  • Rate limit: 60 req/min per project (free), 600 req/min (paid)
  • Max context: by model (Gemini Pro 2M tokens, GPT-5 256k)
For production, cache responses in the database — you will save tokens and latency.

Tips

  • Always use a system prompt — define the role, tone, and output format.
  • Low temperature for facts (0.0–0.3), higher for creativity (0.7–1.0).
  • JSON mode for structured outputs.
  • Retry with exponential backoff on 429 / 503.
  • Prompt caching (Gemini 2.5+): the gateway automatically caches the longest identical prefix of your request and bills it ~75 % cheaper. Put your system prompt and any static context first and keep it bit-identical between turns — put the user input and other volatile bits last. Every response exposes X-Vibe-AI-Cached-Tokens andX-Vibe-AI-Input-Tokens headers so you can measure the hit ratio.