2 min read

Install the OpenAI SDK

Install the OpenAI SDK for Python and Node, configure your API key, and verify with a one-line chat.completions call.

Install the OpenAI SDK

The OpenAI SDK is the official client library for the OpenAI API. It's published for Python (openai) and Node (openai) — same API surface. This post installs both and verifies each with a one-line call.

You'll need an API key from platform.openai.com. New accounts get a small credit; pay-as-you-go after that.

Side note: the OpenAI SDK has become the de-facto interface for talking to many non-OpenAI models too. Ollama, LM Studio, vLLM, Together, Groq, and others all expose OpenAI-compatible endpoints. Knowing this SDK is useful even if you don't pay OpenAI directly.

Python

Install via uv inside a project:

# add the openai sdk to your project

uv add openai

For a one-off without a project:

# run a script with the sdk available

uv run --with openai python -c "from openai import OpenAI; print(OpenAI().chat.completions.create(model='gpt-4o-mini', messages=[{'role':'user','content':'hi'}]).choices[0].message.content)"

Legacy projects on pip work fine: pip install openai.

Node

Install via npm inside a project:

# add the openai sdk to your project

npm install openai

Works identically with pnpm and yarn.

Configure the API key

The SDK reads OPENAI_API_KEY from the environment. Don't hardcode it. For dev, use .env (and .gitignore it):

# create .env with your api key

echo 'OPENAI_API_KEY=sk-...' >> .env

For production, use a secret manager. Same advice as any other API key.

Verify (Python)

# verify python sdk with a one-line call

from openai import OpenAI; print(OpenAI().chat.completions.create(model="gpt-4o-mini", messages=[{"role":"user","content":"hi"}]).choices[0].message.content)

Save to verify.py, then uv run python verify.py.

Verify (Node)

// verify node sdk with a one-line call

import OpenAI from "openai"; const r = await new OpenAI().chat.completions.create({ model: "gpt-4o-mini", messages: [{ role: "user", content: "hi" }] }); console.log(r.choices[0].message.content);

Save to verify.mjs, then node verify.mjs.

Pointing the SDK at a different provider

To use the SDK against Ollama, LM Studio, or any OpenAI-compatible endpoint, override base_url (Python) or baseURL (Node) and pass any string as the API key:

# point the openai sdk at a local ollama server

from openai import OpenAI; client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")

See Install Ollama or Install LM Studio for the local server side.

Common gotchas

  • Wrong env var name: it's OPENAI_API_KEY, exactly. Some old tutorials use OPENAI_KEY — that won't work.
  • chat.completions vs responses: OpenAI now has two endpoints. chat.completions is the older, broader one most code uses; responses is newer and tool-use-first. Pick one per project, don't mix.
  • Model name drift: OpenAI deprecates models on a schedule. gpt-4o-mini is currently the cheapest reasonable default; check the docs before pinning.
  • Rate limits by tier: free credits = tier 1 = low RPM/TPM caps. Tight loops trip them. Use exponential backoff or batch via the Batch API for large jobs (cheaper too).
  • Streaming: .stream=True (Python) or .stream: true (Node) returns an iterable of chunks instead of a single response.

With verify.py or verify.mjs printing a reply, you're ready to call OpenAI — or any of the dozens of OpenAI-compatible providers — from the same code.

From the dictionary

Terms used in this post

Quick reference for the 4 terms you met above. Each one comes from the AI dictionary.

APIGeneral
Application Programming Interface. In LLM context: the HTTP endpoint a hosted model exposes (api.openai.com, api.anthropic.com). You send JSON, you get tokens back. The cloud-inference contract.
LM StudioAI
A GUI app for running local LLMs, wrapping llama.cpp with a chat interface and a model browser. Easier than Ollama for non-CLI users; same underlying engine. Useful for quick model evaluation; less useful for scripting or production-style workflows.
ModelML
In ML, a model is a file of learned numbers (parameters or weights) plus an architecture that tells the program how to use them. Loading a model means reading those numbers; running it means doing arithmetic with them.
OllamaAI
A wrapper around llama.cpp that makes running local LLMs a one-command operation. Pulls quantized GGUF models from a registry, exposes an HTTP API on localhost:11434, and handles model loading/unloading. The most common on-ramp to local inference in 2026.

Rate this article

How helpful did you find this?

Newsletter

Get new articles in your inbox

AI engineering, LLM systems, and software architecture — no filler.

No spam. Unsubscribe any time.

Discussion

Comments

Leave a note about the article, architecture choices, or what you would build next.

Comments are stored in Supabase and fetched per post slug.

Loading comments...