> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://launchdarkly.com/docs/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://launchdarkly.com/docs/_mcp/server.

# Go AI SDK reference

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

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

The Go AI SDK is designed for use with AgentControl. 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/go-server-sdk/tree/v7/ldai).

#### SDK quick links

LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:

<table class="fern-table">
  <tr>
    <th>
      Resource
    </th>

    <th>
      Location
    </th>
  </tr>

  <tr>
    <td>
      SDK API documentation
    </td>

    <td>
      [SDK API docs](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/ldai)
    </td>
  </tr>

  <tr>
    <td>
      GitHub repository
    </td>

    <td>
      [ldai](https://github.com/launchdarkly/go-server-sdk/tree/v7/ldai/)
    </td>
  </tr>

  <tr>
    <td>
      Published module
    </td>

    <td>
      [pkg.go.dev](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/ldai)
    </td>
  </tr>
</table>

## Get started

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

#### Try the Quickstart

This reference guide describes working specifically with the Go AI SDK. For a complete introduction to LaunchDarkly AI SDKs and how they interact with configs, read [Quickstart for AgentControl](/home/agentcontrol/quickstart).

You can use the Go AI SDK to customize your 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 Go AI SDK in your application.

### Install the SDK

First, install the AI SDK as a dependency in your application. How you do this depends on what dependency management system you are using:

* If you are using the standard [Go modules](https://github.com/golang/go/wiki/Modules) system, import the SDK packages in your code and `go build` will automatically download them. The SDK and its dependencies are modules.
* Otherwise, use the `go get` command and specify the SDK version, such as `go get github.com/launchdarkly/go-server-sdk/ldai`.

The Go AI SDK is built on the [Go SDK](/sdk/server-side/go), so you'll need to install that as well.

Here's how:

#### Go AI SDK

```go
import (
  ld "github.com/launchdarkly/go-server-sdk/v7"
  "github.com/launchdarkly/go-server-sdk/ldai"
)
```

### Initialize the client

After you install and import the SDK, create a single, shared instance of `LDClient`. Then, use it to initialize the `LDAIClient`. The `LDAIClient` is how you interact with configs. Specify the SDK key to authorize your application to connect to a particular environment within LaunchDarkly.

#### The Go SDK uses an SDK key

The Go SDK uses an SDK key. Keys are specific to each project and environment. They are available on the **SDK keys** page under **Settings**. To learn more about key types, read [Keys](/sdk/concepts/client-side-server-side#keys-and-credentials).

Here's how:

#### Go AI SDK

```go
client, _ = ld.MakeClient("YOUR_SDK_KEY", 5*time.Second)

aiClient, err := ldai.NewClient(client)

if err != nil {
  // Client couldn't be created
}
```

This example assumes you've imported the LaunchDarkly SDK package as `ld`, as shown above.

#### Best practices for error handling

The second return type in these code samples ( `_` ) represents an error in case the LaunchDarkly client does not initialize. Consider naming the return value and using it with proper error handling.

### Configure the context

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

Here's how:

#### Go AI SDK

```go
context := ldcontext.NewBuilder("example-context-key").
    Kind("user").
    Name("Sandy Smith").
    SetString("email", "sandy@example.com").
    SetValue("groups", ldvalue.ArrayOf(
      ldvalue.String("Acme"), ldvalue.String("Global Health Services"))).
    Build()
```

### Customize a config

Then, use `Config()` to customize the config. Customization means that any variables you include in the messages when you define the config variation have their values set to the context attributes and variables you pass to the `Config()` method.

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 a config key, a context, and a fallback value. It performs the evaluation, then returns a `Config` object with the customized messages and model, and a `Tracker` object to capture performance metrics. If it cannot perform the evaluation or LaunchDarkly is unreachable, it returns the fallback value. For example, you might use an empty, disabled `Config` 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:

#### Go AI SDK

```go
fallbackValue := NewConfig().Build() // by default, the Config is disabled

cfg, tracker := aiClient.Config("example-config-key", context, fallbackValue, map[string]interface{}{"exampleCustomVariable": "exampleCustomValue"})
```

To learn more, read [Customizing AgentControl configs](/sdk/features/agentcontrol-config#go-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 `cfg` is enabled, and handle the disabled case appropriately in your application.

Here's how:

#### Go AI SDK, any model

```go maxLines=0
if cfg.Enabled() {

  response, err := tracker.TrackRequest(func(config *Config) (ProviderResponse, error) {

    // Make request to a provider, which automatically tracks metrics in LaunchDarkly.
    // When sending the request to a provider, use details from config.
    // For instance, you can pass a model parameter (config.ModelParam) or messages (config.Messages).
    // Optionally, return response metadata, for example to do your own additional logging.

    return ProviderResponse{
      Usage: TokenUsage{
        Total: 1, // Token usage data
      },
      Metrics: Metrics{
        Latency: 10 * time.Millisecond // Metrics data
      },
    }, nil
  })

} else {

  // Application path to take when the cfg.config is disabled

}

```

Alternatively, you can use the SDK's other `Track*` functions to record these metrics manually. The `TrackMetric` function is expecting a response, so you may need to do this if your application requires streaming.

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

## Supported features

This SDK supports the following features:

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