Tutorial · 2026

OpenAI API Tutorial: Build Your First AI App

Step-by-step guide to using the OpenAI API in Python. Get from zero to a working AI app in under an hour — with real code, cost tips, and next steps.

Getting Started

First, get your API key from platform.openai.com, then install the SDK:

pip install openai

Set your API key as an environment variable (never hardcode it):

export OPENAI_API_KEY="sk-..."

Your First API Call

from openai import OpenAI

client = OpenAI()  # uses OPENAI_API_KEY env var

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user",   "content": "Explain RAG in one paragraph."},
    ],
    max_tokens=200,
)

print(response.choices[0].message.content)

Key Concepts

Models

gpt-4o is the flagship. gpt-4o-mini is cheaper and good enough for most tasks. Use gpt-4o for complex reasoning, gpt-4o-mini for everything else.

Tokens

LLMs process text as tokens (~¾ of a word each). You pay per token. Use max_tokens to cap output length and prevent surprises.

Temperature

Controls randomness. 0 = deterministic, 1 = creative. Use 0–0.3 for factual tasks, 0.7–1.0 for creative writing.

System prompt

Sets the model's persona and behavior for the whole conversation. This is where you inject instructions, context, and output format requirements.

Common Use Cases

Chatbots

Multi-turn conversations using the messages array. Add assistant messages to maintain history.

Summarization

Send a long document in the user message. Prompt: 'Summarize this in 3 bullet points'.

Classification

Ask the model to classify text into categories. Works best with few-shot examples in the prompt.

Embeddings

Use text-embedding-3-small to turn text into vectors for semantic search and RAG.

Function calling

Structured outputs — get JSON back from the model. Define a schema and the API returns data matching it.

Cost Management Tips

Use gpt-4o-mini for development — it's ~15x cheaper than gpt-4o and good enough for most tasks

Set max_tokens limits on every call to cap output length

Cache responses for identical queries using a simple dict or Redis

Use streaming for long outputs (better UX and fails gracefully)

Batch similar requests instead of one-at-a-time API calls

Frequently Asked Questions

How do I get an OpenAI API key?

Sign up at platform.openai.com, go to the API Keys section, and click 'Create new secret key'. You'll need to add a payment method and purchase at least $5 of credits to start. Keep your key secret — never commit it to Git or expose it in client-side code.

Is the OpenAI API free?

There's no permanent free tier, but new accounts get $5 in free credits to start. After that, you pay per token. GPT-4o-mini is very affordable (around $0.15 per million input tokens). For development, $5–10 goes a long way if you use the mini models and cache responses.

What's the difference between GPT-4o and GPT-4o-mini?

GPT-4o is the flagship model — best quality, higher cost. GPT-4o-mini is much cheaper (about 15x) and suitable for most production use cases: summarization, classification, chat, code generation. Use GPT-4o for complex reasoning tasks, GPT-4o-mini for everything else.

How do I reduce OpenAI API costs?

Use GPT-4o-mini instead of GPT-4o for most tasks. Set max_tokens limits to prevent runaway responses. Cache responses for repeated queries using Redis or a simple dict. Use system prompts efficiently (don't repeat context on every call). Batch requests where possible.

What to build next?

Explore 20+ AI project tutorials — from RAG chatbots to AI agents, all with step-by-step code.

Browse AI Projects →