How to use Claude with the OpenAI Python SDK
Call Claude models with the OpenAI Python SDK by routing requests through thelastapi gateway at /v1/ai with your project or account API key.
On this page 7 sections
You can run Claude models directly with the OpenAI Python SDK by setting your client base URL to https://thelastapi.dev/v1/ai and passing your API key. thelastapi exposes an OpenAI-compatible endpoint that routes chat completions to Anthropic models, including Claude Sonnet 5.5, Claude Opus 5.5, and Claude Haiku 5.5. You keep the familiar openai library syntax, streaming methods, and tool definitions while running the Claude model you select.
This guide walks through configuring the OpenAI Python SDK, selecting Claude model names, streaming responses, and inspecting per-call costs.
Configuring the OpenAI Python SDK for Claude
The official OpenAI Python SDK reads two primary environment variables: OPENAI_BASE_URL and OPENAI_API_KEY. When you initialize a project using the thelastapi CLI, the system writes these variables alongside VIBIZ_AI_BASE_URL and VIBIZ_AI_API_KEY.
To set up your project locally, create a project with the AI resource:
vibiz init "Support Engine" --resources ai
vibiz devThe vibiz dev command writes .env.development.local with development keys and sets OPENAI_BASE_URL=https://thelastapi.dev/v1/ai. Because the client checks standard OpenAI environment variables, you can instantiate OpenAI() with no arguments in your Python script:
import os
from openai import OpenAI
# Automatically picks up OPENAI_BASE_URL and OPENAI_API_KEY from your environment
client = OpenAI()
response = client.chat.completions.create(
model="vibiz/smart",
messages=[
{"role": "system", "content": "You are a concise technical assistant."},
{"role": "user", "content": "How do Python generators save memory?"},
],
)
print(response.choices[0].message.content)If you prefer to configure the client explicitly in code, provide the parameters directly:
from openai import OpenAI
client = OpenAI(
base_url="https://thelastapi.dev/v1/ai",
api_key="vz_sk_live_example_key",
)You can learn more about environment setup and credential scopes in Keys and environments.
Selecting Claude tiers and specific model IDs
thelastapi provides two methods for addressing models in your code: functional thelastapi tier aliases and specific lab model identifiers.
When you use a tier alias like vibiz/smart, the gateway routes your request to a vetted, named model. If that underlying lab model is down, the system returns a 503 model_unavailable or 503 provider_unavailable error rather than silently routing your prompt to an unintended provider.
Here is how the Claude tiers compare:
| Model ID | Lab model | Recommended use | Price per million tokens (in / out) |
|---|---|---|---|
vibiz/smart-mini | Claude Haiku 5.5 | Rapid responses, code review, classification | $0.10 / $0.50 |
vibiz/smart | Claude Sonnet 5.5 | Difficult coding tasks, writing, multi-step agents | $2.00 / $10.00 |
vibiz/pro | Claude Opus 5.5 | Deep analysis, complex evaluation, long agent runs | $4.00 / $20.00 |
anthropic/claude-opus-5.5 | Claude Opus 5.5 | Direct lab pinning | $4.00 / $20.00 |
For Claude Haiku 5.5, note that requests beyond 100,000 input tokens shift into long-context pricing as defined in the price book. Default token limits apply across all chat endpoints: max_tokens is 1,024 when omitted and can be set up to 16,384 depending on the model.
If your application compares outputs across multiple providers, you can change the model string without changing SDK packages. You can explore this pattern in OpenAI-compatible API for Claude and Gemini: change one base URL and One API key for GPT, Claude and Gemini: how a unified AI API works.
Streaming Claude completions in Python
Streaming responses works out of the box using standard iteration over the completion generator. The endpoint returns Server-Sent Events (SSE) that the OpenAI Python SDK translates into delta objects.
import os
from openai import OpenAI
client = OpenAI(
base_url=os.environ.get("VIBIZ_AI_BASE_URL", "https://thelastapi.dev/v1/ai"),
api_key=os.environ.get("VIBIZ_AI_API_KEY"),
)
stream = client.chat.completions.create(
model="vibiz/smart",
messages=[
{"role": "user", "content": "Write a Python function to parse JSON lines."}
],
stream=True,
)
for chunk in stream:
content = chunk.choices[0].delta.content or ""
print(content, end="", flush=True)
print()If an error occurs mid-stream, the gateway sends a final SSE error payload formatted as data: {"error": {...}} instead of the standard [DONE] marker. The SDK catches this error state and surfaces it cleanly to your application.
Reading per-call token costs and metadata
Every completion response returned by /v1/ai/chat/completions contains extra accounting fields attached to the response payload. These fields show you the exact model that executed and what the call cost.
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="vibiz/smart",
messages=[{"role": "user", "content": "Summarize the concept of idempotency."}],
)
# Inspect standard usage
print("Input tokens:", response.usage.prompt_tokens)
print("Output tokens:", response.usage.completion_tokens)
# thelastapi appends accounting metadata to the raw response object
raw_response = response.model_extra
if raw_response:
print("Model executed:", raw_response.get("served_model"))
print("Cost in micro-USD:", raw_response.get("cost_micro_usd"))The served_model field confirms whether the request completed on the expected provider model, and cost_micro_usd gives the exact cost of the execution in millionths of a dollar. If you build JavaScript or TypeScript applications alongside your Python services, the TypeScript client library works identically, as shown in Use @ai-sdk/openai with Claude, Gemini and more.
Managing rate limits and project spend ceilings
All AI calls route through built-in spend and rate safeguards. You can track spending and inspect current limits directly from the CLI or programmatically.
Default rate and concurrency limits
Requests made with an account API key (vz_sk_...) are capped at 300 requests and 400,000 tokens per minute, with up to 50 concurrent requests in flight. Requests using a project key (vz_ai_...) are permitted 120 requests and 200,000 tokens per minute, with 25 concurrent requests in flight. A request over the in-flight limit waits up to 60 seconds for a free slot rather than failing. An account owner can raise project limits up to 600 requests and 2,000,000 tokens per minute.
Budgets and spend ceilings
Every account begins with a default daily spend ceiling of $5 across all AI operations and web tools. Each project has a separate default daily AI budget of $1.
When a project exceeds its threshold, requests fail with a 429 rate_limited status code, and the error object contains details.reason: "project_budget". If the overall account reaches its daily limit, requests fail with details.reason: "daily_spend".
To check and adjust these values, use the CLI:
# View recent spend and current limits
vibiz usage
# Adjust the daily budget for a project
vibiz ai budget 10 --project prj_123
# Adjust the account-wide daily safety cap
vibiz billing limit 50Owners receive automated alert emails when spending crosses 80% and 100% of any daily cap. Full details on limit structures, reset intervals at midnight UTC, and programmatic alerts are outlined in Usage and limits.
Verifying available models from the CLI and API
You can inspect the entire catalog of supported models to find specific Claude model revisions or check real-time pricing. Run the CLI models command:
# List default thelastapi tiers and active lab models
vibiz ai models
# Search specifically for Anthropic Claude models
vibiz ai models claude --allYou can also retrieve the live catalog via GET /v1/ai/models. If you send a model name that is not recognized or mistyped, the API immediately rejects the request with a 400 invalid_request status code and flags param: "model", ensuring you never incur unexpected charges on an unintended model tier. For comprehensive gateway routing options, review the full AI documentation.
To start making Claude calls from your existing code, point your OpenAI client base URL to https://thelastapi.dev/v1/ai and request vibiz/smart or any supported model identifier.
Frequently asked questions
Can I use tool calling and JSON mode with Claude through the OpenAI Python SDK?
Yes. The gateway accepts OpenAI-compatible tool definitions, function schemas, and structured JSON outputs, routing them into Claude completions.
What happens if Anthropic has an outage while my app requests Claude?
The gateway returns a `503` error with `model_unavailable` or `provider_unavailable`. thelastapi never silently swaps your request to a different model or lab.
Do I need separate Anthropic API keys to call Claude through this SDK?
No. A single thelastapi account key (`vz_sk_...`) or project key (`vz_ai_...`) authenticates calls across all models, billing against your account credit balance.
How can I attribute Claude token costs to individual end users in Python?
Pass your user identifier in the `user` parameter of `client.chat.completions.create()`. Usage and spending will break down by individual user in `vibiz usage --by end_user`.