← All articles
AI Tools1 min read

Your First OpenAI CLI Requests

With the CLI set up, let's make real calls — a chat completion, a streamed response, and a scripted prompt you can pipe into other tools.

S

Swapnika Voora

Author

Once the OpenAI CLI is installed and authenticated, the terminal becomes a fast place to prototype prompts. Here are the calls you'll reach for most, from a one-off completion to something you can wire into a script.

A basic chat completion

The simplest useful call sends a prompt and prints the model's reply.

chat.sh
openai api chat.completions.create \
  -m gpt-4o-mini \
  -g user "Explain a Kafka consumer group in one sentence."

The -g flag adds a message with a role; repeat it to build a multi-turn conversation with system and assistant messages.

Script it with the SDK

For anything reusable, drop into Python. The SDK mirrors the CLI and is easier to compose.

summarize.py
from openai import OpenAI
 
client = OpenAI()  # reads OPENAI_API_KEY from the environment
 
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You summarize text concisely."},
        {"role": "user", "content": "Summarize: " + open("notes.txt").read()},
    ],
)
print(response.choices[0].message.content)

Pipe output into your workflow

Because the CLI writes to stdout, you can chain it with everyday Unix tools.

pipe.sh
cat error.log | openai api chat.completions.create \
  -m gpt-4o-mini \
  -g system "You are a log analyst." \
  -g user "$(cat -)" > analysis.txt

Takeaways

  • chat.completions.create with -g flags covers most interactive use.
  • Move to the Python SDK the moment a call needs to be reusable.
  • CLI output is just stdout, so it composes with pipes and redirects.

Keep model ids in a variable or config so you can swap models without editing every script.

#openai#cli#ai#example

More in AI Tools