Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/openai/openai-cookbook/llms.txt

Use this file to discover all available pages before exploring further.

The fastest path to working with the OpenAI API is a single Python script — no framework, no boilerplate. This guide walks you through getting an API key, installing the SDK, and making your first request, so you have a working baseline before you dive into any cookbook example.

Prerequisites

  • An OpenAI account (free to create)
  • Python 3.9+ or Node.js 18+ installed locally
  • A terminal
1

Get your API key

Sign in to the OpenAI platform and navigate to API keys in the left sidebar. Click Create new secret key, give it a name, and copy the value — you won’t be able to see it again.
Never commit your API key to a repository or paste it into code. Always load it from an environment variable or a secrets manager.
2

Set the environment variable

Export your key so the SDK can pick it up automatically at runtime.
export OPENAI_API_KEY="sk-..."
Add the export line to your shell profile (.zshrc, .bashrc, etc.) or create a .env file at the root of your project — most notebooks and IDEs load .env files automatically.
3

Install the SDK

Install the official OpenAI library for your language.
pip install openai
4

Make your first API call

Run the snippet below. If you see a greeting in the output, your setup is working.
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY from environment

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Say hello!"}]
)
print(response.choices[0].message.content)
Expected output:
Hello! How can I help you today?
5

Explore cookbook examples

With a working connection to the API, you’re ready to explore any notebook in the cookbook. Clone the repository and open it in Jupyter.
git clone https://github.com/openai/openai-cookbook.git
cd openai-cookbook
python -m venv .venv && source .venv/bin/activate
pip install jupyter
jupyter lab
Each notebook installs its own dependencies from a local requirements.txt. Check the notebook’s first cell for any additional setup steps.

Understanding the response object

The create call returns a ChatCompletion object. The fields you’ll use most often are:
FieldDescription
response.choices[0].message.contentThe model’s reply as a plain string
response.choices[0].finish_reasonWhy the model stopped: stop, length, or tool_calls
response.usage.prompt_tokensNumber of tokens in your input
response.usage.completion_tokensNumber of tokens in the model’s reply
response.modelThe exact model version that handled the request

Common parameters

response = client.chat.completions.create(
    model="gpt-4o",           # model to use
    messages=[...],            # conversation history
    temperature=0.7,           # randomness: 0 = deterministic, 2 = very random
    max_tokens=512,            # cap on reply length
    response_format={"type": "json_object"},  # force JSON output
)
The messages parameter is a list, so you can pass a full conversation history — not just a single user message. Include a system message at the start to set the model’s behavior and persona.

Next steps

Agents & Automation

Go beyond single-turn calls: build agents that use tools, hand off tasks, and maintain state across many steps.

Embeddings & Search

Convert text to vectors for semantic search, clustering, and recommendation systems.

Fine-tuning

Train a model on your own examples to improve accuracy for a specific task.

Contribute an example

Found a useful pattern? Share a notebook with the community.

Build docs developers (and LLMs) love