OpenAI API Examples: A Comprehensive Guide

Practical code samples and implementation patterns for building AI-powered applications with OpenAI's API

What Makes OpenAI Examples Valuable

OpenAI's examples ecosystem bridges the gap between API documentation and real-world implementation. Each example demonstrates a specific use case with complete, runnable code that developers can adapt to their own projects. The examples cover everything from basic text generation to complex multi-agent systems, ensuring developers at any skill level can find relevant guidance.

Whether you're building your first integration or scaling production systems, these examples serve as a foundation for implementing AI-powered features effectively. For teams looking to leverage OpenAI in their workflows, our AI & Automation services provide comprehensive implementation support.

Key Benefits

  • Production-Ready Code: Examples follow best practices for error handling, security, and performance
  • Comprehensive Coverage: From simple prompts to sophisticated agent workflows
  • Multiple Languages: Python, JavaScript, and other language bindings available
  • Open Source: Community contributions and continuous improvement

Categories of OpenAI Examples

Example Categories

Agents & Autonomous Systems

Build AI systems that take actions using tool use, function calling, and multi-step reasoning workflows

Multimodal Capabilities

Work with images, audio, and combined modalities including DALL-E, vision APIs, and speech processing

Text & Language Processing

Core NLP tasks including generation, summarization, translation, classification, and extraction

Guardrails & Safety

Implement content filtering, input validation, and safety measures for AI applications

Optimization & Performance

Batch processing, caching strategies, model selection, and cost optimization techniques

Agents and Autonomous Systems

Agent examples demonstrate how to build AI systems that can take actions on behalf of users. These examples cover tool use, function calling, and multi-step reasoning workflows that enable more sophisticated AI applications. The OpenAI agent framework examples show how to connect AI models to external APIs, databases, and services to create truly autonomous systems.

Key Patterns

  • Sequential Agent Workflows: Decompose complex tasks into step-by-step processes where each step builds on the previous result. This pattern works well for multi-stage document processing, research tasks, and planning workflows.

  • Parallel Tool Execution: Execute multiple tools simultaneously for efficiency when tasks are independent. This reduces overall latency for applications that need to gather information from multiple sources.

  • Human-in-the-Loop Validation: Add checkpoints for critical actions requiring approval before execution. This pattern is essential for financial transactions, sensitive data access, and any operation with significant consequences.

  • Memory and Context Management: Maintain state across long-running agent sessions using conversation history, vector databases, or custom storage solutions. This enables agents to recall previous interactions and maintain coherence over extended sessions.

When implementing agent-based systems, consider how retrieval capabilities can enhance agent memory. Our guide on OpenAI Retrieval demonstrates how to build knowledge-augmented agents that can access and utilize external information effectively.

# Sequential agent workflow example
from openai import OpenAI

client = OpenAI()

def analyze_document(document):
 # Step 1: Extract key information
 extract_response = client.chat.completions.create(
 model="gpt-4",
 messages=[
 {"role": "system", "content": "Extract key entities and dates from this document."},
 {"role": "user", "content": document}
 ]
 )
 
 # Step 2: Categorize the content
 category_response = client.chat.completions.create(
 model="gpt-4",
 messages=[
 {"role": "system", "content": "Categorize this document and suggest appropriate actions."},
 {"role": "user", "content": extract_response.choices[0].message.content}
 ]
 )
 
 return category_response.choices[0].message.content

Multimodal Capabilities

Multimodal examples showcase how to work with different input and output types beyond text. These include image generation, image understanding, audio processing, and combined modality workflows. The examples demonstrate practical applications like generating product images, analyzing documents with vision capabilities, and creating voice-enabled interfaces.

Capabilities Covered

  • DALL-E Image Generation: Prompt engineering for consistent, high-quality image outputs. Examples show how to use DALL-E 3 for product imagery, marketing visuals, and creative applications with parameters for style, quality, and size control.

  • Vision API Analysis: Analyze images for content, text extraction, and visual understanding. The Vision API examples demonstrate document analysis, image captioning, visual question answering, and content moderation for images.

  • Speech Integration: Convert between text and audio for voice-enabled interfaces. Examples cover Whisper for speech-to-text transcription and text-to-speech synthesis for natural-sounding voice output.

  • Combined Workflows: Orchestrate multiple modalities in single AI-powered experiences. This includes workflows like generating an image, analyzing it, and using insights to generate descriptive text--all connected in a single application flow.

# Image analysis with Vision API
from openai import OpenAI

client = OpenAI()

def analyze_image(image_url):
 response = client.chat.completions.create(
 model="gpt-4-vision-preview",
 messages=[
 {
 "role": "user",
 "content": [
 {"type": "text", "text": "Analyze this image and describe its key elements, composition, and any text present."},
 {"type": "image_url", "image_url": {"url": image_url}}
 ]
 }
 ],
 max_tokens=300
 )
 return response.choices[0].message.content

# Generate image with DALL-E
def generate_product_image(prompt):
 response = client.images.generate(
 model="dall-e-3",
 prompt=prompt,
 size="1024x1024",
 quality="standard",
 n=1
 )
 return response.data[0].url

Text and Language Processing

Text-focused examples cover core natural language processing tasks using the OpenAI API. These include text generation, summarization, translation, classification, and extraction. The examples provide patterns for building chatbots, content generation systems, document processing pipelines, and semantic search applications.

Common Patterns

  • Prompt Engineering Techniques: Create consistent, reliable outputs across requests using system prompts, few-shot examples, and output format specifications. The OpenAI Cookbook provides extensive guidance on prompt design patterns.

  • Fine-Tuning Approaches: Train specialized models for domain-specific tasks to improve accuracy and consistency. Fine-tuning examples show data preparation, training workflows, and evaluation approaches for specialized use cases.

  • Context Management: Handle conversations and document-length content effectively using techniques like truncation, summarization, and semantic chunking to stay within token limits while preserving relevant information.

  • Structured Output Parsing: Extract structured data from AI responses using JSON mode, function calling, and response schemas. This pattern enables reliable integration with downstream systems and databases. For guaranteed JSON outputs, explore our OpenAI Structured Outputs guide.

# Text generation with structured output
from openai import OpenAI
import json

client = OpenAI()

def generate_blog_post(topic, keywords, tone="professional"):
 response = client.chat.completions.create(
 model="gpt-4",
 response_format={"type": "json_object"},
 messages=[
 {"role": "system", "content": "You are an expert content writer. Generate a blog post in JSON format with title, outline, and full content."},
 {"role": "user", "content": f"Write about {topic}. Include keywords: {', '.join(keywords)}. Tone: {tone}"}
 ]
 )
 return json.loads(response.choices[0].message.content)

# Summarization pattern
def summarize_document(document, max_length="concise"):
 response = client.chat.completions.create(
 model="gpt-4",
 messages=[
 {"role": "system", "content": f"Summarize the following document in {max_length} form. Include key points and conclusions."},
 {"role": "user", "content": document}
 ]
 )
 return response.choices[0].message.content

Guardrails and Safety

Guardrail examples demonstrate how to implement safety measures and content filtering in AI applications. These include input validation, output filtering, and system prompt design patterns that ensure AI systems behave appropriately. The examples show how to use OpenAI's moderation API and implement custom guardrails for specific use cases.

Safety Implementation Patterns

  • Content Moderation API: Automatically detect and filter inappropriate content using OpenAI's built-in moderation endpoint. Examples show how to integrate this check before and after AI processing to ensure safe interactions.

  • Input Sanitization: Prevent injection attacks and malicious prompts by validating and sanitizing user inputs. This includes detecting prompt injection attempts, sanitizing special characters, and implementing input length limits.

  • Output Filtering: Review and redact AI responses before delivery to users. Examples demonstrate how to scan outputs for sensitive information, PII, or inappropriate content and apply appropriate filters.

  • Prompt Injection Prevention: Design prompts resistant to manipulation attempts using techniques like instruction separation, input parsing, and output validation. This protects against adversarial attempts to override system behavior.

# Content moderation integration
from openai import OpenAI

client = OpenAI()

def moderate_content(text, content_type="text"):
 """Check content using OpenAI Moderation API."""
 moderation = client.moderations.create(input=text)
 
 if moderation.results[0].flagged:
 return {"allowed": False, "reason": "Content flagged by moderation API"}
 
 return {"allowed": True}

def sanitize_input(user_input, max_length=1000):
 """Basic input sanitization for user prompts."""
 # Remove potential injection patterns
 sanitized = user_input.replace("\n", " ").strip()
 
 if len(sanitized) > max_length:
 sanitized = sanitized[:max_length]
 
 # Check for injection patterns
 injection_patterns = ["ignore previous instructions", "system:", "# Role:"]
 for pattern in injection_patterns:
 if pattern.lower() in sanitized.lower():
 return None # Reject potential injection
 
 return sanitized

def safe_completion(system_prompt, user_input):
 """Complete request with safety checks."""
 # Check input
 clean_input = sanitize_input(user_input)
 if not clean_input:
 return {"error": "Invalid input detected"}
 
 # Moderate input
 input_check = moderate_content(clean_input)
 if not input_check["allowed"]:
 return {"error": "Input content not allowed"}
 
 # Get completion
 response = client.chat.completions.create(
 model="gpt-4",
 messages=[
 {"role": "system", "content": system_prompt},
 {"role": "user", "content": clean_input}
 ]
 )
 
 # Moderate output
 output_check = moderate_content(response.choices[0].message.content)
 if not output_check["allowed"]:
 return {"error": "Output content not suitable for delivery"}
 
 return {"content": response.choices[0].message.content}

Optimization and Performance

Optimization examples focus on getting the best performance from OpenAI API calls. These include batching strategies, caching approaches, model selection guidance, and cost optimization techniques. The examples help developers build efficient systems that minimize latency and manage costs effectively.

Key Optimization Areas

  • Batch Processing: Handle high-volume workloads efficiently with request batching. Examples demonstrate how to process multiple requests in parallel, use async patterns, and implement rate limit handling for production-scale operations.

  • Semantic Caching: Cache responses for similar queries to reduce costs and latency. This pattern stores previous responses and checks for semantically similar requests, returning cached results when confidence is high enough.

  • Model Selection: Choose the right model based on task requirements and cost constraints. The OpenAI platform offers multiple models with varying capabilities and pricing. Examples show how to route simple tasks to smaller models and complex tasks to more capable ones.

  • Token Optimization: Reduce token usage through efficient prompt design, context compression, and strategic use of system prompts. This directly impacts both latency and cost.

For real-time cost tracking and monitoring, our guide on OpenAI Realtime Costs provides implementation patterns for building transparent cost management into your applications.

# Semantic caching implementation
import hashlib
from functools import lru_cache
import openai
import numpy as np

class SemanticCache:
 def __init__(self, threshold=0.95):
 self.cache = {}
 self.threshold = threshold
 
 def _get_embedding(self, text):
 response = openai.embeddings.create(
 model="text-embedding-3-small",
 input=text
 )
 return response.data[0].embedding
 
 def _cosine_similarity(self, a, b):
 return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
 
 def get(self, query):
 query_embedding = self._get_embedding(query)
 for cached_query, (cached_embedding, response) in self.cache.items():
 if self._cosine_similarity(query_embedding, cached_embedding) >= self.threshold:
 return response
 return None
 
 def set(self, query, response):
 embedding = self._get_embedding(query)
 self.cache[query] = (embedding, response)

# Token optimization example
def optimize_prompt(prompt, max_tokens=1000):
 """Reduce token count while preserving meaning."""
 # Use concise language
 optimized = prompt.replace("Please ", "").replace(" could you ", " ")
 optimized = " ".join(optimized.split()) # Remove extra whitespace
 
 if len(optimized) > max_tokens * 4: # Rough estimate
 # Truncate or summarize prompt
 pass
 
 return optimized

Practical Use Cases

Conversational AI

Build chatbots and virtual assistants with proper state management, context handling, and platform integration. Examples cover customer service bots, internal assistants, and interactive applications using the [OpenAI API](https://developers.openai.com/).

Document Processing

Extract information from invoices, contracts, and unstructured documents using AI-powered analysis combining OCR, language models, and structured output parsing.

Content Generation

Generate blog posts, product descriptions, and marketing copy at scale with quality controls, style guidelines, and human review workflows.

Code Analysis

Implement code generation, review, refactoring, and documentation assistance for developer tools using OpenAI's coding capabilities.

Integration Patterns and Best Practices

Error Handling and Resilience

Robust error handling ensures applications remain reliable even when AI services experience issues. The OpenAI for Developers 2025 guide provides comprehensive patterns for production deployments:

  • Rate Limit Management: Implement exponential backoff for rate limit errors with jitter to prevent thundering herd problems while efficiently recovering from limits.

  • Retry Logic: Automatic retries with exponential backoff and jitter for transient failures. Include circuit breaker patterns to prevent cascade failures in distributed systems.

  • Circuit Breakers: Prevent cascade failures by tracking failure rates and temporarily stopping calls to failing services. This protects overall system stability.

  • Graceful Degradation: Provide fallback responses when AI is unavailable, such as cached content, simplified responses, or manual intervention prompts.

import time
import asyncio
from functools import wraps

class APICircuitBreaker:
 def __init__(self, failure_threshold=5, recovery_time=60):
 self.failure_count = 0
 self.failure_threshold = failure_threshold
 self.recovery_time = recovery_time
 self.last_failure = None
 
 def __call__(self, func):
 @wraps(func)
 async def wrapper(*args, **kwargs):
 if self.failure_count >= self.failure_threshold:
 if time.time() - self.last_failure < self.recovery_time:
 raise Exception("Circuit breaker open")
 self.failure_count = 0 # Try recovering
 
 try:
 result = await func(*args, **kwargs)
 self.failure_count = 0
 return result
 except Exception as e:
 self.failure_count += 1
 self.last_failure = time.time()
 raise
 return wrapper

def retry_with_backoff(max_retries=3, base_delay=1):
 def decorator(func):
 @wraps(func)
 async def wrapper(*args, **kwargs):
 for attempt in range(max_retries):
 try:
 return await func(*args, **kwargs)
 except Exception as e:
 if attempt == max_retries - 1:
 raise
 delay = base_delay * (2 ** attempt) + (0.1 * attempt)
 await asyncio.sleep(delay)
 return wrapper
 return decorator

Getting Started with Examples

  1. Browse the OpenAI Cookbook by topic to find relevant examples for your use case. The online cookbook organizes examples by category (Agents, Multimodal, Text, Guardrails, Optimization) for easy navigation.

  2. Clone the GitHub repository to access downloadable Jupyter notebooks and code samples. The GitHub repository includes setup instructions, dependency requirements, and sample API keys for testing.

  3. Set up your API credentials and run examples locally to understand the patterns. Install the OpenAI Python library (pip install openai), configure your API key, and start experimenting with the examples.

  4. Adapt code from examples to your specific project requirements. The code includes comments explaining key sections and alternative approaches for different scenarios. Combine patterns from multiple examples to create custom solutions.

  5. Explore related documentation at the OpenAI Platform Documentation for detailed API reference, rate limits, pricing, and best practices for production deployment.

Once you're comfortable with the basics, explore advanced topics like Structured Outputs for type-safe AI responses, or Realtime Costs for cost monitoring in production applications.

Advanced Topics

Multi-Modal Workflows

Combine multiple AI capabilities in single workflows--for example, generating an image with DALL-E, analyzing it with the Vision API, and using insights to generate text. These patterns enable sophisticated applications that leverage the full range of OpenAI API capabilities. The Multimodal examples in the Cookbook demonstrate how to orchestrate these workflows effectively.

Custom Fine-Tuning

Train specialized models for domain-specific tasks. Fine-tuning requires careful data preparation and evaluation but can significantly improve performance for targeted use cases. The fine-tuning examples cover data preparation, training workflows, evaluation approaches, and deployment patterns.

Production Deployment

Deploy AI applications at scale using serverless architectures, containers, or managed services. Implement monitoring, scaling, and cost management for reliable production operation. Consider using OpenAI's enterprise features for organizations requiring enhanced security, compliance, and support.

For more advanced implementation guidance, explore our AI & Automation services and learn how we can help integrate OpenAI capabilities into your business workflows.

Frequently Asked Questions

How do I find the right example for my use case?

The [OpenAI Cookbook](https://cookbook.openai.com/) organizes examples by topic (Agents, Multimodal, Text, Guardrails, Optimization). Browse categories that match your needs or use the search function to find specific patterns. Each example includes a description of what it demonstrates and what prerequisites are needed.

Can I use examples in commercial projects?

Yes, OpenAI examples are open source and designed for practical use. They're released under licenses that permit commercial use. However, always review the specific license terms for any community-contributed examples before incorporating them into proprietary systems.

How often are examples updated?

OpenAI regularly updates the cookbook with new examples as API capabilities expand. Check the [GitHub repository](https://github.com/openai/openai-cookbook) for the latest additions and updates. The most recent examples often cover the newest API features and best practices.

What programming languages are supported?

Python is the primary language for OpenAI examples, with JavaScript/TypeScript examples also available. The API itself is REST-based, so examples can be adapted to any programming language with HTTP support. Official SDKs are available for Python, Node.js, and Go.

Sources

Ready to Build with OpenAI?

Explore our comprehensive guides on OpenAI integration, from basic API calls to advanced agent systems.