This guide shows how to manage AI model configuration and prompts for an OpenAI-powered application in a runtime environment. It uses the LaunchDarkly Node.js (server-side) AI SDK and AgentControl to dynamically customize your application.
Using AgentControl to customize your application means you can:
This guide steps you through the process of working in your application and in LaunchDarkly to customize your application.
If you’re already familiar with setting up AgentControl in the LaunchDarkly UI and want to skip straight to the sample code, you can find it on GitHub.
If you’re not familiar with AgentControl and would like additional explanation, you can start with the Quickstart for AgentControl and come back to this guide when you’re ready for a more realistic example.
You can find reference guides for each of the AI SDKs at AI SDKs.
To complete this guide, you must have the following prerequisites:
In this example, you manage a product recommendation system for an e-commerce platform. Using the LaunchDarkly Node.js (server-side) AI SDK, you’ll configure AI prompts to provide personalized product suggestions based on your customers’ preferences. You’ll also track metrics, such as the number of output tokens used by your generative AI application.
First, install the required SDKs:
Then, set up credentials in your environment:
If you are starting from scratch, you can use the Node dotenv package in conjunction with a .env file to load your API keys.
Inside of your project, create a shared utility for initializing the LaunchDarkly SDK and its AI client.
Create a config/launchDarkly.ts file:
Next, create an AgentControl config in the LaunchDarkly UI. AgentControl configs are the LaunchDarkly resources that manage model configurations and messages for your generative AI applications.
To create an AgentControl config:
Then, configure the model and prompt by creating a variation. Every AgentControl config has one or more variations, each of which includes your AI messages and model configuration.
Here’s how:
Next, add system, user, or assistant messages to define your prompt. In this example, you’ll augment the prompt with saved user preferences.
Start with the system message:
Now, add an assistant message that pre-loads the model with the user’s preferences and provides some instructions about the input format, response format, and context:
The double curly braces in the prompts allow you to augment the messages with customized data at runtime. To learn more, read Syntax for customization.
Within the UI, the variation will look like the following:

Click Review and save at the top right of the page to save the configuration.
Targeting is how you specify that specific end users working in your application will receive specific variations that you’ve created in your AgentControl config.
To set up targeting, click the Targeting tab. Targeting for AgentControl configs is on by default. Set the default targeting rule to serve the “Shopping preference assistant” variation you just created for your test environment.
To specify the AgentControl config variation to use by default when the AgentControl config is toggled on:
By default, this AgentControl config will now serve the variation shopping preference assistant variation:

To integrate the LaunchDarkly AgentControl config into your application, you’ll need to set up your OpenAI client, set up your application data, add your completion code, and finally put it all together.
Next, set up your OpenAI client. Following a similar pattern as above, create a config/openAi.ts file to initialize the OpenAI client:
Then, set up the data for your application. Normally, the product data that your application retrieves would come from a database. For this example, create a data/products.ts file to serve mock data:
Additionally, add the following data/mockProducts.ts file in the same folder:
The data contains the name, price, type, preference tags to match to a customer’s preferences, a description, and a product ID. This should give the AI model ample data to match to a customer’s query.
Next, add the AgentControl config and completion code into your application. Create a completions/shoppingAssistant.ts file:
When you wrap the completion call in tracker.trackMetricsOf, paired with getAIMetricsFromResponse from the @launchdarkly/server-sdk-ai-openai package, you can monitor the performance of your application.
Now that you’ve set up the AgentControl config and completion, you need to plug this into your app somewhere where you can get a response.
Here’s an example:
With these parameters, run your application and you should get a response that looks like this:
You can play around with the preferences or the query passed in to see different results.
At this point, when you run your application, you receive a response from the model that has relevant results to customized to the input, but the formatting isn’t customer-facing. If you were storing your prompts in code, this would require a code change to format differently. However, because you have it in an AgentControl config, you can update the formatting without having to touch the code or redeploy the application.
Next, let’s adjust the formatting. You can do this without changing application code, or even requiring developer involvement. If any member of your team wants to edit the prompt, adjust the formatting or tone, or make any changes, they can do that directly from the LaunchDarkly UI.
In the LaunchDarkly UI, navigate to the Variations tab of the AgentControl config you created earlier. You’ll edit the existing variation to reflect the formatting you’d prefer for the output. You’ll also update the system messaging to be a bit more friendly and less academic.
First, edit the existing variation and adjust the system message to the following:
Next, adjust the formatting on the assistant response format message to be more customer-facing, provide the result in more nicely-formatted Markdown, and provide a link to the product in the catalog:
With these parameters, run your application again:
and you should get a response that looks like this:
The output now contains a friendlier message, links to the products, and provides the descriptions in Markdown format. As long as you’re providing the data in the completion input context, you can update this output to contain any of the input parameters without the need for code changes.
When you set up the OpenAI completion call, you minted a tracker via aiConfig.createTracker() and passed the OpenAI call to tracker.trackMetricsOf with getAIMetricsFromResponse as the metrics extractor. This pattern automatically captures duration, success/error, and token usage metrics for OpenAI:
After you’ve run your completion a few times, check the Monitoring tab for your AgentControl config in the LaunchDarkly UI. The Monitoring tab displays metrics that are automatically tracked, including:
In combination with LaunchDarkly’s targeting rules, you can duplicate the prompts with different models and messages to see the differences in their generation patterns.
You can also use the LaunchDarkly AI client to keep a running total of positive and negative sentiment about the prompt generation. Feedback must be recorded on the same tracker, with the same runId, as the AI call it is about, or the feedback won’t be attributed to that response. There are two cases to handle: same-request feedback and deferred feedback.
If the feedback signal is available during the same request that produced the response (for example, an automated validation step runs immediately after the completion), reuse the tracker you already minted for trackMetricsOf:
If the feedback arrives later (for example, the user clicks thumbs up or down minutes or hours after the response was shown), the original tracker object is gone. To prevent this, use the resumption-token pattern: at generation time, serialize tracker.resumptionToken alongside the response so your frontend can echo it back. In the feedback handler, call aiClient.createTracker(resumptionToken, ctx) to rebuild a tracker bound to the original runId, and then record feedback on the rebuilt tracker.
In this guide, you reviewed how to manage AI model configuration and prompts for an OpenAI-powered application, and how to dynamically customize your application.
Using AgentControl with the LaunchDarkly AI SDKs means you can:
To learn more, read AgentControl and AI SDKs.