Implementation:Microsoft Semantic kernel AddOpenAIChatClient
| Knowledge Sources | |
|---|---|
| Domains | AI_Orchestration, Service_Connector_Architecture |
| Last Updated | 2026-02-11 19:00 GMT |
Overview
Concrete tool for registering an OpenAI chat completion service with the kernel builder provided by the Microsoft Semantic Kernel library.
Description
AddOpenAIChatClient is an extension method on IKernelBuilder that registers an OpenAI-backed chat completion client into the kernel's dependency injection container. It configures the connection to OpenAI's API using the specified model identifier and API key, and optionally accepts an organization ID, a service ID for multi-service scenarios, and a custom HttpClient for network-level customization.
Under the hood, this method creates an OpenAIClient with the provided credentials and registers it as an IChatClient (from the Microsoft.Extensions.AI abstraction layer) in the builder's IServiceCollection. This registration follows the Microsoft.Extensions.AI client pattern, which provides a standardized interface over multiple AI providers.
Usage
Use this method when your application needs to interact with OpenAI models for chat completion. Call it on the IKernelBuilder returned by Kernel.CreateBuilder() before calling Build(). If your application uses Azure-hosted OpenAI models, use the Azure-specific variant instead.
Code Reference
Source Location
- Repository: semantic-kernel
- File:
dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIKernelBuilderExtensions.ChatClient.cs:L28-70
Signature
public static IKernelBuilder AddOpenAIChatClient(
this IKernelBuilder builder,
string modelId,
string apiKey,
string? orgId = null,
string? serviceId = null,
HttpClient? httpClient = null)
Import
using Microsoft.SemanticKernel;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| builder | IKernelBuilder |
Yes | The kernel builder instance to register the service on (implicit via extension method). |
| modelId | string |
Yes | The OpenAI model identifier (e.g., "gpt-4o", "gpt-4o-mini"). |
| apiKey | string |
Yes | The API key for authenticating with OpenAI. |
| orgId | string? |
No | Optional OpenAI organization identifier for billing and access control. |
| serviceId | string? |
No | Optional unique identifier for this service registration, used to distinguish between multiple AI services in the same kernel. |
| httpClient | HttpClient? |
No | Optional custom HttpClient for controlling timeouts, proxies, and other HTTP-level behavior. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | IKernelBuilder |
The same builder instance, enabling fluent method chaining. |
Usage Examples
Basic Registration
using Microsoft.SemanticKernel;
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatClient(
modelId: "gpt-4o",
apiKey: "your-api-key")
.Build();
Registration with Organization ID
using Microsoft.SemanticKernel;
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatClient(
modelId: "gpt-4o",
apiKey: "your-api-key",
orgId: "org-abc123")
.Build();
Multi-Service Registration with Service IDs
using Microsoft.SemanticKernel;
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatClient(
modelId: "gpt-4o",
apiKey: "your-api-key",
serviceId: "primary")
.AddOpenAIChatClient(
modelId: "gpt-4o-mini",
apiKey: "your-api-key",
serviceId: "fast")
.Build();
Registration with Custom HttpClient
using Microsoft.SemanticKernel;
using System.Net.Http;
var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(120) };
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatClient(
modelId: "gpt-4o",
apiKey: "your-api-key",
httpClient: httpClient)
.Build();
Related Pages
Implements Principle
Requires Environment
- Environment:Microsoft_Semantic_kernel_DotNet_SDK_Environment
- Environment:Microsoft_Semantic_kernel_OpenAI_API_Environment