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

# Go AI SDK reference

**Developer**

**Federal docs**

**EU docs**

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 retrieves a config variation, calls your model provider through a handler, and records metrics, traces, and judge evaluations around each call.

> [!WARNING]
>
> **This SDK replaces the previous Go AI SDK**
>
> Version 1.0.0 of the Go AI SDK is a new module, `github.com/launchdarkly/go-ai-sdk`, with a different API from the previous Go AI SDK, `github.com/launchdarkly/go-server-sdk-ai/ldai`. The two modules are not interchangeable. If your application uses the previous SDK, its reference is available at [Go AI SDK reference (legacy)](https://launchdarkly.com/docs/sdk/ai/go-legacy.md). To plan your move to this SDK, read [Migrate from the legacy AI SDKs](https://launchdarkly.com/docs/sdk/ai/migration.md).

> [!NOTE]
>
> **SDK quick links**
>
> The SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications.
>
> <table>
>   <thead>
>     <tr>
>       <th>Resource</th>
>       <th>Location</th>
>     </tr>
>   </thead>
>
>   <tbody>
>     <tr>
>       <td>SDK API documentation</td>
>       <td>[SDK API docs](https://pkg.go.dev/github.com/launchdarkly/go-ai-sdk)</td>
>     </tr>
>
>     <tr>
>       <td>GitHub repository</td>
>       <td>[go-ai-sdk](https://github.com/launchdarkly/go-ai-sdk)</td>
>     </tr>
>
>     <tr>
>       <td>Published module</td>
>       <td>[pkg.go.dev](https://pkg.go.dev/github.com/launchdarkly/go-ai-sdk)</td>
>     </tr>
>
>     <tr>
>       <td>Sample applications</td>
>       <td>[examples](https://github.com/launchdarkly/go-ai-sdk/tree/main/examples)</td>
>     </tr>
>   </tbody>
> </table>

## Get started

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

> [!NOTE]
>
> **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](https://launchdarkly.com/docs/home/agentcontrol/quickstart).

You can use the Go AI SDK to customize your config based on the [context](https://launchdarkly.com/docs/home/getting-started/vocabulary.md#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 SDK to call your model provider, record metrics from the generation, and evaluate model output with judges.

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

### Prerequisites

To use the Go AI SDK, you need:

* Go 1.26 or later.
* A LaunchDarkly account where you have completed the [Quickstart for AgentControl](https://launchdarkly.com/docs/home/agentcontrol/quickstart).
* An existing AgentControl config.

### Install the SDK

First, install the AI SDK as a dependency in your application. The SDK requires Go 1.26 or later. How you install it depends on what dependency management system you are using.

* If you are using the standard [Go modules](https://go.dev/wiki/Modules) system, import the SDK packages in your code and `go build` automatically downloads them.
* Otherwise, use the `go get` command and specify the SDK version, such as `go get github.com/launchdarkly/go-ai-sdk/aiserver@v1.0.0`.

All of the packages are in one Go module and share one version. The `aiserver` package is the core client. Add one handler package for each model provider you use. Installing `aiserver` also installs the LaunchDarkly Go SDK and the OpenTelemetry modules that it depends on.

Here is how:

```bash title="Shell" lines wrap theme={null}
go get github.com/launchdarkly/go-ai-sdk/aiserver
go get github.com/launchdarkly/go-ai-sdk/claudemessages
```

Then, import the packages you use. Here is how:

```go title="Go AI SDK" lines wrap theme={null}
import (
    "github.com/launchdarkly/go-ai-sdk/aiserver"
    "github.com/launchdarkly/go-ai-sdk/claudemessages"
    "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
)
```

### Choose a provider handler

A handler calls a model provider on behalf of the SDK. The provider name and mode on the config variation decide which handler runs. A config in completion mode uses a messages handler, and a config in agent mode uses an agent handler.

<table>
  <thead>
    <tr>
      <th>Package</th>
      <th>Provider</th>
      <th>Mode</th>
      <th>Description</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`claudemessages`</td>
      <td>Anthropic</td>
      <td>messages</td>
      <td>Anthropic Messages API. Runs function tools in a local tool loop.</td>
    </tr>

    <tr>
      <td>`claudeagents`</td>
      <td>Anthropic</td>
      <td>agent</td>
      <td>In-process agent loop on the Anthropic Messages API, with the Claude Code built-in tools.</td>
    </tr>

    <tr>
      <td>`openaimessages`</td>
      <td>OpenAI</td>
      <td>messages</td>
      <td>OpenAI Responses API.</td>
    </tr>

    <tr>
      <td>`openaiagents`</td>
      <td>OpenAI</td>
      <td>agent</td>
      <td>In-process agent loop on the OpenAI Responses API, with handoffs.</td>
    </tr>

    <tr>
      <td>`bedrockmessages`</td>
      <td>Amazon Bedrock</td>
      <td>messages</td>
      <td>Amazon Bedrock Converse API.</td>
    </tr>

    <tr>
      <td>`bedrockagents`</td>
      <td>Amazon Bedrock</td>
      <td>agent</td>
      <td>Amazon Bedrock agent handler.</td>
    </tr>

    <tr>
      <td>`langchainmessages`</td>
      <td>Any provider (`*`)</td>
      <td>messages</td>
      <td>LangChain-style handler. The fallback when no other handler names the provider.</td>
    </tr>

    <tr>
      <td>`langchainagents`</td>
      <td>Any provider (`*`)</td>
      <td>agent</td>
      <td>LangChain-style agent handler. The fallback when no other handler names the provider.</td>
    </tr>
  </tbody>
</table>

The SDK also includes the following packages.

<table>
  <thead>
    <tr>
      <th>Package</th>
      <th>Description</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`aiserver`</td>
      <td>The core client. Includes `Config`, `Graph`, `ResolveGraph`, `InspectConfig`, the registry, lifecycle functions, judges, and telemetry.</td>
    </tr>

    <tr>
      <td>`skills`</td>
      <td>Agent skills. Reads skills that configs reference, verifies them, and writes them to disk.</td>
    </tr>

    <tr>
      <td>`evaluations`</td>
      <td>Evaluations from code. Runs a dataset through a handler, scores each output, and reports the verdict.</td>
    </tr>

    <tr>
      <td>`llms`</td>
      <td>The chat-model interface, plus built-in OpenAI, Anthropic, and Bedrock models for the LangChain handlers.</td>
    </tr>
  </tbody>
</table>

Each handler reads the usual environment variables for its provider, such as `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, or the AWS credential chain and `AWS_BEARER_TOKEN_BEDROCK`. You can also inject your own provider client through the handler options.

> [!NOTE]
>
> **The agent handlers run their own agent loop**
>
> Go has no official OpenAI Agents SDK or Claude Agent SDK. `openaiagents` and `claudeagents` run their own agent loop on the official provider SDKs. LangChain publishes no official Go SDK. `langchainmessages` and `langchainagents` use the `llms` package and state-graph runner that ship with this SDK.

> [!WARNING]
>
> **The claudeagents built-in tools run on your host**
>
> The `claudeagents` built-in tools, such as `ClaudeBash`, `ClaudeRead`, and `ClaudeEdit`, run in your process with the permissions of the process user. File tools are confined to `Options.WorkDir`, but `ClaudeBash` is not sandboxed and inherits the full process environment, including secrets such as API keys. Enable these tools only for configs and inputs you trust. Run the application in a container or virtual machine with a dedicated working directory and least-privilege credentials.

### Initialize the client

After you install and import the SDK, initialize the AI client. The AI client is how you interact with configs. Specify the SDK key to authorize your application to connect to a particular environment within LaunchDarkly.

> [!NOTE]
>
> **The Go AI SDK uses an SDK key**
>
> The Go AI 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](https://launchdarkly.com/docs/sdk/concepts/client-side-server-side.md#keys-and-credentials).

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
ctx := context.Background()

_, err := aiserver.InitClient(ctx, aiserver.InitOptions{
    SDKKey: "YOUR_SDK_KEY", // Or leave empty and set LD_SDK_KEY.
})
if err != nil {
    log.Fatal(err) // The client could not be created.
}
defer aiserver.Shutdown(ctx)
```

You can initialize the client in three ways.

* Lazy initialization. The first call to `Config`, `Graph`, `ResolveGraph`, or `InspectConfig` calls `InitClient` with empty options, which means every setting comes from environment variables.
* Explicit initialization. Call `InitClient(ctx, opts)` to pass options or to fail fast at startup.
* Bring your own client. Call `InitWithClient(ctx, ldClient, opts)` with any value that satisfies `aiserver.LDClient`. Any `*ld.LDClient` satisfies the requirement. To build one, import `github.com/launchdarkly/go-server-sdk/v7` as `ld` and call `ld.MakeClient`.

If a client is already installed, `InitClient` and `InitWithClient` return the installed client. Call `Shutdown` first to switch clients. `Shutdown` exports pending spans, flushes events, and closes the LaunchDarkly client, including a client you passed to `InitWithClient`. `InitOptions.WaitFor` sets the initialization timeout. A zero or negative value means the 10 second default.

A failed initialization is not cached. The next call tries again. If you rely on lazy initialization while LaunchDarkly is unreachable, each call can wait up to the timeout before it fails. To keep an outage from adding that wait to every request, call `InitClient` at startup and handle its error there.

> [!NOTE]
>
> **Initializing the client also sets up OpenTelemetry tracing**
>
> `InitClient` and `InitWithClient` register a global OpenTelemetry tracer provider. To learn what the SDK exports, read [Record metrics and traces](#record-metrics-and-traces).

### 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 is how:

```go title="Go AI SDK" lines wrap theme={null}
userContext := 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()
```

This example uses the `ldcontext` and `ldvalue` packages from `github.com/launchdarkly/go-sdk-common/v3`.

#### Protect private attributes

Anonymous contexts and private attributes work the same way as in the Go server SDK. The AI SDK sends its events through the Go server SDK, which applies its usual handling of anonymous contexts and private attributes. Spans carry only context kinds and keys, never attribute values. The SDK records them as `feature_flag.context.id`, `feature_flag.contextKeys`, and `context.contextKeys.<kind>`.

Prompt templates are the exception. The full context, including private attributes, is available to templates as `ldContext`.

> [!WARNING]
>
> **Templates can expose private attributes**
>
> If a template in your config's messages references a private attribute, the SDK puts the attribute's value into the prompt. With content capture on, which is the default, the value also appears in span text. To turn content capture off, read [Control what the SDK records on spans](#control-what-the-sdk-records-on-spans).

### Customize a config and call your model

Then, use `Config` to customize a config and call your model. 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 in. You set the mode for a config when you [create it](https://launchdarkly.com/docs/home/agentcontrol/create.md) in the LaunchDarkly UI.

The customization process within the AI SDK is similar to [evaluating flags](https://launchdarkly.com/docs/sdk/features/evaluating.md) in one of the LaunchDarkly client-side, server-side, or edge SDKs, in that the SDK completes the customization without a separate network call. After it customizes the config, the SDK routes the call to the handler that matches the variation's provider and mode, and returns the response.

`Config` takes a config key and the handlers that can serve it. `Invoke` takes the input, the context, and optional call options. Use `WithVariables` to fill template variables in the config's messages, and `WithHistory` to pass earlier messages in the conversation.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
claude, err := claudemessages.NewHandler(claudemessages.Options{}) // Reads ANTHROPIC_API_KEY.
if err != nil {
    log.Fatal(err)
}

resp, err := aiserver.Config(aiserver.ConfigArgs{
    Key:      "example-config-key",
    Handlers: []*aiserver.ProviderHandler{claude},
}).Invoke(ctx, "What is a feature flag?", userContext,
    aiserver.WithVariables(map[string]any{"tone": "brief"}),
)
if err != nil {
    if errors.Is(err, aiserver.ErrConfigDisabled) {
        // The variation is turned off. Serve your own fallback response.
        return fallbackAnswer()
    }
    if resp.Response == nil {
        log.Fatal(err) // The call failed before the model answered.
    }
    log.Printf("judge failed: %v", err) // The answer is still usable.
}

fmt.Println(resp.Text(), resp.Usage.Total)
```

`resp.Text()` returns the response as text, whether the model returned a string or structured output.

#### Use a convenience function

If you use one provider in one mode, a convenience function builds the handler and makes the call in one step. Each handler package has one, such as `openaimessages.OpenAIMessages`, `claudemessages.ClaudeMessages`, and `openaiagents.OpenAIAgents`. Pass the config key, the user input, the context, and optional `InvokeOptions`. The options hold `Variables` for template variables, `ToolHandlers`, `Registry`, `SkipJudges`, and `Handler`, which sets the handler options.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
resp, err := openaimessages.OpenAIMessages(ctx, "example-config-key", "What is a feature flag?", userContext,
    &openaimessages.InvokeOptions{
        Variables: map[string]any{"tone": "brief"},
    },
)
if err != nil {
    if resp.Response == nil {
        log.Fatal(err) // The call failed before the model answered.
    }
    log.Printf("judge failed: %v", err) // The answer is still usable.
}

fmt.Println(resp.Text())
```

If a config can serve more than one provider, use `Config` and `Invoke` with several handlers instead.

#### Handle a disabled or unavailable config

A call can fail before it reaches a model. The SDK has no fallback value. Instead, `Invoke` and `Stream` return an error that you match with `errors.Is`, and your application decides what to do next.

* `ErrConfigDisabled` means the config variation is turned off.
* `ErrConfigNotFound` means the flag returned no config. The flag is missing, has the wrong type, or could not be evaluated, for example because the client is not ready. The error wraps the reason from the LaunchDarkly Go SDK, so `errors.Is` can match that cause too.

To see every sentinel error, read [Errors](#errors).

#### Stream a response

Use `Stream` to receive the response as the model generates it. `Stream` returns an iterator. Each event has a type, and chunk events carry text.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
cfg := aiserver.Config(aiserver.ConfigArgs{
    Key:      "example-config-key",
    Handlers: []*aiserver.ProviderHandler{claude},
})

for ev, err := range cfg.Stream(ctx, "Tell me about feature flags", userContext) {
    if err != nil {
        log.Fatal(err)
    }
    if ev.Type == aiserver.EventChunk {
        fmt.Print(ev.Text)
    }
}
```

If you break out of the loop, the SDK abandons the stream. The SDK marks the spans with `launchdarkly.stream.abandoned` and tracks neither a success nor an error. If a handler panics, the SDK tracks a generation error and then the panic continues to your code. A panic in your own loop body counts as abandonment.

#### Use structured output

A config can return structured output that conforms to a JSON schema, rather than plain text. Use structured output when another system, database, or function consumes the model's response, or when you need to extract specific information, such as a severity or category.

Set the output format on the config variation in the LaunchDarkly UI. The variation's `OutputFormat` holds the schema. When a variation has an output format, `resp.Text()` returns the structured output as a JSON string, which you can unmarshal into your own type.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
var ticket struct {
    Severity string `json:"severity"`
    Category string `json:"category"`
}
if err := json.Unmarshal([]byte(resp.Text()), &ticket); err != nil {
    log.Fatal(err)
}
```

#### Use more than one provider

Pass several handlers to serve variations that use different providers. The variation's provider name and mode pick the handler. A handler with the provider `*`, such as the LangChain handlers, is the fallback when no other handler names the provider.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
openai, err := openaimessages.NewHandler(openaimessages.Options{})
if err != nil {
    log.Fatal(err)
}
claude, err := claudemessages.NewHandler(claudemessages.Options{})
if err != nil {
    log.Fatal(err)
}

cfg := aiserver.Config(aiserver.ConfigArgs{
    Key:      "example-config-key",
    Handlers: []*aiserver.ProviderHandler{openai, claude},
})
```

If no handler serves the variation's provider and mode, the call returns `ErrNoHandler`.

#### Write a custom handler

If you use a provider that has no handler package, or you want to call an internal endpoint, use `aiserver.CreateHandler` to build your own handler. A custom handler works with `Config` routing and registries in the same way as the built-in handlers.

`CreateHandler` takes four arguments:

* A `ProvidesFor` value, which holds the provider name and mode that the handler serves.
* An invoke function, which receives a `HandlerRequest` and returns a `HandlerResult`.
* An optional stream function. If you pass nil, `Stream` falls back to the invoke function.
* A `ContentCapture` setting for the handler's spans.

The `HandlerRequest` holds the customized config, the user input, the tool handlers, the variables, and the conversation history. Return the model's answer in the result's `Output`, and its token counts in `Usage`, with keys such as `input_tokens` and `output_tokens`.

Here is how:

```go title="Go AI SDK" expandable lines wrap theme={null}
internal := aiserver.CreateHandler(
    aiserver.ProvidesFor{Provider: "internal", Mode: aiserver.ModeMessages},
    func(ctx context.Context, req aiserver.HandlerRequest) (aiserver.HandlerResult, error) {
        body, err := json.Marshal(map[string]any{"model": req.Config.Model.Name, "prompt": req.UserInput})
        if err != nil {
            return aiserver.HandlerResult{}, err
        }
        httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost,
            "https://models.internal.example.com/generate", bytes.NewReader(body))
        if err != nil {
            return aiserver.HandlerResult{}, err
        }
        httpReq.Header.Set("Content-Type", "application/json")

        res, err := http.DefaultClient.Do(httpReq)
        if err != nil {
            return aiserver.HandlerResult{}, err
        }
        defer res.Body.Close()

        var data struct {
            Text  string         `json:"text"`
            Usage map[string]any `json:"usage"`
        }
        if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
            return aiserver.HandlerResult{}, err
        }
        return aiserver.HandlerResult{Output: data.Text, Usage: data.Usage}, nil
    },
    nil,                     // No stream function. Stream falls back to the invoke function.
    aiserver.CaptureDefault, // Follow the process-wide content capture setting.
)

resp, err := aiserver.Config(aiserver.ConfigArgs{
    Key:      "example-config-key",
    Handlers: []*aiserver.ProviderHandler{internal},
}).Invoke(ctx, "What is feature flagging?", userContext)
```

A custom handler with the provider `*` serves any provider that no other handler names, which makes it a fallback.

#### Share handlers and tools with a registry

If your application calls several configs, build the handlers and tools once, then share them with a registry. Tool keys must match the keys in the variation's tools map exactly. The SDK never offers a tool with no registered function to the model.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
getWeather := aiserver.ToolFunc(func(ctx context.Context, args map[string]any) (any, error) {
    return "Sunny, 22 degrees Celsius", nil
})

registry := aiserver.NewRegistry(aiserver.RegistryEntries{
    Handlers: []*aiserver.ProviderHandler{openai, claude},
    Tools:    map[string]aiserver.ToolHandler{"get-weather": getWeather},
})

resp, err := aiserver.Config(aiserver.ConfigArgs{
    Key:      "example-config-key",
    Registry: registry,
}).Invoke(ctx, "What is the weather in Lisbon?", userContext)
```

Handlers and tools that you pass directly on a call take precedence over the registry's.

Unlike the JavaScript and Python AI SDKs, the Go AI SDK has no global registry. Build a `*Registry` and pass it explicitly. If you want one registry for the whole process, keep it in a variable in your own package.

#### Use built-in provider tools

Some handlers include built-in tools that run natively, rather than a function you write. Built-in tools are `*aiserver.NativeTool` values. To use one, assign it as the implementation for a LaunchDarkly tool key in place of a function. The SDK tracks a built-in tool call like any other tool call.

The `claudeagents` package exports these built-in tools:

* `ClaudeWebSearch` and `ClaudeWebFetch`, which map to Anthropic server tools. Anthropic runs them.
* `ClaudeBash`, `ClaudeRead`, `ClaudeEdit`, `ClaudeWrite`, `ClaudeGlob`, `ClaudeGrep`, `ClaudeTodoWrite`, and `ClaudeNotebookEdit`, which run on your host. To learn about the risks, read [Choose a provider handler](#choose-a-provider-handler).

Here is how to enable Claude web search:

```go title="Go AI SDK" lines wrap theme={null}
claude, err := claudeagents.NewHandler(claudeagents.Options{})
if err != nil {
    log.Fatal(err)
}

resp, err := aiserver.Config(aiserver.ConfigArgs{
    Key:      "example-config-key",
    Handlers: []*aiserver.ProviderHandler{claude},
    ToolHandlers: map[string]aiserver.ToolHandler{
        "web-search":  claudeagents.ClaudeWebSearch, // Built-in tool, no function needed.
        "get-weather": getWeather,
    },
}).Invoke(ctx, "What are the latest LaunchDarkly release notes?", userContext)
```

#### Read a config without calling a model

Use `InspectConfig` to read the variation that a context receives, without calling a model. This is useful if you want to call your provider yourself.

`InspectConfig` returns an `InspectResult` and an error. A turned-off variation returns `Enabled` as false and a nil error. Initialization, evaluation, and parse failures return an error and a zero result.

The `InspectResult` has the following fields:

* `Enabled`: Whether the variation is turned on.
* `Config`: The config variation, as an `*AiConfig`. This is nil when the variation is turned off.
* `Meta`: Metadata about the variation, as a `*VariationMeta`. It has `VariationKey`, `Version`, `Mode`, `ModelKey`, and `ModelVersion`.

The `Config` holds everything you need to call a provider yourself. It has `Instructions`, `Messages`, `Model`, `Provider`, `Tools`, `JudgeConfiguration`, `EvaluationMetricKey`, and `OutputFormat`. Each message has a `Role` and `Text`. The model has a `Name`, `Parameters`, and `Custom` values, and the provider has a `Name`.

Consider the following before you call a provider directly:

* The SDK returns messages as templates, without filling in their variables. Use `aiserver.ParseTemplate` to fill them.
* LaunchDarkly records no metrics for a provider call that you make yourself.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
res, err := aiserver.InspectConfig(ctx, "example-config-key", userContext)
if err != nil {
    log.Fatal(err) // Initialization, evaluation, or parse failure.
}
if !res.Enabled {
    return // The variation is turned off.
}

cfg := res.Config
fmt.Println(cfg.Provider.Name, cfg.Model.Name, cfg.Model.Parameters["temperature"])

for _, m := range cfg.Messages {
    fmt.Println(m.Role, aiserver.ParseTemplate(m.Text, map[string]any{"tone": "brief"}))
}
```

## Run agent graphs

An agent graph links multiple agent configs together into nodes and edges. Use `Graph` to run a graph. The SDK routes each node to the handler that matches the node's provider and mode. Pass agent handlers.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
claude, err := claudeagents.NewHandler(claudeagents.Options{})
if err != nil {
    log.Fatal(err)
}
openai, err := openaiagents.NewHandler(openaiagents.Options{})
if err != nil {
    log.Fatal(err)
}
agents := []*aiserver.ProviderHandler{claude, openai}

graph := aiserver.Graph("example-graph-key", aiserver.GraphOptions{Handlers: agents})

out, err := graph.Invoke(ctx, "Plan a week in Lisbon", userContext)
```

`Invoke` and `Stream` on a graph take optional `CallOption` values. Use `WithVariables` to pass template variables.

If the graph, or a node that it references, is turned off, the call returns `ErrGraphDisabled`. Other failures, such as an initialization or evaluation error, return a different error.

`Stream` on a graph yields node start, chunk, node done, handoff, and done events. A node's own judge scores are on its node done event, and the done event holds only the graph-level judge scores. To collect every score, read each node done event.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
for ev, err := range graph.Stream(ctx, "Plan a week in Lisbon", userContext) {
    if err != nil {
        log.Fatal(err)
    }
    switch ev.Type {
    case aiserver.EventChunk:
        fmt.Print(ev.Text)
    case aiserver.EventNodeDone:
        // Read the node's own judge scores here.
    case aiserver.EventDone:
        // The graph run is complete.
    }
}
```

Each agent handler package has a graph convenience wrapper with the handler already bound, such as `openaiagents.OpenAIGraph`, `claudeagents.ClaudeGraph`, `bedrockagents.BedrockGraph`, and `langchainagents.LangChainGraph`.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
tools := map[string]aiserver.ToolHandler{"get-weather": getWeather}

graph, err := openaiagents.OpenAIGraph("example-graph-key", aiserver.GraphOptions{ToolHandlers: tools}, openaiagents.Options{})
if err != nil {
    log.Fatal(err)
}

out, err := graph.Invoke(ctx, "Plan a week in Lisbon", userContext)
if err != nil {
    log.Fatal(err)
}

fmt.Println(out.Response, out.Usage.Total)
```

If you want to run the graph with a framework-native runner, use `ResolveGraph` to read the topology and node configs. If the graph is turned off, `ResolveGraph` returns a disabled definition and a nil error. Check `def.Enabled` before you run it. The agent handler packages include adapters that take the result, such as `ToClaudeAgents`, `ToOpenAIAgents`, and `ToLangGraph`. This example reuses the `tools` map from the previous example.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
def, err := aiserver.ResolveGraph(ctx, "example-graph-key", userContext, aiserver.GraphOptions{})
if err != nil {
    log.Fatal(err)
}
if !def.Enabled {
    return // The graph is turned off.
}

runner, err := openaiagents.ToOpenAIAgents(def, &openaiagents.NativeGraphOptions{
    ToolHandlers: tools,
    Context:      userContext, // Omit to track no events.
})
if err != nil {
    log.Fatal(err)
}

out, err := runner.Invoke(ctx, "Plan a week in Lisbon")
```

## Evaluate output with judges

Judges are AgentControl configs that score the output of another config. You attach judges to a variation in the LaunchDarkly UI, and each attachment has a sampling percentage. To learn more, read [Judges](https://launchdarkly.com/docs/home/agentcontrol/judges.md). The SDK runs the attached judges after the call returns, and includes the results in the response. In an agent graph, each node can have its own judges, and a graph-level judge scores the final answer.

A judge needs a handler for its own provider. If no handler covers the judge's provider, the SDK skips that judge. If the judge's own config is missing or turned off, the SDK skips that judge and logs the skip. If a judge's provider call fails, `Invoke` returns both the response and the error. The answer is still usable. Check the response before you treat the error as fatal.

### Run judges later

By default, `Invoke` returns the judge results with the response. To keep response time low, set `SkipJudges` on `ConfigArgs`, or on the `InvokeOptions` of a convenience function. The response then carries `JudgeTasks` instead of results. Each task is JSON-serializable, which means you can queue it. Pass each task and your handlers to `RunJudge` when you are ready to run it, for example on your own goroutine.

`RunJudge` returns a `*JudgeRunResult` and an error. The result has `Score`, `Response`, `Usage`, and `TrackData`. `RunJudge` returns an error only when the judge's provider call fails. It returns a nil result and a nil error when no handler serves the judge's provider, or when the judge returns no numeric score.

`RunJudge` does not send the evaluation metric. To record the score, call `aiserver.Track` with the task's evaluation metric key, the task's context, the result's `TrackData`, and the score.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
func runJudgeWorker(ctx context.Context, task aiserver.JudgeTask, handlers []*aiserver.ProviderHandler) {
    res, err := aiserver.RunJudge(ctx, task, handlers)
    if err != nil {
        log.Printf("judge %s failed: %v", task.ConfigKey, err)
        return
    }
    if res == nil {
        return // No handler serves the judge's provider, or the judge returned no numeric score.
    }
    if task.EvaluationMetricKey != "" {
        aiserver.Track(task.EvaluationMetricKey, task.UserContext, res.TrackData, res.Score)
    }
}
```

Start the worker for each task with `go runJudgeWorker(ctx, task, handlers)`. Pass handlers that cover each judge's provider. If you use a registry, pass `registry.Handlers()`.

## Record metrics and traces

The SDK records metrics around each call without extra code. It sends LaunchDarkly `$ld:ai:*` events for token usage, latency, and errors, and it creates OpenTelemetry spans for handler calls, tool calls, and judge evaluations.

Version 1.0 of the Go AI SDK does not record user feedback, such as a thumbs up or down on a response. It has no feedback event and no resumption tokens.

### Configure tracing

`InitClient` registers a global OpenTelemetry tracer provider. It exports spans over OpenTelemetry Protocol (OTLP)/HTTP with gzip compression. The SDK uses the endpoint from `InitOptions.OTLPEndpoint`, then `OTEL_EXPORTER_OTLP_ENDPOINT`, then the default endpoint.

The tracer provider sets these resource attributes.

<table>
  <thead>
    <tr>
      <th>Attribute</th>
      <th>Value</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`launchdarkly.project_id`</td>
      <td>Set from the SDK key, from `InitOptions.SDKKey` or `LD_SDK_KEY`.</td>
    </tr>

    <tr>
      <td>`service.name`</td>
      <td>`LD_SERVICE_NAME`, or `go-sdk` if unset.</td>
    </tr>

    <tr>
      <td>`deployment.environment`</td>
      <td>`LD_ENVIRONMENT`, when set.</td>
    </tr>
  </tbody>
</table>

> [!WARNING]
>
> **The tracer provider sends your SDK key as a resource attribute**
>
> The `launchdarkly.project_id` resource attribute carries your SDK key. If you point `OTEL_EXPORTER_OTLP_ENDPOINT` or `InitOptions.OTLPEndpoint` at your own collector, the SDK sends the key to that collector with every export.

To group the spans from one conversation, bind a conversation ID to the context. The SDK adds `gen_ai.conversation.id` to every span that starts under that context.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
ctx = aiserver.WithConversationID(ctx, "conversation-123")
```

### Control what the SDK records on spans

By default, spans include content. Handler spans carry the prompt and completion text, tool spans carry the tool arguments and results, and judge spans carry the judge's explanation. The SDK always records the model, provider, token usage, status, and tool names.

> [!WARNING]
>
> **Spans include prompt and completion text by default**
>
> If your prompts or completions can contain sensitive data, turn content capture off before the first handler call.

You can turn content capture off for one handler, or for the whole process. A handler setting takes precedence over the process setting, and the process setting takes precedence over the default, which is on.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
// For one handler.
claude, err := claudemessages.NewHandler(claudemessages.Options{
    CaptureContent: aiserver.CaptureOff,
})
if err != nil {
    log.Fatal(err)
}

// For the whole process. Call this before the first handler call.
if err := aiserver.Configure(aiserver.SDKOptions{CaptureContent: aiserver.CaptureOff}); err != nil {
    log.Fatal(err)
}
```

After the first handler call, `Configure` accepts only options that have the same effect, and otherwise returns `ErrConfigureLocked`. `Shutdown` resets this setting.

## Use Agent skills

Agent skills are skill files that AgentControl configs reference. The `skills` package reads those skills, verifies their content, and writes them to disk as `<root>/<key>/SKILL.md`. Your agent can then load them. Writing skills to disk works on POSIX systems only.

Install the package with `go get github.com/launchdarkly/go-ai-sdk/skills`.

The following example keeps a directory in sync with LaunchDarkly. When you revoke a skill, the SDK's watcher removes it from disk within 500 milliseconds. Add `.launchdarkly-skills.json` to your `.gitignore` file, because the SDK writes a manifest with that name.

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
store, err := skills.NewLaunchDarklySkillStore(os.Getenv("LD_SDK_KEY"), nil) // Server-side SDK key only.
if err != nil {
    log.Fatal(err)
}
if err := store.Start(); err != nil {
    log.Fatal(err)
}
defer store.Close()

if !store.WaitForSkills(10 * time.Second) {
    log.Printf("no skill payload yet: failed=%q", store.Failed())
}
skills.SetSkillStore(store)

report, watcher, err := skills.WatchAllSkills(ctx, "/etc/agent/skills", nil)
if err != nil {
    log.Fatal(err)
}
defer watcher.Close()

for _, a := range report.Errors() {
    log.Printf("skill %q: %s", a.Key(), a.ErrorMessage())
}
```

To learn more, read [Agent skills](https://launchdarkly.com/docs/sdk/features/agent-skills.md).

## Run evaluations from code

The `evaluations` package runs a dataset through your own handler, scores each output with AgentControl judges or local scorers, and reports the results to LaunchDarkly. Use it as a gate in continuous integration.

Install the package with `go get github.com/launchdarkly/go-ai-sdk/evaluations`, plus the handler package that runs your generation step.

Running evaluations from code needs more credentials than the other features. In LaunchDarkly, the API access token is called the Eval API key. To generate your credentials, click **Evaluations** in the side navigation, click **Run evaluations**, then choose **From code**. If you create the token yourself, assign a role that allows reading datasets and tools, and creating evaluations and evaluation runs, in the target project. The Writer role works.

<table>
  <thead>
    <tr>
      <th>Variable</th>
      <th>Required</th>
      <th>Description</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`LD_PROJECT_KEY`</td>
      <td>Yes, unless you set `InitEvaluationsOptions.ProjectKey`</td>
      <td>The project that holds the evaluations, runs, tools, datasets, and judges.</td>
    </tr>

    <tr>
      <td>`LD_API_TOKEN`</td>
      <td>Yes, unless you set `InitEvaluationsOptions.APIToken`</td>
      <td>A LaunchDarkly API access token. It authorizes every management API request.</td>
    </tr>

    <tr>
      <td>`LD_SDK_KEY`</td>
      <td>Yes, unless you set `InitEvaluationsOptions.SDKKey` or already initialized a client</td>
      <td>The server-side SDK key for the client that sends result events.</td>
    </tr>

    <tr>
      <td>`LD_API_BASE_URI`</td>
      <td>No</td>
      <td>The management API host. The default is `https://app.launchdarkly.com`. The SDK never uses `LD_BASE_URI` for this.</td>
    </tr>

    <tr>
      <td>`LD_UI_BASE_URI`</td>
      <td>No</td>
      <td>The host of the run link in the result. The default is `https://app.launchdarkly.com`.</td>
    </tr>
  </tbody>
</table>

Here is how:

```go title="Go AI SDK" lines wrap theme={null}
evals, err := evaluations.InitEvaluations(evaluations.InitEvaluationsOptions{})
if err != nil {
    log.Fatal(err)
}

judge, err := evaluations.NewJudge("accuracy", nil)
if err != nil {
    log.Fatal(err)
}

openai, err := openaimessages.NewHandler(openaimessages.Options{})
if err != nil {
    log.Fatal(err)
}

res, err := evals.Run(ctx, evaluations.RunOptions{
    Key:     "support-bot-" + time.Now().Format("20060102-150405"), // Unique for each run.
    Dataset: evaluations.DatasetKey("support-questions"),
    Handler: openai,
    Generation: &evaluations.GenerationConfig{
        Provider: "OpenAI",
        Model:    "gpt-4o",
        Mode:     evaluations.GenerationModeCompletion,
        Messages: []aiserver.Message{{Role: "system", Text: "You are a support agent."}},
    },
    Criteria: []evaluations.Criterion{judge},
})
if err != nil {
    log.Fatal(err)
}

fmt.Println(res.URL)
if !res.Passed {
    os.Exit(1)
}
```

The SDK never computes a verdict. It reports scores, and LaunchDarkly rules on them against the thresholds that you pin on the evaluation, and returns its verdict in `res.Verdict` as `EvalVerdictPass` or `EvalVerdictFail`. When LaunchDarkly returns a verdict, `Passed` follows it. Otherwise, `Passed` is true only if no row errored or failed.

To learn more, read [Evaluations from code](https://launchdarkly.com/docs/sdk/features/evals-from-code.md).

## Reference

This section lists the environment variables that configure the SDK, the errors that it returns, and its main functions.

### Configuration

Each environment variable fills the matching `InitOptions` field only if that field is empty. A field that you set wins over its variable, and a blank value counts as unset.

<table>
  <thead>
    <tr>
      <th>Variable</th>
      <th>InitOptions field</th>
      <th>Required</th>
      <th>Description</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`LD_SDK_KEY`</td>
      <td>`SDKKey`</td>
      <td>Yes, unless you set the field or use `InitWithClient`</td>
      <td>The server-side SDK key.</td>
    </tr>

    <tr>
      <td>`LD_BASE_URI`</td>
      <td>`BaseURI`</td>
      <td>No</td>
      <td>The polling base URI, for non-production environments.</td>
    </tr>

    <tr>
      <td>`LD_STREAM_URI`</td>
      <td>`StreamURI`</td>
      <td>No</td>
      <td>The streaming URI.</td>
    </tr>

    <tr>
      <td>`LD_EVENTS_URI`</td>
      <td>`EventsURI`</td>
      <td>No</td>
      <td>The events URI.</td>
    </tr>

    <tr>
      <td>`OTEL_EXPORTER_OTLP_ENDPOINT`</td>
      <td>`OTLPEndpoint`</td>
      <td>No</td>
      <td>The OTLP/HTTP trace endpoint. The SDK appends `/v1/traces`. The default is `https://otel.observability.app.launchdarkly.com`.</td>
    </tr>

    <tr>
      <td>`LD_SERVICE_NAME`</td>
      <td>`ServiceName`</td>
      <td>No</td>
      <td>The `service.name` resource attribute. The default is `go-sdk`.</td>
    </tr>

    <tr>
      <td>`LD_ENVIRONMENT`</td>
      <td>`Environment`</td>
      <td>No</td>
      <td>The `deployment.environment` resource attribute. Omitted if unset.</td>
    </tr>

    <tr>
      <td>`LD_ENVIRONMENT_ID`</td>
      <td>None</td>
      <td>No</td>
      <td>The LaunchDarkly environment ID. The SDK sends it as `environmentId` on every `$ld:ai:*` event and as `feature_flag.set.id` on handler spans, so that AgentControl monitoring can correlate traces. Omitted if unset.</td>
    </tr>
  </tbody>
</table>

### Errors

Match errors with `errors.Is`. Each error is wrapped with the key or provider that it concerns, and keeps the LaunchDarkly SDK's own reason if there is one.

<table>
  <thead>
    <tr>
      <th>Error</th>
      <th>Meaning</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`ErrNotInitialized`</td>
      <td>You called `GetClient` before `InitClient` or `InitWithClient`.</td>
    </tr>

    <tr>
      <td>`ErrConfigNotFound`</td>
      <td>The flag returned no config. It is missing, the wrong type, or could not be evaluated, for example because the client is not ready.</td>
    </tr>

    <tr>
      <td>`ErrConfigDisabled`</td>
      <td>The config variation is turned off.</td>
    </tr>

    <tr>
      <td>`ErrNoHandler`</td>
      <td>No handler serves the variation's provider and mode.</td>
    </tr>

    <tr>
      <td>`ErrGraphDisabled`</td>
      <td>The graph, or a node that it references, is turned off.</td>
    </tr>

    <tr>
      <td>`ErrConfigureLocked`</td>
      <td>You called `Configure` with a different effect after the first handler call.</td>
    </tr>
  </tbody>
</table>

### API reference

For the full API, read the [SDK API docs](https://pkg.go.dev/github.com/launchdarkly/go-ai-sdk). The following table lists the main functions in the `aiserver` package, and the convenience functions in the handler packages.

<table>
  <thead>
    <tr>
      <th>Function</th>
      <th>Purpose</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`Config(ConfigArgs)`</td>
      <td>Binds a config key. Use `Invoke` and `Stream` on the result.</td>
    </tr>

    <tr>
      <td>`Graph(key, GraphOptions)`</td>
      <td>Binds an agent graph. Use `Invoke` and `Stream` on the result.</td>
    </tr>

    <tr>
      <td>`ResolveGraph(ctx, key, ldctx, GraphOptions)`</td>
      <td>Returns the topology and node configs for framework-native runners, and an error for resolution failures.</td>
    </tr>

    <tr>
      <td>`InspectConfig(ctx, key, ldctx)`</td>
      <td>Reads a variation without calling a model. Returns an error for initialization, evaluation, and parse failures.</td>
    </tr>

    <tr>
      <td>`Configure(SDKOptions)`</td>
      <td>Sets the process-wide content capture default.</td>
    </tr>

    <tr>
      <td>`RunJudge`</td>
      <td>Runs a judge task from a call with `SkipJudges` set, for example on your own goroutine.</td>
    </tr>

    <tr>
      <td>`InitClient`, `InitWithClient`, `GetClient`, `Shutdown`</td>
      <td>Manage the client lifecycle.</td>
    </tr>

    <tr>
      <td>`SetLogger`</td>
      <td>Sets the SDK logger. The default is `slog.Default()`.</td>
    </tr>

    <tr>
      <td>`OpenAIMessages`, `ClaudeMessages`, `OpenAIAgents`, and others</td>
      <td>Convenience functions in the handler packages. Build the handler and call `Config().Invoke` in one step.</td>
    </tr>

    <tr>
      <td>`OpenAIGraph`, `ClaudeGraph`, `BedrockGraph`, `LangChainGraph`</td>
      <td>Graph wrappers in the agent handler packages. Bind a handler to `Graph`.</td>
    </tr>
  </tbody>
</table>

## Supported features

This SDK supports the following features:

* [Agent skills](https://launchdarkly.com/docs/sdk/features/agent-skills.md)
* [Anonymous contexts](https://launchdarkly.com/docs/sdk/features/anonymous.md)
* [Context configuration](https://launchdarkly.com/docs/sdk/features/context-config.md)
* [Customizing AgentControl configs](https://launchdarkly.com/docs/sdk/features/agentcontrol-config.md)
* [Evaluations from code](https://launchdarkly.com/docs/sdk/features/evals-from-code.md)
* [Private attributes](https://launchdarkly.com/docs/sdk/features/private-attributes.md)
* [Tracking AI metrics](https://launchdarkly.com/docs/sdk/features/ai-metrics.md)

## What's next

Now that you can call models with the Go AI SDK, explore these next steps:

* [Create configs](https://launchdarkly.com/docs/home/agentcontrol/create.md) to set up your own AgentControl configs in the LaunchDarkly UI.
* [Agent graphs](https://launchdarkly.com/docs/home/agentcontrol/agent-graphs.md) to build multi-agent workflows.
