Home / Learn / Use Claude with the OpenAI SDK: OpenAI-compatible endpoint, streaming, tools and limits
OpenAI SDK + Claude. Two complementary physical connector shapes joining a code window to another, a clear metaphor for SDK compatibility.

Use Claude with the OpenAI SDK: OpenAI-compatible endpoint, streaming, tools and limits

Code written for the OpenAI SDK can call Claude without a rewrite: point base_url at the gateway's /v1, use a gateway key and a Claude model ID. The endpoint accepts Chat Completions requests, streams in the same format and supports function calling. A few Claude features only exist in Anthropic's native format; the last section covers when to switch.

Endpoint facts

Item Value
Base URLhttps://aiprimetech.io/v1
Chat endpointPOST https://aiprimetech.io/v1/chat/completions
Model listGET https://aiprimetech.io/v1/models
AuthAuthorization: Bearer <key>; x-api-key is also accepted
ModelsClaude IDs such as claude-sonnet-5; GPT IDs with a key from the Codex group

Python

terminal
pip install openai
app.py
import os
from openai import OpenAI

client = OpenAI(base_url="https://aiprimetech.io/v1", api_key=os.environ["AIPRIME_API_KEY"])

resp = client.chat.completions.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[
        {"role": "system", "content": "You are a concise assistant."},
        {"role": "user", "content": "Explain idempotency keys in two sentences."},
    ],
)
print(resp.choices[0].message.content)
print(resp.usage)

base_url must end in /v1: the SDK appends /chat/completions to it. Without /v1 the request lands on the website and returns HTML; with /v1 twice it returns 404 page not found.

Node.js

app.mjs
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://aiprimetech.io/v1", apiKey: process.env.AIPRIME_API_KEY });

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

Streaming

stream.py
stream = client.chat.completions.create(
    model="claude-sonnet-5",
    max_tokens=2048,
    stream=True,
    stream_options={"include_usage": True},
    messages=[{"role": "user", "content": "Outline a zero-downtime migration."}],
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
    if chunk.usage:
        print("\n", chunk.usage)

Stream long answers: a non-streaming request has to finish inside your client's timeout, a stream does not. include_usage adds a final chunk with token counts. More on why it matters in streaming LLM responses.

Function calling

tools.py
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

resp = client.chat.completions.create(
    model="claude-sonnet-5", max_tokens=512, tools=tools,
    messages=[{"role": "user", "content": "What is the weather in Paris?"}],
)
call = resp.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)   # get_weather {"city": "Paris"}

The reply comes back with finish_reason: "tool_calls" and OpenAI-style tool_calls. Run the function, append a tool message with the result and the matching tool_call_id, then call again, exactly as with OpenAI models.

Reasoning and thinking

reasoning_effort maps to Claude's effort setting and accepts low, medium, high, xhigh or max. The value none is rejected. When the model thinks, its reasoning arrives in a separate reasoning_content field next to content, so code that reads message.content keeps working unchanged.

reasoning.py
resp = client.chat.completions.create(
    model="claude-opus-5-5", max_tokens=4096, reasoning_effort="high",
    messages=[{"role": "user", "content": "Find the bug in this function: ..."}],
)

Models

Model ID Use it for
claude-sonnet-5Default for coding and agent work: fast, strong, mid-priced.
claude-opus-5-5Harder reasoning, large refactors, long agent runs.
claude-fable-5-1Most capable and most expensive; save it for the hardest tasks.
claude-haiku-4-5Cheap and quick: titles, summaries, classification, small steps.

Use these IDs exactly. Old dated IDs such as claude-3-5-sonnet-20241022 still appear in some model lists but are not served and return a model-not-supported error.

When to use the native Anthropic API instead

The OpenAI format covers chat, streaming, tools and images. Use Anthropic's own Messages format, at the same gateway, when you need explicit prompt-cache breakpoints, thinking blocks you pass back between turns, PDF documents or citations. The Anthropic Python SDK guide and the Vercel AI SDK guide cover that route.

Troubleshooting

What you see Cause Fix
A JSON parse error, response starting with <base_url without /v1https://aiprimetech.io/v1
404 page not found/v1 twiceExactly one /v1
401 Invalid API keyMissing or wrong key in the processCheck the variable; a key belongs to one gateway
Model not supportedOld dated or misspelled model IDUse an ID from the table above
'none' is not supportedreasoning_effort="none"Use low or higher, or leave it out
429 Concurrency limit exceeded for userToo many requests in flightLimit parallelism; see rate limits and retries
Timeouts on long answersLong non-streaming requestStream, or raise the client timeout

Frequently asked questions

Can I use Claude with the OpenAI SDK?
Yes, through an OpenAI-compatible endpoint: set base_url to the gateway's /v1, use a gateway API key and a Claude model ID such as claude-sonnet-5. Streaming and function calling work in the OpenAI format.
What base URL do I use for Claude in the OpenAI SDK?
The gateway URL ending in /v1, e.g. https://aiprimetech.io/v1. The SDK appends /chat/completions.
Does function calling work with Claude through the OpenAI format?
Yes. Pass tools in the OpenAI format; Claude returns tool_calls with finish_reason tool_calls, and you answer with tool messages as usual.
When should I use the native Anthropic API instead?
When you need explicit prompt caching, thinking blocks across turns, PDF input or citations. Those are part of Anthropic's Messages format.
Start using Claude in minutes

Get an API key — no Anthropic account or waitlist required.

Get your API key

AI Prime Tech is an independent API gateway. It is not affiliated with, endorsed by, or a reseller of Anthropic. Claude and related model names are trademarks of their respective owners.

More Claude API guides

How to Get a Claude API KeyClaude API Pricing, ExplainedThe Cheapest Way to Use the Claude APIUnlimited Cursor with the Claude APIUnlimited Claude Code AccessClaude API Gateway vs. Anthropic DirectClaude models explained: a Claude model comparison of Claude Opus vs Sonnet vs Haiku vs Fable — all Claude API models, Anthropic model names and prices (2026)Paying for the Claude API with CryptoClaude API Access Without a WaitlistHow to Reduce Claude Token UsageUnlimited Cline with the Claude APIUnlimited Roo Code with the Claude APIUnlimited Claude in JetBrains IDEsAider with Claude through a gateway: ANTHROPIC_API_BASE, config files, architect mode and fixesUnlimited Claude in VS CodeOpenCode config for Claude: opencode.json setup, models and common errorsHermes Agent with Claude through a gateway: custom provider setup, cost controls and fixesRunning Unlimited OpenClaw on ClaudeWhat an Anthropic-Compatible API MeansClaude Code API key — ANTHROPIC_API_KEY: how to get it, set it up, and when to use claude setup-token insteadClaude Code API Costs — Pricing and How to Cut ThemClaude API errors explained: server error 500, overloaded 529, rate limit 429, bad request 400 — and the fix for eachHow to buy Claude API credits — and why a Claude credit beats any "buy Claude account" offerClaude free API: is there a free Claude API key, and what is actually free in 2026?Claude API vs Claude Pro/Max Subscription — Which to ChooseCodex CLI with a custom provider: config.toml, GPT and Claude models, IDE extension and fixesConnecting Claude to n8nRouting LiteLLM to ClaudeUsing Claude in Kilo CodeGitHub Copilot Chat with Claude through a gateway: Custom Endpoint setup, agent mode and fixesWire Zed to Claude Through the GatewayConnect Cherry Studio to ClaudeChatAnthropic in LangChain: import, base_url, streaming, tools and errorsAnthropic Python SDK: install, set base_url, stream, and handle errorsClaude with the Vercel AI SDK: createAnthropic baseURL, streaming and fixesRun Factory Droid on ClaudeThe Claude Code API, ExplainedIntegrating the Claude API Into Your ApplicationHow to get unlimited Claude usage — and why there is no "Claude Code cracked"