# OpenAIExtensions Methods

- Package: [Aspire.Hosting.OpenAI](/reference/api/csharp/aspire.hosting.openai.md)
- Type: [OpenAIExtensions](/reference/api/csharp/aspire.hosting.openai/openaiextensions.md)
- Kind: `Methods`
- Members: `5`

Provides extension methods for adding OpenAI Model resources to the application model.

## AddModel(IResourceBuilder<OpenAIResource>, string, string)

- Name: `AddModel(IResourceBuilder<OpenAIResource>, string, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<OpenAIModelResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.OpenAI/OpenAIExtensions.cs#L106-L136)

Adds an OpenAI Model child to the provided OpenAI resource.

```csharp
public static class OpenAIExtensions
{
    public static IResourceBuilder<OpenAIModelResource> AddModel(
        this IResourceBuilder<OpenAIResource> builder,
        string name,
        string model)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<OpenAIResource>`)
  The OpenAI resource builder.
- `name` (`string`)
  The name of the model resource. This name is used as the connection string name.
- `model` (`string`)
  The model identifier, e.g., "gpt-4o-mini".

## Returns

`IResourceBuilder<OpenAIModelResource>` -- The model resource builder.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AddOpenAI(IDistributedApplicationBuilder, string)

- Name: `AddOpenAI(IDistributedApplicationBuilder, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<OpenAIResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.OpenAI/OpenAIExtensions.cs#L25-L93)

Adds an OpenAI parent resource that can host multiple models.

```csharp
public static class OpenAIExtensions
{
    public static IResourceBuilder<OpenAIResource> AddOpenAI(
        this IDistributedApplicationBuilder builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the OpenAI resource.

## Returns

`IResourceBuilder<OpenAIResource>` -- The OpenAI resource builder.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithApiKey(IResourceBuilder<OpenAIResource>, IResourceBuilder<ParameterResource>)

- Name: `WithApiKey(IResourceBuilder<OpenAIResource>, IResourceBuilder<ParameterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<OpenAIResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.OpenAI/OpenAIExtensions.cs#L160-L176)

Configures the API key for the OpenAI parent resource from a parameter.

```csharp
public static class OpenAIExtensions
{
    public static IResourceBuilder<OpenAIResource> WithApiKey(
        this IResourceBuilder<OpenAIResource> builder,
        IResourceBuilder<ParameterResource> apiKey)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<OpenAIResource>`)
- `apiKey` (`IResourceBuilder<ParameterResource>`)

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithEndpoint(IResourceBuilder<OpenAIResource>, string)

- Name: `WithEndpoint(IResourceBuilder<OpenAIResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<OpenAIResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.OpenAI/OpenAIExtensions.cs#L147-L151)

Sets a custom OpenAI-compatible service endpoint URI on the parent resource.

```csharp
public static class OpenAIExtensions
{
    public static IResourceBuilder<OpenAIResource> WithEndpoint(
        this IResourceBuilder<OpenAIResource> builder,
        string endpoint)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<OpenAIResource>`)
  The OpenAI parent resource builder.
- `endpoint` (`string`)
  The endpoint URI, e.g., https://mygateway.example.com/v1.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithHealthCheck(IResourceBuilder<OpenAIModelResource>)

- Name: `WithHealthCheck(IResourceBuilder<OpenAIModelResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<OpenAIModelResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.OpenAI/OpenAIExtensions.cs#L203-L237)

Adds a health check to the OpenAI Model resource.

```csharp
public static class OpenAIExtensions
{
    public static IResourceBuilder<OpenAIModelResource> WithHealthCheck(
        this IResourceBuilder<OpenAIModelResource> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<OpenAIModelResource>`)
  The resource builder.

## Returns

`IResourceBuilder<OpenAIModelResource>` -- The resource builder.

## Remarks

This method adds a health check that verifies the OpenAI endpoint is accessible, the API key is valid, and the specified model is available. The health check will:

- Return `HealthStatus.Healthy` when the endpoint returns HTTP 200
- Return `HealthStatus.Unhealthy` with details when the API key is invalid (HTTP 401)
- Return `HealthStatus.Unhealthy` with error details when the model is unknown (HTTP 404)

Because health checks are included in the rate limit of the OpenAI API, it is recommended to use this health check sparingly, such as when you are having issues understanding the reason the model is not working as expected. Furthermore, the health check will run a single time per application instance.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
