OpenAI CLI Cheatsheet
The OpenAI CLI is the most direct tool for interacting with the OpenAI API. Install the `openai` Python package, set your API key via `OPENAI_API_KEY`, and start using chat completions, image generation (DALL-E), audio transcription/synthesis (Whisper/TTS), embeddings, file management, and fine-tuning — all from the command line.
Quick Start
``bash
# Install
pip install openai
# Set API Key export OPENAI_API_KEY=sk-proj-xxx
# List available models openai api models.list
# Basic chat openai api chat.completions.create -m gpt-4o -g user "Hello!"
# Stream a response openai api chat.completions.create -m gpt-4o -g user "Tell me a story" --stream
# Generate an image
openai api images.generate -p "A cat astronaut on Mars"
`
Authentication
The CLI authenticates via the OPENAI_API_KEY environment variable. You can also pass -k to override temporarily. For Azure OpenAI, use -t azure with Azure-specific flags.
Chat Completions
The core feature. Use openai api chat.completions.create with -m for model and -g for messages (can be specified multiple times for multi-turn). System prompt: use -g system 'instructions'. Streaming: add --stream.
Image Generation (DALL-E)
Use openai api images.generate with -p (prompt), -n (count, 1-10), and -s (size: 1024x1024, 1792x1024, etc.). Edit existing images with openai api images.edit.
Audio (Whisper & TTS)
Transcribe with openai api audio.transcriptions.create -f audio.mp3. Translate non-English to English with openai api audio.translations.create. Generate speech with openai api audio.speech.create --input "Hello" --voice alloy.
Embeddings
Create text embeddings for RAG and semantic search with openai api embeddings.create -m text-embedding-3-small -i "Your text".
Fine-tuning
Upload training data → create job → monitor status → use the fine-tuned model ID like any other model.
File Management
Files are used for fine-tuning and batch processing. Manage them with openai api files.list, files.create, files.retrieve, and files.delete`.
Startup & Modes(12)
| Command | Level | ||
|---|---|---|---|
openai --helpShow help information | Basic | openai --help | |
openai -kSpecify API key (overrides env var temporarily) | Basic | openai -k sk-xxx api models.list | |
openai -bSpecify API base URL (compatible with third-party APIs) | Intermediate | openai -b https://api.openai.com/v1 api models.list | |
openai -oSpecify organization ID | Intermediate | openai -o org-xxx api models.list | |
export OPENAI_API_KEYSet API key environment variable | Basic | export OPENAI_API_KEY=sk-proj-xxx | |
export OPENAI_BASE_URLSet custom API endpoint (third-party compatible) | Intermediate | export OPENAI_BASE_URL=https://api.example.com/v1 | |
openai api models.listList all available models | Basic | openai api models.list | |
openai api models.retrieveRetrieve detailed info about a specific model | Basic | openai api models.retrieve gpt-4o | |
openai api moderations.createCheck content policy compliance | Intermediate | openai api moderations.create -i 'I want to harm someone' | |
openai api completions.createCreate text completion (legacy API) | Intermediate | openai api completions.create -m gpt-3.5-turbo-instruct -p 'Once upon a time' -M 100 | |
curl -X POST https://api.openai.com/v1/chat/completionsCall Chat Completions API with curl | Intermediate | curl -s https://api.openai.com/v1/chat/completions -H 'Content-Type: application/json' -H 'Authorization: Bearer $OPENAI_API_KEY' -d '{"model":"gpt-4o","messages":[{"role":"user","content":"Hello"}]}'
| |
openai -VCheck openai CLI version | Basic | openai -V |
Chat Commands(8)
| Command | Level | ||
|---|---|---|---|
openai api chat.completions.createCreate a chat completion (requires model and messages) | Basic | openai api chat.completions.create -m gpt-4o -g user 'Hello!' | |
openai api chat.completions.create -mSpecify model for chat | Basic | openai api chat.completions.create -m gpt-4o -g user 'What is AI?' | |
openai api chat.completions.create -gAdd multi-turn messages (use multiple times) | Basic | openai api chat.completions.create -m gpt-4o -g user 'Hi' -g assistant 'Hello!' -g user 'How are you?' | |
openai api chat.completions.create -g systemSet system prompt (system message) | Intermediate | openai api chat.completions.create -m gpt-4o -g system 'You are a helpful assistant' -g user 'Hello' | |
openai api chat.completions.create --streamStream chat completion output | Intermediate | openai api chat.completions.create -m gpt-4o -g user 'Tell me a story' --stream | |
openai api chat.completions.create -tSet temperature (0-2, default 1) | Intermediate | openai api chat.completions.create -m gpt-4o -g user 'Explain quantum' -t 0.3 | |
openai api chat.completions.create -MSet maximum output tokens | Intermediate | openai api chat.completions.create -m gpt-4o -g user 'Write essay' -M 2048 | |
openai api chat.completions.create -nGenerate multiple responses to choose from | Intermediate | openai api chat.completions.create -m gpt-4o -g user 'Solve math' -n 3 |
ai-image(4)
| Command | Level | ||
|---|---|---|---|
openai api images.generateGenerate images from text (DALL-E) | Basic | openai api images.generate -p 'A cat astronaut on Mars' -n 1 -s 1024x1024 | |
openai api images.generate -pSpecify image prompt | Basic | openai api images.generate -p 'Cyberpunk city at night, neon lights' -n 2 | |
openai api images.generate -sSpecify image size | Basic | openai api images.generate -p 'Mountain landscape' -s 1792x1024 | |
openai api images.editEdit existing image (based on mask) | Expert | openai api images.edit -i input.png -m mask.png -p 'Add a flower' |
ai-audio(3)
| Command | Level | ||
|---|---|---|---|
openai api audio.transcriptions.createTranscribe audio to text (Whisper) | Basic | openai api audio.transcriptions.create -f recording.mp3 -m whisper-1 | |
openai api audio.translations.createTranslate non-English audio to English | Basic | openai api audio.translations.create -f chinese_speech.mp3 -m whisper-1 | |
openai api audio.speech.createConvert text to speech (TTS) | Basic | openai api audio.speech.create --input 'Hello world' --voice alloy --model tts-1 |
ai-embedding(1)
| Command | Level | ||
|---|---|---|---|
openai api embeddings.createCreate text embeddings | Intermediate | openai api embeddings.create -m text-embedding-3-small -i 'Hello world' |
File Management(4)
| Command | Level | ||
|---|---|---|---|
openai api files.listList uploaded files | Basic | openai api files.list | |
openai api files.createUpload a file (for fine-tuning or batch processing) | Intermediate | openai api files.create -f training_data.jsonl -p fine-tune | |
openai api files.retrieveRetrieve file details | Basic | openai api files.retrieve file-xxx | |
openai api files.deleteDelete an uploaded file | Basic | openai api files.delete file-xxx |
ai-training(6)
| Command | Level | ||
|---|---|---|---|
openai api fine_tuning.jobs.createCreate a fine-tuning job | Expert | openai api fine_tuning.jobs.create -t file-xxx -m gpt-4o-mini | |
openai api fine_tuning.jobs.listList all fine-tuning jobs | Intermediate | openai api fine_tuning.jobs.list | |
openai api fine_tuning.jobs.retrieveCheck fine-tuning job status | Intermediate | openai api fine_tuning.jobs.retrieve ftjob-xxx | |
openai api fine_tuning.jobs.cancelCancel a running fine-tuning job | Intermediate | openai api fine_tuning.jobs.cancel ftjob-xxx | |
openai api fine_tuning.jobs.list_eventsView fine-tuning job event logs | Intermediate | openai api fine_tuning.jobs.list_events ftjob-xxx | |
openai tools fine_tunes.prepare_dataValidate and prepare fine-tuning data file | Expert | openai tools fine_tunes.prepare_data -f training_data.jsonl |