跳到正文
DEV Community· RefractHub·· 55 分鐘前AI 評分58

Refract 單一 API Key 快速入門:以 Python 和 Node 呼叫 Claude、GPT、Gemini 和 Grok

One API key for Claude, GPT, Gemini and Grok: a Python and Node quick start

AI 導讀

Refract 提供一個 OpenAI 相容端點,讓用戶以單一 API key 呼叫 Anthropic、OpenAI、Google、xAI、DeepSeek、Moonshot、Qwen 和 Z.ai 的模型,並沿用現有 SDK、只需更改模型名稱。

正文

Cover image for One API key for Claude, GPT, Gemini and Grok: a Python and Node quick start

RefractHub

If you build with LLMs, you probably juggle several provider accounts, keys and SDKs. Here's a shortcut: one OpenAI-compatible endpoint that serves models from Anthropic, OpenAI, Google, xAI, DeepSeek, Moonshot, Qwen and Z.ai, so you keep the SDK you already use and only change the model name.

I'm using Refract here. It's pay per token with no subscription, and prices are well below the official ones (Claude Sonnet 5.5 is $0.50 / $2.50 per 1M tokens against $2 / $10).

Python (OpenAI SDK)

import os
from openai import OpenAI

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

for model in ["claude-sonnet-5-5", "gpt-6.2-sol", "gemini-4-argon", "grok-4.7"]:
    r = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": "Explain recursion in one sentence."}],
    )
    print(model, "→", r.choices[0].message.content)

Node (OpenAI SDK)

import OpenAI from "openai";

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

const r = await client.chat.completions.create({
  model: "claude-opus-5-5",
  messages: [{ role: "user", content: "Write a haiku about TypeScript." }],
});
console.log(r.choices[0].message.content);

Python (Anthropic SDK)

The Anthropic Messages API works too, so existing Claude code needs only a new base URL:

import os, anthropic

client = anthropic.Anthropic(base_url="https://refracthub.com", api_key=os.environ["REFRACT_API_KEY"])
msg = client.messages.create(model="claude-sonnet-5-5", max_tokens=300,
                             messages=[{"role": "user", "content": "Summarise the plot of Hamlet in 3 lines."}])
print(msg.content[0].text)

Streaming, tools and images

Streaming (stream=True), tool calling and image inputs work the same way as with the official SDKs. A few more things that help:

  • Spending caps per key, so a bug in a loop can't drain your balance.
  • A usage log with the tokens and cost of every request.
  • "model": "auto" lets Refract pick a model for each request.

Try it

Get a key at refracthub.com and see every model ID and price on the models page. The examples above are also in a GitHub repo: refract-examples.

來源:DEV Community · dev.to