The OpenAI API provides access to GPT-4, GPT-4o, and other models through simple HTTP requests. Send messages, receive completions.
Basic usage: POST to https://api.openai.com/v1/chat/completions with model and messages array. Each message has a role (system, user, assistant) and content.
System prompts set the AI behavior: You are a helpful assistant. Temperature (0-2) controls randomness: 0 for deterministic, 2 for creative. max_tokens limits response length.
Streaming: set stream:true for real-time token delivery. Process SSE (Server-Sent Events) chunks for responsive UIs.
Rate limits and pricing vary by model and tier. Monitor usage in the OpenAI dashboard. Implement retry logic with exponential backoff.
Embeddings API creates vector representations for semantic search. Assistants API manages multi-turn conversations with file search, code interpreter, and function calling.