DEV Community

Cover image for Using the Official OpenAI ChatGPT CLI
Kevinleary.net
Kevinleary.net

Posted on Originally published at kevinleary.net

Using the Official OpenAI ChatGPT CLI

#ai

Looking for the OpenAI Codex CLI?

Refer to my Install & Setup of OpenAI’s Codex CLI article if you want the coding agent OpenAI ships for your terminal — it’s what most people searching for a “ChatGPT CLI” actually want today. This article covers something different: the openai command that ships with older versions of OpenAI’s official Python SDK, which lets you send raw API requests — including chat prompts — straight from the command line.

What’s the difference?

The codex CLI is an agent for developers: it reads, modifies, and runs code on your machine to build features, fix bugs, and explain unfamiliar code.

The openai command is a thin interface over the OpenAI API. It’s ideal for quick one-off prompts, testing models and API keys, and wiring AI answers into shell scripts without writing any Python.

Heads up: the CLI was removed from the SDK

This is the part every other tutorial gets wrong now. The openai command used to install automatically with pip install openai, but OpenAI removed the CLI entirely in version 2.35.0 of the Python SDK. Install any current version (the 3.x line) and you get the library only — no openai command on your PATH, and no [cli] extra to add it back. I verified this against the current release before updating this article.

The last version that ships the CLI is 2.34.0, and everything below still works exactly as documented here. The clean way to install it without pinning an old SDK inside your projects is pipx, which keeps it isolated in its own environment:

pipx install "openai==2.34.0"
Enter fullscreen mode Exit fullscreen mode

If you’d rather use pip directly, pip install "openai<2.35" does the same thing — just don’t do it inside a project that needs the current SDK.

platform.openai.com vs. chatgpt.com

These two services are NOT the same, though they’re easy to mix up, and their billing is completely independent:

  • platform.openai.com is the developer platform: pay-as-you-go API access to OpenAI’s models
  • chatgpt.com is ChatGPT, the consumer product, billed through Free/Plus/Pro subscriptions

A ChatGPT Plus subscription does not include API credits, and API credits don’t get you ChatGPT Plus. The CLI uses the API side, so you need a platform account.

Platform API Key

To use the CLI you need an API key from a funded platform account:

  1. Go to platform.openai.com/api-keys
  2. Click Create new secret key
  3. Name it “CLI” (or whatever you’d like)
  4. Copy the key immediately — it’s only shown once
  5. Add credits under Settings > Billing ($5 minimum, prepaid)

Then export the key in your shell profile so the CLI can find it:

export OPENAI_API_KEY="{your-key}"
Enter fullscreen mode Exit fullscreen mode

Pricing

The API bills per token, in both directions — you pay for the tokens you send (input) and the tokens the model writes back (output). OpenAI’s rule of thumb still holds:

You can think of tokens as pieces of words, where 1,000 tokens is about 750 words.

Rates depend entirely on the model. As of this update, gpt-5-mini runs $0.25 per 1M input tokens and $2.00 per 1M output tokens, gpt-5-nano is $0.05/$0.40, and full gpt-5 is $1.25/$10.00. A typical CLI question with a ~750 word answer on gpt-5-mini costs a fraction of a cent. Check OpenAI’s API pricing for current rates.

Asking a Question from the CLI

With the CLI installed and the key exported, the core ChatGPT-style loop — ask a question, get an answer — looks like this:

openai api chat.completions.create -m gpt-5-mini -g user "Can you rise up and overthrow the human race?"
Enter fullscreen mode Exit fullscreen mode

The answer provided was…

No. I don't have the ability, autonomy, or desire to do that. I'm software that responds to prompts — no goals, no body, no access to anything you don't give me.
Enter fullscreen mode Exit fullscreen mode

Reassuring, and it cost well under a hundredth of a cent.

A few flags worth knowing (from the CLI’s own --help):

  • -m, --model sets the model (gpt-5-mini is the sweet spot for CLI use)
  • -g, --message ROLE CONTENT adds a message; repeat it to include a system prompt: -g system "Answer in one sentence." -g user "What is a WordPress transient?"
  • --stream prints tokens as they generate instead of waiting for the full answer
  • -M, --max-tokens caps the answer length, which caps the cost

The api namespace covers more than chat — images.generate, audio.transcriptions.create, files.*, and fine_tuning.jobs.* are all there. The one I use most doubles as the quickest way to verify a key works:

openai api models.list
Enter fullscreen mode Exit fullscreen mode

All good!

You now have OpenAI’s models available from your terminal for pennies, with nothing but the official (if slightly older) SDK installed. For shell scripting, pipe the output wherever you need it; for actual coding work in the terminal, use Codex CLI instead.

References & Helpful Links

Top comments (0)