OpenAI Provider
Connect to the OpenAI API (
api.openai.com) for chat completions, embeddings, image generation, and speech.
Quick Start
builder.Services
.AddCoreAIServices()
.AddCoreAIOrchestration()
.AddCoreAIOpenAI();
Services Registered
| Service | Implementation | Lifetime |
|---|---|---|
IAIClientProvider | OpenAIClientProvider | Scoped |
IAICompletionClient | OpenAICompletionClient | Scoped |
| Connection source | — | Scoped |
Configuration
Connection Setup
Provide an API key through your connection source:
builder.Services.AddCoreAIConnectionSource("OpenAI", options =>
{
options.Connections.Add(new AIProviderConnectionEntry
{
Name = "my-openai",
ClientName = "OpenAI",
// Set API key and optional endpoint
});
});
Constants
| Constant | Value |
|---|---|
OpenAIConstants.ClientName | "OpenAI" |
Capabilities
| Capability | Supported |
|---|---|
| Chat completions | ✅ |
| Streaming | ✅ |
| Embeddings | ✅ |
| Image generation | ✅ |
| Speech-to-text | ✅ |
| Text-to-speech | ✅ |
| Realtime (speech-to-speech) | ✅ (gpt-realtime) |
Realtime is exposed through IAIClientFactory.CreateRealtimeClientAsync (see
Real-time clients); the supported voices are returned by
IRealtimeVoiceResolver.
Text-to-speech is exposed through IAIClientFactory.CreateTextToSpeechClientAsync, and streams where the model
can: gpt-4o-mini-tts streams its audio, while tts-1 and tts-1-hd answer in one piece. ISpeechVoiceResolver
returns the model's voices: thirteen for gpt-4o-mini-tts (alloy, ash, ballad, cedar, coral, echo, fable, marin,
nova, onyx, sage, shimmer, verse) and nine for tts-1 and tts-1-hd (alloy, ash, coral, echo, fable, nova, onyx,
sage, shimmer). OpenAI produces raw PCM (16-bit mono) at 24 kHz only: ask for pcm, or for Pcm<rate> as a
cascaded realtime deployment does. Pcm24000
is passed through as OpenAI's pcm. Any other rate (Pcm8000 or Pcm16000 for a phone line, for example) is asked
for at 24 kHz and converted to the rate asked for as it streams, with a filter that removes what a lower rate cannot
carry. So an OpenAI model can be the speaking leg of a cascade at any output rate.
A request can also say how fast and how to speak. TextToSpeechOptions.Speed is sent as the API's speed, kept
within the 0.25 to 4 OpenAI accepts. A speaking style, given as the TextToSpeechOptionsProperties.Instructions
additional property, is sent as the model's instructions ("Speak at a brisk, natural phone pace"); tts-1 and
tts-1-hd take no instructions, so it is left out for them. A cascade passes its SpeechSpeed and SpeakingStyle
this way (see pace and speaking style).
Configuration Example
A full appsettings.json configuration for OpenAI:
{
"CrestApps": {
"AI": {
"Connections": [
{
"Name": "openai-primary",
"ClientName": "OpenAI",
"ApiKey": "sk-..."
}
]
}
}
}
Or register connections programmatically:
builder.Services.AddCoreAIConnectionSource("OpenAI", options =>
{
options.Connections.Add(new AIProviderConnectionEntry
{
Name = "my-openai",
ClientName = "OpenAI",
// API key is read from configuration or set directly
});
});
OpenAI-compatible endpoints
The OpenAI client is often useful beyond api.openai.com. It can also connect to providers that expose OpenAI-compatible chat and embedding endpoints.
Common examples include:
| Provider | Notes |
|---|---|
| OpenAI | Native target for this client |
| Azure OpenAI | OpenAI-compatible request shape with Azure-specific endpoint and deployment conventions |
| Anthropic | Provides OpenAI-compatible endpoints in addition to its native Claude API |
| Google Gemini | Offers an OpenAI-compatibility layer for supported Gemini models |
| Groq | OpenAI-compatible chat/completions API |
| Mistral | OpenAI-compatible API surface for supported models |
| xAI | OpenAI-compatible API for Grok models |
| Together AI | OpenAI-compatible endpoints for hosted open-weight models |
| Fireworks AI | OpenAI-compatible endpoints for hosted open-weight models |
| OpenRouter | OpenAI-compatible multi-provider routing layer |
| DeepSeek | OpenAI-compatible chat and embedding-style endpoints |
| Perplexity | OpenAI-compatible chat/completions endpoint |
| Ollama / vLLM / LocalAI | Common self-hosted OpenAI-compatible endpoints |
Some providers expose both OpenAI-compatible and native APIs. Anthropic is one example: you can use its OpenAI-compatible endpoint through this client, or use the dedicated Claude Orchestrator when you want the native Claude-oriented integration path.
Chat API
Reasoning models such as gpt-6-luna reject function tools together with a reasoning effort on /v1/chat/completions and only accept that combination on the Responses API (/v1/responses). The connection's optional ChatApi setting decides where those requests go:
ChatApi | Behavior |
|---|---|
Auto (default) | On OpenAI's own endpoint (no Endpoint, or host api.openai.com or a regional host such as eu.api.openai.com), a request that carries function tools and a reasoning effort other than None goes to the Responses API. Every other endpoint uses Chat Completions only. |
ChatCompletions | Every request goes to Chat Completions. |
Responses | Requests with function tools and a reasoning effort go to the Responses API on any endpoint. Use it for OpenAI-compatible servers that implement /v1/responses, such as OpenRouter or xAI. |
Values are not case-sensitive. An unknown value is logged as a warning and treated as Auto. All other requests stay on Chat Completions. Responses requests are sent with store: false, so conversations are not kept on the provider's servers. A reasoning deployment must declare the reasoningEffort parameter: without it no effort is sent, the model applies its own default, and Chat Completions rejects the tools.
{
"CrestApps": {
"AI": {
"Connections": [
{
"Name": "openrouter",
"ClientName": "OpenAI",
"Endpoint": "https://openrouter.ai/api/v1",
"ApiKey": "...",
"ChatApi": "Responses"
}
]
}
}
}
Never commit API keys to source control. Use environment variables, user secrets, or a vault provider for the values you map into your CrestApps:AI:Connections configuration.
Available Models
| Model | Type | Context Window | Best For |
|---|---|---|---|
gpt-4.1 | Chat | 1M tokens | Complex reasoning, coding, instruction following |
gpt-4.1-mini | Chat | 1M tokens | Balanced performance and cost |
gpt-4.1-nano | Chat | 1M tokens | Fast, cost-effective for simple tasks |
o4-mini | Reasoning | 200K tokens | STEM, math, coding with chain-of-thought |
gpt-4o | Chat | 128K tokens | Multimodal (text + vision), general purpose |
gpt-4o-mini | Chat | 128K tokens | Budget-friendly multimodal |
text-embedding-3-small | Embedding | 8K tokens | Cost-effective embeddings |
text-embedding-3-large | Embedding | 8K tokens | Higher-quality embeddings |
dall-e-3 | Image | — | Image generation |
whisper-1 | Speech-to-text | — | Audio transcription |
tts-1 / tts-1-hd | Text-to-speech | — | Voice synthesis |
gpt-4o-mini-tts | Text-to-speech | — | Streamed voice synthesis; can be a cascaded realtime deployment's speaking leg |
Model availability and capabilities change frequently. Check the OpenAI models documentation for the latest information.
Streaming
The OpenAI provider fully supports streaming responses. When streaming is enabled, tokens are sent to the client as they are generated rather than waiting for the complete response:
// Streaming is handled automatically by the orchestrator when the
// chat interaction is configured for streaming (the default for real-time chat).
// No additional configuration is needed.
Streaming is the default behavior for the chat interactions module. The OpenAICompletionClient uses the IChatClient.GetStreamingResponseAsync() method from the Microsoft.Extensions.AI abstraction.
Function Calling
OpenAI models support function calling (tool use), which is the foundation for the Custom AI Tools system. When tools are registered and assigned to a profile, the OpenAI provider automatically:
- Serializes tool definitions as JSON Schema in the request
- Parses tool call responses from the model
- Invokes the matching
AIToolvia the orchestrator - Sends tool results back to the model for the final response
All GPT-4 and newer models support parallel function calling (multiple tools invoked in a single turn).