SDK quick linksLaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, sample applications, and provider-specific packages:
| Resource | Location |
|---|---|
| SDK API documentation | SDK API docs |
| GitHub repository | node-server-sdk-ai |
| Sample application | Using Bedrock, Using OpenAI |
| Published module | npm |
| Provider-specific packages | OpenAI, LangChain, Vercel AI |
For use in server-side applications onlyThis SDK is intended for use in multi-user Node.js server applications. To learn more about LaunchDarkly’s different SDK types, read Choosing an SDK type.
Get started
LaunchDarkly AI SDKs interact with AgentControl configs. Configs are the LaunchDarkly resources that manage model configurations and messages for your generative AI applications. You can use the Node.js (server-side) AI SDK to customize your config based on the 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 Node.js (server-side) 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 to identify the latest version. The Node.js (server-side) AI SDK is built on the Node.js (server-side) SDK, so you’ll need to install that as well. Alternatively, you can install a server-side edge SDK instead. In addition, you can also use provider-specific AI SDK packages for better integration and improved version management with your preferred AI framework. The following provider-specific packages are available: These packages require Node.js version 16 or higher. Here’s how:init, LDContext, and initAi in your application code. If you are using TypeScript, you can optionally import the LaunchDarkly LDAIClient and LDAIConfig. These are implied, so are not strictly required.
Here’s how:
Initialize the client
After you install and import the SDK, create a single, shared instance ofLDClient. When the LDClient is initialized, 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 Node.js SDKs use an SDK keyBoth the Node.js (server-side) AI SDK and the Node.js (server-side) SDK use 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.
Configure the context
Configure 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:Customize a config
Customize your config to set values for variables used in messages or instructions based on the context attributes and variables you provide. The details of customizing a config depend on whether you are using configs in completion mode or agent mode. You set the mode for a particular config when you create it in the LaunchDarkly UI.Customize configs in completion mode
In completion mode, each variation in your config includes a single set of roles and messages used to prompt your generative AI model. Incompletion mode, use completionConfig() to customize the config. The completionConfig() function takes a config key, a context, a fallback value, and optional variables to use in the customization. It performs the evaluation, then returns the customized messages and model along with a tracker instance 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 configuration object 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.
Here’s how:
Customize configs in agent mode
In agent mode, each variation in your config includes a set of instructions, which enable multi-step workflows. Inagent mode, use agentConfig() or agentConfigs() to customize the config. The agentConfig() function customizes a single agent config, while the agentConfigs() function customizes an array of agent configurations.
The instructions returned by the SDK come directly from the instructions you define for the config variation in the LaunchDarkly UI. The goal or task shown in the UI is delivered unchanged as the instructions field in the SDK.
Customization requires a config key, fallback value, and optional variables for each agent-mode evaluation. Additionally, customization requires a context. Both functions perform the evaluation and then return the customized instructions for each config, along with a tracker instance for recording metrics. If the function cannot perform the evaluation or LaunchDarkly is unreachable, it returns the fallback value.
For example, you might use an empty, disabled configuration object 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.
Here’s how:
Use managed objects
As an alternative to the lower-level flow described in the rest of this guide, the SDK provides higher-level managed objects that combine model invocation, message conversion, and metrics tracking. UsecreateModel(), createAgent(), or createJudge() on the LDAIClient to get a ManagedModel, ManagedAgent, or Judge. This is the recommended pattern for new applications because it handles provider routing and metric extraction automatically.