Skip to main content

OpenAI Provider

The OpenAI provider gives you direct access to OpenAI’s latest models including GPT-4o, GPT-4, o1, o3, and GPT-3.5-Turbo. Use this provider when you want to work directly with OpenAI’s API rather than through Azure.

Provider Options

OpenAI integration offers multiple approaches:

Chat Client

Direct chat completions with streaming

Responses Client

Structured responses with tools

Assistants API

Persistent agents with code interpreter and file search

Installation

Authentication

OpenAI uses API keys for authentication:
.env

Explicit API Key

Never commit API keys to source control. Always use environment variables or secure secret management.

OpenAI Chat Client

The Chat Client provides direct access to OpenAI chat completions.

Basic Usage

Configuration

Streaming Responses

Function Calling

OpenAI Responses Client

The Responses Client provides structured response generation:

OpenAI Assistants API

The Assistants API provides persistent agents with managed state, code interpreter, and file search capabilities.

Creating an Assistant

Using an Existing Assistant

Code Interpreter

Enable code interpreter for Python code execution:
Enable file search for RAG capabilities:

Available Models

OpenAI offers several model families:
Reasoning models (o1, o3) do not support function calling or streaming. They are optimized for complex reasoning tasks.

Reasoning Models

OpenAI’s o1 and o3 models provide advanced reasoning capabilities:
Reasoning models do not support streaming or function calling. They are optimized for complex reasoning tasks that require extended thinking.

Vision Capabilities

GPT-4o and GPT-4-turbo support image inputs:

Structured Outputs

Force the model to return structured JSON outputs:

Best Practices

Never hardcode API keys in your source code:
  1. Use environment variables for API keys
  2. Use .env files locally (add to .gitignore)
  3. Use Azure Key Vault or similar for production
  4. Rotate API keys regularly
  5. Use project-scoped keys when possible
  • Use gpt-4o-mini for development and testing
  • Use gpt-4o for production workloads requiring maximum quality
  • Use gpt-3.5-turbo for simple tasks where cost is critical
  • Use o1/o3 for complex reasoning tasks that don’t need function calling
OpenAI has rate limits based on your tier:
  1. Implement exponential backoff retry logic
  2. Monitor usage in the OpenAI dashboard
  3. Consider upgrading your tier for higher limits
  4. Use batch processing for high-volume workloads
Tokens directly impact cost:
  1. Keep system prompts concise but clear
  2. Use smaller models when appropriate
  3. Implement conversation pruning for long sessions
  4. Monitor token usage in responses
  5. Use structured outputs to reduce verbose responses
When using the Assistants API:
  1. Delete assistants when no longer needed
  2. Clean up uploaded files
  3. Delete old threads to manage costs
  4. Use try-finally blocks to ensure cleanup

Troubleshooting

If you see API key errors:
  1. Verify the API key is correct and active
  2. Check if the key has expired
  3. Ensure the key has proper permissions
  4. Verify organization ID if using organization keys
If you’re hitting rate limits:
  1. Check your usage tier at platform.openai.com
  2. Implement exponential backoff
  3. Reduce request frequency
  4. Consider upgrading to a higher tier
  5. Use batch API for non-urgent requests
If the model is not available:
  1. Check if you have access to the model
  2. Verify the model name is correct
  3. Some models require waitlist access
  4. Check if your account tier supports the model
If function calling isn’t working:
  1. Verify the model supports function calling (o1/o3 don’t)
  2. Check function schema is valid JSON Schema
  3. Ensure function descriptions are clear
  4. Verify parameter types match the schema

Cost Optimization

OpenAI pricing varies by model. Here are tips to optimize costs:
  1. Use appropriate models: Don’t use GPT-4o when GPT-4o-mini or GPT-3.5-turbo will suffice
  2. Monitor token usage: Track input and output tokens to identify optimization opportunities
  3. Optimize prompts: Shorter, clearer prompts use fewer tokens
  4. Cache common responses: Cache responses for frequently asked questions
  5. Use batch API: For non-time-sensitive workloads, batch API is cheaper
Check current pricing at openai.com/pricing

Next Steps

Function Tools

Add function calling capabilities to your agents

Sessions & Memory

Manage multi-turn conversations with memory

Tools

Add function calling to your agents

Workflows

Build multi-agent workflows