DEV Community

shashank ms
shashank ms

Posted on

Introduction to OpenAI SDK

The OpenAI SDK is the official client library for interacting with LLM APIs from Python and Node.js applications. It wraps raw HTTP requests, authentication headers, streaming parsers, and retry logic into a typed, object-oriented interface. If you are building chat interfaces, autonomous agents, or background inference pipelines, the SDK keeps your code concise and portable across providers.

What the OpenAI SDK Provides

The SDK exposes a unified interface for the full OpenAI API surface. You can generate chat completions, stream partial responses, call external tools via function calling, enforce structured output with JSON mode, and manage embeddings, image generation, audio transcription, and text-to-speech from a single client instance. It handles request serialization, SSE parsing for streams, exponential backoff, and error classification so your application does not need to manage raw HTTP plumbing.

Installation and Setup

Install the package from PyPI or npm. The only runtime requirements are your API key and, if you are switching to a compatible third-party provider, a custom base URL.

pip install openai
from openai import OpenAI

client = OpenAI(
    api_key="your-api-key"
    # base_url="https://api.oxlo.ai/v1"  # optional provider override
)

Core API Patterns

Chat completions are the primary entry point. A minimal request looks like this:

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello, world"}]
)
print(response.choices[0].message.content)

For real-time UIs, enable streaming to receive tokens as they are generated:

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Count to 10"}],
    stream=True
)

for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

The SDK also supports function calling, multi-turn conversation state, and vision inputs by following the same message schema regardless of which provider serves the request.

Provider Portability

A growing number of inference providers implement the OpenAI API specification. That means the SDK is no longer tied exclusively to OpenAI's platform. In most cases, moving your workload to another provider requires only two changes: the base_url and the API key. This portability is why many teams standardize on the OpenAI SDK even when they run open-source models.

Using Oxlo.ai as a Drop-In Backend

Oxlo.ai is a developer-first inference platform that is fully OpenAI SDK compatible. You can point your existing client to Oxlo.ai without rewriting request logic or changing payload shapes.

from openai import OpenAI

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key="your-oxlo.ai-api-key"
)

response = client.chat.completions.create(
    model="Llama 3.3 70B",
    messages=[{"role": "user", "content": "Explain agentic workflows"}]
)

Oxlo.ai hosts 45+ open-source and proprietary models across seven categories, including general-purpose LLMs such as Llama 3.3 70B and Qwen 3 32B, reasoning models such as DeepSeek R1 671B MoE and Kimi K2.6, code models, vision models, image generation, audio, and embeddings. Because the platform exposes the standard chat/completions, embeddings, images/generations, audio/transcriptions, and audio/speech endpoints, every feature shown above, streaming, function calling, JSON mode, and vision, works without modification.

Cost Model Advantages for Long-Context Work

Most token-based providers scale cost with prompt length. If you send long documents or maintain large conversation buffers for agents, your bill grows with every token in the context window. Oxlo.ai uses request-based pricing: one flat cost per API request regardless of how long the prompt is. For long-context and agentic workloads, this can make Oxlo.ai significantly cheaper than token-based alternatives. The platform also advertises no cold starts on popular models, which keeps latency predictable for interactive applications.

Pricing and Tiers

Oxlo.ai offers a free tier at $0 per month with 60 requests per day and access to 16+ free models, plus a 7-day full-access trial. Paid plans include Pro at $80 per month for 1,000 requests per day, Premium at $350 per month for 5,000 requests per day with priority queue access, and Enterprise custom contracts with unlimited requests and dedicated GPUs. Exact per-request rates are listed on the Oxlo.ai pricing page.

Summary

The OpenAI SDK is the de facto standard for LLM client code. It unifies chat, embeddings, audio, and image generation behind a single typed interface. Because the ecosystem now includes fully compatible providers, you can keep the SDK and swap the backend. Oxlo.ai offers an OpenAI SDK drop-in replacement with request-based pricing, 45+ models, and no cold starts. If your application relies on long contexts or high-frequency agentic calls, testing Oxlo.ai is a matter of changing the base_url to https://api.oxlo.ai/v1.

Top comments (0)