> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# .NET AI SDK reference

> This topic documents how to get started with the .NET AI SDK, and links to reference information on all of the supported features.

> For the complete documentation index, see [llms.txt](/llms.txt). For the full documentation snapshot, see [llms-full.txt](/llms-full.txt).

## Overview

This topic documents how to get started with the .NET AI SDK, and links to reference information on all of the supported features.

The .NET AI SDK is designed for use with LaunchDarkly's AI Configs. It is in a pre-1.0 release and the API may change based on feedback. Active feature development is ongoing in the [Python](/sdk/ai/python) and [Node.js](/sdk/ai/node-js) AI SDKs, so this SDK receives new features at a slower pace. You can follow development or contribute on [GitHub](https://github.com/launchdarkly/dotnet-core/tree/main/pkgs/sdk/server-ai).

<Note title="SDK quick links">
  LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:
</Note>

| Resource | Location |
| - | - |
| SDK API documentation | [SDK API docs](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server-ai) |
| GitHub repository | [server-ai](https://github.com/launchdarkly/dotnet-core/tree/main/pkgs/sdk/server-ai/) |
| Sample application | |
| Published module | [NuGet](https://www.nuget.org/packages/LaunchDarkly.ServerSdk.Ai/) |

<Warning title="For use in server-side applications only">
  This SDK is intended for use in multi-user .NET server applications. To learn more about LaunchDarkly's different SDK types, read [Choosing an SDK type](/sdk/concepts/client-side-server-side).
</Warning>

## Get started

LaunchDarkly AI SDKs interact with [AI Configs](/home/getting-started/vocabulary#ai-config). AI Configs are the LaunchDarkly resources that manage model configurations and messages for your generative AI applications.

<Tip title="Try the Quickstart">
  This reference guide describes working specifically with the .NET AI SDK. For a complete introduction to LaunchDarkly AI SDKs and how they interact with AI Configs, read [Quickstart for AI Configs](/home/ai-configs/quickstart).
</Tip>

You can use the .NET AI SDK to customize your AI Config based on the [context](/home/getting-started/vocabulary#context) that you provide. This means both the messages and the model evaluation in your generative AI application are specific to each end user, at runtime. You can also use the AI SDKs to record metrics from your AI model generation, including duration and tokens.

Follow these instructions to start using the .NET AI SDK in your application.

### Install the SDK

First, install the AI SDK as a dependency in your application using your application's dependency manager. If you want to depend on a specific version, refer to the [SDK releases page](https://github.com/launchdarkly/dotnet-core/releases?q=ai) to identify the latest version.

The .NET AI SDK is built on the [.NET (server-side) SDK](/sdk/server-side/dotnet), so you'll need to install that as well. The .NET AI SDK requires version 8 or higher of the .NET (server-side) SDK.

Here's how:

<CodeGroup>
  ```bash title="Shell" theme={null}
      Install-Package LaunchDarkly.ServerSdk
      Install-Package LaunchDarkly.ServerSdk.Ai
  ```
</CodeGroup>

Next, import the namespaces in your application code. The namespace is not the same as the package name:

<CodeGroup>
  ```csharp title=".NET AI SDK" theme={null}
      using LaunchDarkly.Sdk.Server.Ai;
      using LaunchDarkly.Sdk.Server.Ai.Adapters;
      using LaunchDarkly.Sdk.Server.Ai.Config;
  ```
</CodeGroup>

### Initialize the client

After you install and import the SDK, create a single, shared instance of `LdClient`. When the `LdClient` is initialized, use it to initialize the `LdAiClient`. The `LdAiClient` is how you interact with AI Configs. Specify the SDK key to authorize your application to connect to a particular environment within LaunchDarkly.

<Note title="The .NET SDKs use an SDK key">
  The .NET AI and server-side SDKs use an SDK key. Keys are specific to each project and environment. They are available from **Project settings**, on the **Environments** list. To learn more about key types, read [Keys](/sdk/concepts/client-side-server-side#keys).
</Note>

Here's how:

<CodeGroup>
  ```csharp title=".NET AI SDK" theme={null}
      var baseClient = new LdClient(Configuration.Builder("sdk-key-123").StartWaitTime(TimeSpan.FromSeconds(5)).Build());
      var aiClient = new LdAiClient(new LdClientAdapter(baseClient));
  ```
</CodeGroup>

### Configure the context

Next, configure the context that will use the AI Config, that is, the context that will encounter generated AI content in your application. The context attributes determine which variation of the AI Config LaunchDarkly serves to the end user, based on the targeting rules in your AI Config. If you are using template variables in the messages in your AI Config's variations, the context attributes also fill in values for the template variables.

Here's how:

<CodeGroup>
  ```csharp title=".NET AI SDK" theme={null}
      LDContext context = Context.Builder("example-context-key")
          .Set("firstName", "Sandy")
          .Set("lastName", "Smith")
          .Set("email", "sandy@example.com")
          .Set("groups", LdValue.ArrayOf(LdValue.Of("Acme"), LdValue.Of("Global Health Services")))
          .Build();
  ```
</CodeGroup>

### Customize an AI Config

Then, use `Config` to customize the AI Config. Customization means that any variables you include in the messages when you define the AI Config variation have their values set to the context attributes and variables you pass to `Config`. You need to call `Config` each time you generate content from your AI model.

The customization process within the AI SDK is similar to [evaluating flags](/sdk/features/) in one of LaunchDarkly's client-side, server-side, or edge SDKs, in that the SDK completes the customization without a separate network call. The `Config` function takes an AI Config key, a context, and a fallback value. It performs the evaluation, then returns the customized messages and model along with a tracker for recording metrics. If it cannot perform the evaluation or LaunchDarkly is unreachable, it returns the fallback value. For example, you might use an empty, disabled `LdAiConfig` as a fallback value, or a fully configured default. Either way, you should make sure to check for this case and handle it appropriately in your application.

After you call `Config`, you can pass the customized messages directly to your AI.

Here's how:

<CodeGroup>
  ```csharp title=".NET AI SDK" theme={null}
      var fallbackConfig = LdAiConfig.New()
        .SetEnabled(false)
        .Build()

      var tracker = aiClient.Config(
        "example-ai-config-key",
        context,
        fallbackConfig,
        new Dictionary<string, object> {
          { "exampleCustomVariable", "exampleCustomValue" }
        }
      );
  ```
</CodeGroup>

To learn more, read [Customizing AI Configs](/sdk/features/ai-config#net-ai).

### Call provider, record metrics from AI model generation

Finally, use the `TrackRequest` function to make a request to your generative AI provider and record metrics from your AI model generation. Make sure to check whether the returned `LdAiConfig` is enabled, and handle the disabled case appropriately in your application.

Here's how:

<CodeGroup>
  ```csharp title=".NET AI SDK, any model" theme={null}
      if (tracker.Config.Enabled == true) {

        var response = tracker.TrackRequest(Task.Run(() =>
          {
            // Make request to a provider, which automatically tracks metrics in LaunchDarkly.
            // When sending the request to a provider, use details from tracker.Config.
            // For instance, you can pass tracker.Config.Model and tracker.Config.Messages.
            // Optionally, return response metadata, for example to do your own additional logging.
            //
            // CAUTION: If the call inside of Task.Run() throws an exception,
            // the SDK will re-throw that exception

            return new Response
            {
              Usage = new Usage { Total = 1, Input = 1, Output = 1 }, /* Token usage data */
              Metrics = new Metrics { LatencyMs = 100 } /* Metrics data */
            };
          }
        ));

      } else {

        // Application path to take when the tracker.Config is disabled

      }
  ```
</CodeGroup>

If you would like to do any additional tracking, besides what LaunchDarkly provides, it is your responsibility to fill in the `Response` object with the data you want to track.

Make sure to call `Config` each time you generate content from your AI model:

<CodeGroup>
  ```csharp title=".NET AI SDK, next request to provider" theme={null}
      var tracker = aiClient.Config(
        "example-ai-config-key",
        context,
        fallbackConfig,
        new Dictionary<string, object> {
          { "exampleCustomVariable", "exampleCustomValue" }
        }
      );

      var response = tracker.TrackRequest(...)
  ```
</CodeGroup>

To learn more, read [Tracking AI metrics](/sdk/features/ai-metrics#net-ai).

## Supported features

This SDK supports the following features:

* [Anonymous contexts](/sdk/features/anonymous#net-ai)
* [Context configuration](/sdk/features/context-config/ai#net-ai)
* [Customizing AI Configs](/sdk/features/ai-config#net-ai)
* [Private attributes](/sdk/features/private-attributes/ai#net-ai)
* [Tracking AI metrics](/sdk/features/ai-metrics#net-ai)
