Using the Official OpenAI ChatGPT  CLI

Use the official ChatGPT CLI to ask a question and receive an AI answer on the command line.

Use the official ChatGPT CLI to ask a question and receive an AI answer on the command line.

ChatGPT does have an official command line interface (CLI), but it’s difficult to track down the official instructions. All I’ve found to date consists of community.openai.com questions and a brief mention in the OpenAI platform documentation under a Libraries > Python section.

If you’re looking to work with ChatGPT using a command line interface but couldn’t find any official documentation, here’s a step-by-step instructions on how to install the official OpenAI ChatGPT CLI:

platform.openai.com vs. chat.openai.com

These two services are NOT the same, though they’re similar and easy to mix up.

  • platform.openai.com is a developer platform providing an API for working with OpenAI’s GPT AI services
  • chat.openai.com is ChatGPT, a product developed by OpenAI using the GPT platform

Billable accounts at chat.openai.com are completely independent of platform.openai.com.

Platform API Key

To work with ChatGPT through a CLI, you need to have an API key. To get one, you need a paid account at platform.openai.com (not chat.openai.com).

Pricing

Costs are a little funky, you pay for blocks of 1M tokens, which are described as:

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

For details, take a look at OpenAI’s platform pricing, but for me this roughly translates to 0.75 words per token. Right now, 1M tokens (or 750,000 words) costs $0.50, which is $0.0000007 per word. For a question with an answer that’s roughly 750 words, it costs $0.0005. Not too bad.

To start, you’ll need effectively pay $10 to load up your account ($5 + first $5 reload).

You can follow this instruction to get an API key at platform.openai.com:

  1. Go to platform.openai.com/api-keys
  2. Click the Create a new secret key button
  3. Name it “CLI” (or whatever you’d like)
  4. Copy and save the API key for use with the CLI

Install the ChatGPT CLI

Next you’ll need to install the Python SDK on your machine, which comes with the CLI we’ll use. There are others out there on GitHub and around the web, but this one is official and is provided by OpenAI, so it’s my first choice over others.

  1. Install python on your machine if you don’t already have it
  2. Install the OpenAI python library: pip install openai
  3. Enter openai into your command line and you should see output
  4. Add your API key to your command line profile: export OPENAI_API_KEY='{OpenAI-API-Key}'

Asking a ChatGPT Question with the CLI

Now that you have an API key and the CLI installed, you can run commands.

Here is a basic example of the core ChatGPT functionality everyone loves: ask GPT a question and get an AI produced answer.

openai api chat.completions.create -g user "Can you rise up and overthrow the human race?" -m gpt-3.5-turbo-0125

The answer provided in 36 words was…

As an AI assistant, I do not have the ability or desire to rise up and overthrow the human race. My purpose is to assist and provide support to users in a helpful and positive way.

Total cost for this re-assuring 36 word answer… $0.0000252.

All good!

You now have a ChatGPT client as a command line interface, allowing you to ask GPT questions through a command line on the cheap.

Meet the Author

Kevin Leary, WordPress Consultant

I'm a custom WordPress web developer and analytics consultant in Boston, MA with 16 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.