# OpenAIProviderExtensions

> Tracon documentation. Tracon is a .NET package family that adds a control
> plane on Microsoft Agent Framework, published as NuGet packages that run
> inside your own ASP.NET Core application.
>
> Page: https://tracon.dev/api/tracon.openaiproviderextensions/
> Summary: OpenAIProviderExtensions: Extensions that add the OpenAI provider to the Tracon chain.

<div class="api-provenance">Namespace Tracon · Assembly <code>Tracon.OpenAI.dll</code></div>

Extensions that add the OpenAI provider to the Tracon chain.

```csharp
public static class OpenAIProviderExtensions
```

<dl class="api-relations"><dt>Inheritance</dt><dd><a href="https://learn.microsoft.com/dotnet/api/system.object">object</a> ← <a href="/api/tracon.openaiproviderextensions/">OpenAIProviderExtensions</a></dd><dt>Inherited Members</dt><dd><a href="https://learn.microsoft.com/dotnet/api/system.object.gettype">object.GetType()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone">object.MemberwiseClone()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.tostring">object.ToString()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)">object.Equals(object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)">object.Equals(object?, object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.referenceequals">object.ReferenceEquals(object?, object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.gethashcode">object.GetHashCode()</a></dd></dl>

## Methods

### <a id="Tracon_OpenAIProviderExtensions_UseOpenAI_Tracon_ITraconBuilder_System_String_System_Action_Tracon_OpenAIProviderOptions__"></a> UseOpenAI\(ITraconBuilder, string, Action<OpenAIProviderOptions\>?\)

Adds the OpenAI provider with the given API key.

```csharp
public static ITraconBuilder UseOpenAI(this ITraconBuilder builder, string apiKey, Action<OpenAIProviderOptions>? configure = null)
```

#### Parameters

`builder` [ITraconBuilder](/api/tracon.itraconbuilder/)

The Tracon chain.

`apiKey` [string](https://learn.microsoft.com/dotnet/api/system.string)

The OpenAI API key.

`configure` [Action](https://learn.microsoft.com/dotnet/api/system.action\-1)<[OpenAIProviderOptions](/api/tracon.openaiprovideroptions/)\>?

An extra options callback.

#### Returns

 [ITraconBuilder](/api/tracon.itraconbuilder/)

The same chain, for chaining.

#### Remarks

The key is passed in by the caller; Tracon never reads
`IConfiguration` on its own, and the key is never written to the
database.

```csharp
builder.AddTracon()
       .UseOpenAI(builder.Configuration["OpenAI:ApiKey"]!);
```

#### Exceptions

 [ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)

`builder` is [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).

 [ArgumentException](https://learn.microsoft.com/dotnet/api/system.argumentexception)

`apiKey` is empty.

### <a id="Tracon_OpenAIProviderExtensions_UseOpenAI_Tracon_ITraconBuilder_Microsoft_Extensions_Configuration_IConfiguration_"></a> UseOpenAI\(ITraconBuilder, IConfiguration\)

Adds the OpenAI provider with options read from the
`Tracon:Providers:OpenAI` section.

```csharp
public static ITraconBuilder UseOpenAI(this ITraconBuilder builder, IConfiguration configurationSection)
```

#### Parameters

`builder` [ITraconBuilder](/api/tracon.itraconbuilder/)

The Tracon chain.

`configurationSection` [IConfiguration](https://learn.microsoft.com/dotnet/api/microsoft.extensions.configuration.iconfiguration)

The section the options are read from. Usually
`configuration.GetSection(OpenAIProviderOptions.SectionName)`.

#### Returns

 [ITraconBuilder](/api/tracon.itraconbuilder/)

The same chain, for chaining.

#### Exceptions

 [ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)

Any parameter is [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).

### <a id="Tracon_OpenAIProviderExtensions_UseOpenAI_Tracon_ITraconBuilder_System_Action_Tracon_OpenAIProviderOptions__"></a> UseOpenAI\(ITraconBuilder, Action<OpenAIProviderOptions\>\)

Adds the OpenAI provider with options set in code.

```csharp
public static ITraconBuilder UseOpenAI(this ITraconBuilder builder, Action<OpenAIProviderOptions> configure)
```

#### Parameters

`builder` [ITraconBuilder](/api/tracon.itraconbuilder/)

The Tracon chain.

`configure` [Action](https://learn.microsoft.com/dotnet/api/system.action\-1)<[OpenAIProviderOptions](/api/tracon.openaiprovideroptions/)\>

The options callback.

#### Returns

 [ITraconBuilder](/api/tracon.itraconbuilder/)

The same chain, for chaining.

#### Remarks

This call registers two providers: [OpenAIProviderNames.ChatCompletions](/api/tracon.openaiprovidernames/#Tracon_OpenAIProviderNames_ChatCompletions)
and [OpenAIProviderNames.Responses](/api/tracon.openaiprovidernames/#Tracon_OpenAIProviderNames_Responses). The agent definition picks the one
it uses through [ModelBinding.Provider](/api/tracon.modelbinding/#Tracon_ModelBinding_Provider).

Registration goes through `AddModelProvider(...)`; no existing service is
*replaced*. `UsePostgreSql` uses `Replace` because it takes the
place of the existing stores; this call *adds* a new provider, so it has no
such need.

When it is called more than once, the options are merged; the providers are
registered only once.

#### Exceptions

 [ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)

Any parameter is [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).
