Skip to content

AI Gateway

The AI Gateway gives your project a single, OpenAI-compatible endpoint. Your app calls it with the signed-in user’s access token; the provider key (OpenAI, Anthropic, Gemini, Azure OpenAI, Mistral, xAI, OpenRouter, any OpenAI-compatible server or Ollama) never leaves the server. Quota, rate limit, fallback, cache and usage are enforced per user and per tenant.

In the dashboard:

  1. Add a provider key under Model Providers.
  2. Open AI Gateway and add a model: an alias your apps send as model (for example chat-fast), its kind (chat or embedding) and one or more routes (a credential plus a provider model). Routes are tried in order: when the first provider fails (network error, 5xx, 429), the next one answers.
  3. Set the default quota and rate limits, optionally the cache and the egress allowlist, switch Enable AI Gateway on and save.

Only the listed aliases can be called. Anonymous users are refused unless Allow anonymous users is on.

The base URL is <project URL>/ai/v1 and the API key is the user’s access token (Authorization: Bearer <access token>). A secret key may be used on a server; it counts against the project only.

import { createClient } from "@potalab/base"
import { withAi } from "@potalab/base-cloud-sdk-ai-gateway"
const base = withAi(createClient({ url, key }))
const { data, error } = await base.ai.chat({
model: "chat-fast",
messages: [{ role: "user", content: "Hi" }],
})
const stream = await base.ai.stream({ model: "chat-fast", messages })
for await (const chunk of stream.data!) process.stdout.write(chunk.choices[0]?.delta.content ?? "")
const emb = await base.ai.embed({ model: "embed-small", input: ["a", "b"] })

Any OpenAI SDK works too: point baseURL at <project URL>/ai/v1 and use the access token as apiKey.

Endpoint Notes
POST /ai/v1/chat/completions Messages (text, image_url as data: or https:), tools, tool_choice, response_format, temperature, max_tokens. n must be 1. stream: true answers Server-Sent Events.
POST /ai/v1/embeddings input is a string or a list; encoding_format is float or base64.
GET /ai/v1/models The aliases of the project.

OpenAI-compatible and Azure providers stream for real. Anthropic and Gemini answers are produced in full and replayed as SSE. Fallback only happens before the first byte.

  • Token quota per user, per tenant (claim tenant_id) and per project, per day or month (UTC). A request first reserves an estimate and is settled to the provider’s real usage afterwards; a failed request is refunded. A subject with nothing left gets 429 insufficient_quota with Retry-After.
  • Rate limit in requests per minute per user, tenant and project (429 rate_limit_exceeded).
  • Plan: ai_gateway_tokens_month caps the whole project; the lower of the plan and the admin value wins.

Off by default. When on, identical deterministic requests (chat with temperature <= 0.3, no tools, no streaming; embeddings) are served from the database without calling the provider and without charging tokens. By default an answer is only served to the user it was generated for (scope: user); project shares answers between users, so use it only for non-personal prompts. Cached responses are stored in clear text for up to 24 hours: do not turn the cache on for prompts with personal data.

Providers with a fixed address are always allowed. A credential with a custom base_url (Azure, OpenAI-compatible, Ollama) is called only when its host is in the project’s egress allowlist (api.example.com, *.example.com, host:8443).

Usage in the dashboard shows requests, errors, cache hits, tokens in and out, estimated cost, per model, per day and top users. Cost is tokens x price with the prices you set per model; without a price the cost is unknown.

Errors use the OpenAI shape { error: { message, type, code } }: 401 invalid_api_key, 402 feature_not_in_plan, 403 ai_gateway_disabled | anonymous_not_allowed, 404 model_not_found, 400 invalid_request_error | context_length_exceeded, 429 rate_limit_exceeded | insufficient_quota | upstream_rate_limited, 502 upstream_error. Provider messages and keys are never forwarded to the client.

The gateway also powers Semantic columns, which use your embedding aliases.