> ## 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.

# Context configuration: AI SDKs

> This topic explains how to configure contexts in LaunchDarkly SDKs. This feature is available for both client-side and server-side SDKs. This page covers ai sdks.

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

## AI SDKs

Here are the configuration options for contexts in AI SDKs:

* [.NET AI](#net-ai)
* [Go AI](#go-ai)
* [Node.js (server-side) AI](#nodejs-server-side-ai)
* [Python AI](#python-ai)
* [Ruby AI](#ruby-ai)

### .NET AI

<Accordion title="Expand .NET AI SDK code sample">
  In the .NET AI SDK, you can construct a `Context` that only has a key by calling `Context.New`. The context kind defaults to "user," or you can supply a different context kind. Alternatively, you can use the `Context.Builder` method for building a context with other properties.

  The argument to `Builder` is the context's key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.

  Here's an example:

  <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>

  Here's how to construct a context with a context kind of something other than "user":

  <CodeGroup>
    ```csharp title=".NET AI SDK" theme={null}
          var context2 = Context.New(ContextKind.Of("organization"), "example-organization-key");
    ```
  </CodeGroup>

  Here's how to construct a multi-context, which includes multiple context kinds:

  <CodeGroup>
    ```csharp title=".NET AI SDK" theme={null}
          var userContext = Context.New("example-context-key");

          var deviceContext = Context.Builder("example-device-key")
              .Kind("device")
              .Build();

          var multiContext = Context.NewMulti(userContext, deviceContext);

    ```
  </CodeGroup>

  <Note title="Interpreting custom attribute types">
    The optional name and kind attributes, which you can set with `.Name()` and `.Kind()`, expect string values. If the kind attribute is not specified, it is assumed to be "user." Other attribute values can be booleans, numbers, strings, arrays, or JSON objects. The SDK uses the [`LdValue`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.LdValue.html) type to represent arrays and objects. The .NET SDK is strongly-typed, so be aware of this distinction.
  </Note>

  If an attribute is a JSON object, then in your AI Config targeting, you can use `/` as a delimiter to refer to specific object fields. For example, if you have an "address" attribute that includes several fields, then you could use `/address/city` in your targeting rules. You can use `.` as a delimiter in your AI Config message. Continuing the same example, you could use `{{ldctx.address.city}}` in your message, and the value of the "city" field will be substituted when you customize the AI Config. To learn more, read [Target with flags](/home/flags/target) and [Customizing AI Configs](/sdk/features/ai-config#net-ai).

  To learn how to configure private attributes in the .NET AI SDK, read [Private attributes](/sdk/features/private-attributes/ai#net-ai).

  To learn how to configure anonymous contexts in the .NET AI SDK, read [Anonymous contexts and users](/sdk/features/anonymous#net-ai).
</Accordion>

### Go AI

<Accordion title="Expand Go AI SDK code sample">
  The Go AI SDK defines a `Context` struct and a `Builder`. The context `key` is the only mandatory context attribute. The combination of `key` and `kind` must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.

  Here's an example:

  <CodeGroup>
    ```go title="Go AI SDK" theme={null}
          import (
              "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
              "github.com/launchdarkly/go-sdk-common/v3/ldvalue"
          )

          // Context with only a key
          // by default, the context kind is "user"
          context1 := ldcontext.New("example-context-key")

          // Context with a key plus other attributes
          context2 := ldcontext.NewBuilder("context-key-456def").
              Kind("organization").
              Name("Global Health Services").
              SetString("email", "info@globalhealthexample.com").
              SetValue("address", ldvalue.ObjectBuild().
                  SetString("street", "123 Main Street").
                  SetString("city", "Springfield")).
              SetValue("groups", ldvalue.ArrayOf(
                ldvalue.String("Acme"), ldvalue.String("Global Health Services"))).
              Build()
    ```
  </CodeGroup>

  Here's how to construct a context with a context kind of something other than "user":

  <CodeGroup>
    ```go title="Go AI SDK" theme={null}
          context1 := ldcontext.NewWithKind("organization", "example-organization-key")
    ```
  </CodeGroup>

  Here's how to construct a multi-context, which includes multiple context kinds:

  <CodeGroup>
    ```go title="Go AI SDK" theme={null}
          multiContext := ldcontext.NewMulti(
              ldcontext.New("example-user-key"),
              ldcontext.NewWithKind("device", "example-device-key")
          )
    ```
  </CodeGroup>

  Each individual context within a multi-context can have the same attributes. The only restriction is that each context has to have a different context `kind` from the others within the multi-context.

  You can also use the context builder to create each of the individual contexts:

  <CodeGroup>
    ```go title="Go AI SDK" theme={null}
          multiContext := ldcontext.NewMulti(
              ldcontext.NewBuilder("example-user-key").Name("Sandy").Build(),
              ldcontext.NewBuilder("example-device-key").Kind("device").Name("iPad").Build(),
          )
    ```
  </CodeGroup>

  <Note title="Interpreting custom attribute types">
    The kind and name attributes expect string values. You can set the kind, or, if you do not set it, LaunchDarkly assumes that the context kind is "user." Other attribute values can be booleans, numbers, strings, arrays, or JSON objects. These types are all represented by the [`ldvalue.Value`](https://pkg.go.dev/github.com/launchdarkly/go-sdk-common/v3@v3.0.0/ldvalue#Value) type. The Go SDK is strongly-typed, so be aware of this distinction.
  </Note>

  If an attribute is a JSON object, then in your AI Config targeting, you can use `/` as a delimiter to refer to specific object fields. For example, if you have an "address" attribute that includes several fields, then you could use `/address/city` in your targeting rules. You can use `.` as a delimiter in your AI Config message. Continuing the same example, you could use `{{ldctx.address.city}}` in your message, and the value of the "city" field will be substituted when you customize the AI Config. To learn more, read [Target with flags](/home/flags/target) and [Customizing AI Configs](/sdk/features/ai-config#go-ai).

  To learn how to configure private attributes in the Go AI SDK, read [Private attributes](/sdk/features/private-attributes/ai#go-ai).

  To learn how to configure anonymous contexts in the Go AI SDK, read [Anonymous contexts and users](/sdk/features/anonymous#go-ai).
</Accordion>

### Node.js (server-side) AI

<Accordion title="Expand Node.js (server-side) AI SDK code sample">
  In the Node.js (server-side) AI SDK, contexts are JSON objects. The `key` property is the context key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.

  Here's an example:

  <CodeGroup>
    ```ts title="Node.js (server-side) AI SDK (TypeScript)" theme={null}
          const context: LDContext = {
            kind: 'user',
            key: 'example-user-key',
            firstName: 'Sandy',
            lastName: 'Smith',
            email: 'sandy@example.com',
            groups: ['Acme', 'Global Health Services'],
          };
    ```

    ```js title="Node.js (server-side) AI SDK (JavaScript)" theme={null}
          const context = {
            kind: 'user',
            key: 'example-user-key',
            firstName: 'Sandy',
            lastName: 'Smith',
            email: 'sandy@example.com',
            groups: ['Acme', 'Global Health Services'],
          };
    ```
  </CodeGroup>

  Here's how to construct a context with a context kind of something other than "user":

  <CodeGroup>
    ```ts title="Node.js (server-side) AI SDK (TypeScript)" theme={null}
          const context: LDContext = {
             kind: 'device',
             key: 'example-device-key'
          }
    ```
  </CodeGroup>

  Here's how to construct a multi-context, which includes multiple context kinds:

  <CodeGroup>
    ```ts title="Node.js (server-side) AI SDK (TypeScript)" theme={null}
          const context: LDContext = {
            kind: 'multi',
            user: { key: 'example-user-key' },
            device: { key: 'example-device-key' }
          }
    ```
  </CodeGroup>

  <Note title="Interpreting custom attribute types">
    The optional `name` and `kind` attributes expect string values. If the `kind` attribute is not specified, it is assumed to be "user." Other attribute values can be booleans, numbers, strings, arrays, or JSON objects.
  </Note>

  If an attribute is a JSON object, then in your AI Config targeting, you can use `/` as a delimiter to refer to specific fields. For example, if you have an "address" attribute that includes several fields, then you could use `/address/city` in your targeting rules. You can use `.` as a delimiter in your AI Config message. Continuing the same example, you could use `{{ldctx.address.city}}` in your message, and the value of the "city" field will be substituted when you customize the AI Config. To learn more, read [Target with AI Configs](/home/ai-configs/target) and [Customizing AI Configs](/sdk/features/ai-config#nodejs-server-side-ai).

  To learn how to configure private attributes in the Node.js (server-side) AI SDK, read [Private attributes](/sdk/features/private-attributes/ai#nodejs-server-side-ai).

  To learn how to configure anonymous contexts in the Node.js (server-side) AI SDK, read [Anonymous contexts and users](/sdk/features/anonymous#nodejs-server-side-ai).
</Accordion>

### Python AI

<Accordion title="Expand Python AI SDK code sample">
  In the Python AI SDK, the `Context` class has a `create` method for creating a context with a context kind of "user" and with only a key. It has a `builder` method for building a context with other properties.

  The argument to `Context.builder` is the context's key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.

  Here's an example:

  <CodeGroup>
    ```python title="Python AI SDK" theme={null}
          context = Context.builder("example-context-key") \
              .set("firstName", "Sandy") \
              .set("lastName", "Smith") \
              .set("email", "sandy@example.com") \
              .set("groups", ["Acme", "Global Health Services"]) \
              .build()
    ```
  </CodeGroup>

  Here's how to construct a context with a context kind of something other than "user":

  <CodeGroup>
    ```python title="Python AI SDK" theme={null}
          context1 = Context.create("example-organization-key", "organization")
    ```
  </CodeGroup>

  Here's how to construct a multi-context, which includes multiple context kinds:

  <CodeGroup>
    ```python title="Python AI SDK" theme={null}
          multi_context = Context.create_multi(
              Context.create("example-user-key"),
              Context.create("example-device-key", "device")
          )
    ```
  </CodeGroup>

  If you have many attributes to set, you can also create a context from a dictionary:

  <CodeGroup>
    ```python title="Python AI SDK" theme={null}
          pre_existing_dict = {
              'key': 'example-context-key',
              'kind': 'user',
              'firstName': 'Sandy',
              'lastName': 'Smith',
              'email': 'sandy@example.com',
              'groups': ['Acme', 'Global Health Services'],
          }

          context = Context.from_dict(pre_existing_dict)
    ```
  </CodeGroup>

  <Note title="Interpreting custom attribute types">
    The optional name and kind attributes expect string values. If the "kind" attribute is not specified, it is assumed to be "user." Other attribute values can be booleans, numbers, strings, arrays, or objects.
  </Note>

  If an attribute is a JSON object, then in your AI Config targeting, you can use `/` as a delimiter to refer to specific fields. For example, if you have an "address" attribute that includes several fields, then you could use `/address/city` in your targeting rules. You can use `.` as a delimiter in your AI Config message. Continuing the same example, you could use `{{LDCTX.address.city}}` in your message, and the value of the "city" field will be substituted when you customize the AI Config. To learn more, read [Target with AI Configs](/home/ai-configs/target) and [Customizing AI Configs](/sdk/features/ai-config#python-ai).

  To learn how to configure private attributes in the Python AI SDK, read [Private attributes](/sdk/features/private-attributes/ai#python-ai).

  To learn how to configure anonymous contexts in the Python AI SDK, read [Anonymous contexts and users](/sdk/features/anonymous#python-ai).
</Accordion>

### Ruby AI

<Accordion title="Expand Ruby AI SDK code sample">
  In the Ruby AI SDK, contexts are instances of `LaunchDarkly::LDContext`.

  The `key` property is the context's key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key, an email address, or a hash string, as long as the same context always has the same key. We recommend using a hash string if possible.

  Here's an example:

  <CodeGroup>
    ```ruby title="Ruby AI SDK" theme={null}
          context = LaunchDarkly::LDContext.create({
              key: "example-user-key",
              kind: "user",
              firstName: "Sandy",
              lastName: "Smith",
              email: "sandy@example.com",
              groups: ["Acme", "Global Health Services"]
          })
    ```
  </CodeGroup>

  Here's how to construct a context with a context kind of something other than "user":

  <CodeGroup>
    ```ruby title="Ruby AI SDK" theme={null}
          context = LaunchDarkly::LDContext.with_key("example-context-key", "organization")
    ```
  </CodeGroup>

  Here's how to construct a multi-context, which includes multiple context kinds:

  <CodeGroup>
    ```ruby title="Ruby AI SDK" theme={null}
          multi_context = LaunchDarkly::LDContext.create_multi([
              LaunchDarkly::LDContext.with_key("example-user-key"),
              LaunchDarkly::LDContext.with_key("example-device-key", "device"),
          ])
    ```
  </CodeGroup>

  <Note title="Interpreting custom attribute types">
    The optional name and kind attributes expect string values. If the "kind" attribute is not specified, it is assumed to be "user" and the hash is assumed to be in the legacy user format. Other attribute values can be booleans, numbers, strings, arrays, or objects.
  </Note>

  If an attribute is a JSON object, then in your flag or segment targeting, you can use `/` as a delimiter to refer to specific object fields. For example, if you have an "address" attribute that includes several fields, then you could use `/address/city` in your targeting. To learn more, read [Target with flags](/home/flags/target).

  You can use `.` as a delimiter in your AI Config message. Continuing the same example, you could use `{{LDCTX.address.city}}` in your message, and the value of the "city" field will be substituted when you customize the AI Config. To learn more, read [Target with AI Configs](/home/ai-configs/target) and [Customizing AI Configs](/sdk/features/ai-config#ruby-ai).

  To learn how to configure private attributes in the Ruby AI SDK, read [Private attributes](/sdk/features/private-attributes/ai#ruby-ai).

  To learn how to configure anonymous contexts in the Ruby AI SDK, read [Anonymous contexts and users](/sdk/features/anonymous#ruby-ai).
</Accordion>
