Cloudflare AI Gateway
Cloudflare AI Gateway は、カスタムプロバイダ として https://api.aiand.com をプロキシできます。Gateway のログ・分析・キャッシュ・レート制限が使え、課金はこれまで通り ai& の sk- キーです。
ai& は Gateway のネイティブプロバイダではありません。URL は /openai や /aiand ではなく custom-{slug} です。
セットアップ
Section titled “セットアップ”先に API キー を発行します。
-
Cloudflare ダッシュボードの AI → AI Gateway → Custom Providers でプロバイダを追加します。
- Slug:
aiand - Base URL:
https://api.aiand.com(末尾スラッシュも/v1も付けない) - 有効化する
- Slug:
-
ゲートウェイを作成します(名前は任意。以下では
aiand-gateway)。 -
ai& のキーは
Authorization: Bearer sk-…に載せます。この経路では Authenticated Gateway は任意です。オンなら AI Gateway Run 権限の Cloudflare トークンをcf-aig-authorizationにも付けます。 -
キーを Provider Keys(BYOK)に保存して
Authorizationを省略する場合は、Authenticated Gateway をオンにし、毎回cf-aig-authorizationを付けます。この Cloudflare トークンはsk-ではありません。
カスタムプロバイダの作成画面にキー欄はありません。それが正しいです。
ゲートウェイ経由の呼び出し
Section titled “ゲートウェイ経由の呼び出し”ACCOUNT_ID は Cloudflare のアカウント ID に置き換えてください。
curl https://gateway.ai.cloudflare.com/v1/ACCOUNT_ID/aiand-gateway/custom-aiand/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $AIAND_API_KEY" \ -H "cf-aig-authorization: Bearer $CF_AIG_TOKEN" \ -d '{ "model": "openai/gpt-oss-120b", "messages": [{"role": "user", "content": "Hello"}] }'例には両方のヘッダがあります。cf-aig-authorization を外してよいのは、自分で sk- を送り、かつ Authenticated Gateway がオフのときだけです。Authorization を外してよいのは BYOK のときだけで、その場合は cf-aig-authorization を残します。
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.AIAND_API_KEY, baseURL: "https://gateway.ai.cloudflare.com/v1/ACCOUNT_ID/aiand-gateway/custom-aiand/v1", defaultHeaders: { "cf-aig-authorization": `Bearer ${process.env.CF_AIG_TOKEN}`, },});
const response = await client.chat.completions.create({ model: "openai/gpt-oss-120b", messages: [{ role: "user", content: "Hello" }],});SDK が /chat/completions を付け、Gateway が https://api.aiand.com/v1/chat/completions を呼びます。同じ baseURL で /v1/responses、/v1/messages、/v1/models、/v1/completions も使えます。
Unified /compat(任意)
Section titled “Unified /compat(任意)”curl https://gateway.ai.cloudflare.com/v1/ACCOUNT_ID/aiand-gateway/compat/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $AIAND_API_KEY" \ -H "cf-aig-authorization: Bearer $CF_AIG_TOKEN" \ -d '{ "model": "custom-aiand/openai/gpt-oss-120b", "messages": [{"role": "user", "content": "Hello"}] }'/compat は chat completions だけです。Responses、Messages、カタログは /custom-aiand/v1 を使ってください。
ゲートウェイ設定で Cache Responses をオンにします。同一リクエストは cf-aig-cache-status: HIT になり、ai& には届かず課金されません。ミスは MISS です。
デフォルトのキャッシュキーはプロバイダ・パス・モデル・認証・JSON ボディ全体の完全一致です。バイパスは cf-aig-skip-cache: true です。
ストリーミングはデフォルトではキャッシュされません。Cache Responses の対象は同一の非ストリーミングのテキストと画像だけです。stream: true は ai& に届き、課金されます。
cf-aig-authorizationの欠落や誤り(認証オン時): Cloudflare の401(AiGatewayError)。OpenAI 形式ではありません。- 誤ったカスタム slug: Cloudflare の
502(The provider did not return a valid response)。ai& の404ではありません。 - 未知のモデル、壊れた JSON、空の
messages、不正なreasoning_effort、テキスト専用モデルへの画像: いつもどおりの OpenAI 形式エラーがそのまま返ります。
これではないもの
Section titled “これではないもの”env.AI.run("@cf/…") は別製品の Workers AI です。Worker から AI Gateway なしで ai& を呼ぶなら、OpenAI SDK の baseURL を https://api.aiand.com/v1 にしてください。