0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GPT・Claude・Gemini・画像/動画モデルを1つのAPIキーで使う方法

0
Posted at

AIアプリを作っていると、最初はOpenAIだけで十分でも、すぐに他のモデルも試したくなります。

例えば:

  • チャットにはGPT系モデル
  • コードレビューにはClaude
  • マルチモーダル入力にはGemini
  • 画像生成にはGPT Image / Midjourney / Qwen Image
  • 動画生成にはKling / Veo / Sora / Runway
  • 検索にはEmbedding / Rerank

モデルごとにAPIキー、SDK、課金画面、エラー形式が増えていくと、アプリ本体よりも「API管理」のほうが面倒になります。

この記事では、OpenAI互換のAPIゲートウェイを使って、1つのAPIキーから複数モデルを呼び出す構成を紹介します。例としてCrazyrouterを使います。

ドキュメント入口はこちらです:

全体像

OpenAI互換クライアントでは、基本的に以下を変えるだけです。

api_key  = Crazyrouterで発行したAPIキー
base_url = https://crazyrouter.com/v1

通常のOpenAI SDKを使いながら、モデル名を変えることでGPT、Claude、Geminiなどを切り替えます。

Pythonで呼び出す

まずSDKをインストールします。

pip install openai

環境変数にAPIキーを入れます。

export CRAZYROUTER_API_KEY="sk-your-key"

Pythonコード:

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["CRAZYROUTER_API_KEY"],
    base_url="https://crazyrouter.com/v1",
)

response = client.chat.completions.create(
    model="gpt-5",
    messages=[
        {"role": "user", "content": "APIゲートウェイとは何ですか?短く説明してください。"}
    ],
)

print(response.choices[0].message.content)

Claudeに切り替える

アプリ側のコード構造はそのままに、モデル名を変えます。

response = client.chat.completions.create(
    model="claude-sonnet-4.6",
    messages=[
        {"role": "user", "content": "このPython関数のバグをレビューしてください。"}
    ],
)

print(response.choices[0].message.content)

Geminiに切り替える

response = client.chat.completions.create(
    model="gemini-2.5-pro",
    messages=[
        {"role": "user", "content": "この仕様を実装タスクに分解してください。"}
    ],
)

print(response.choices[0].message.content)

Node.jsでも同じ考え方

npm install openai
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.CRAZYROUTER_API_KEY,
  baseURL: "https://crazyrouter.com/v1",
});

const result = await client.chat.completions.create({
  model: "gpt-5",
  messages: [
    { role: "user", content: "Explain unified AI API gateways in one paragraph." },
  ],
});

console.log(result.choices[0].message.content);

画像・動画モデルを使う場合

テキストモデルだけでなく、画像生成や動画生成も同じドキュメント入口から探せます。

  • Image Generation
  • Video Generation
  • Models & Billing
  • API Endpoints
  • Quick Start

最初に見る場所:

よくあるミス

1. /v1 を付け忘れる

OpenAI互換SDKでは通常、以下のように /v1 が必要です。

https://crazyrouter.com/v1

2. Claude Codeなどのネイティブ系ツールにも /v1 を付けてしまう

Claude CodeのようなAnthropicネイティブ系ツールでは、OpenAI互換SDKとはエンドポイント指定が違う場合があります。

このあたりはツールごとの設定ページを見たほうが安全です。

3. 1つのAPIキーを全部に使い回す

個人的には、用途ごとにAPIキーを分けるのがおすすめです。

local-dev
production-web
claude-code
cursor
video-worker

あとでログ確認、予算管理、キーのローテーションが楽になります。

まとめ

複数のAIモデルを使うアプリでは、モデルごとにSDKやAPIキーを増やすより、OpenAI互換のゲートウェイにまとめるほうが実装がシンプルになります。

特に以下のような場合に便利です。

  • GPT / Claude / Geminiを比較しながら開発したい
  • 画像・動画・音声モデルも後から追加したい
  • Cursor、Claude Code、Codex CLIなどのAI開発ツールも同じ基盤で管理したい
  • APIキーや利用量をプロジェクト単位で分けたい

Crazyrouterのドキュメント入口は、タスク別に読むページを選べる構成になっています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?