This guide shows how to manage AI model usage by customer tier in an OpenAI-powered application. It uses the LaunchDarkly Python AI SDK and AgentControl to dynamically adjust the model used based on customer details.
Using AgentControl and targeting to customize your applications means you can:
This guide steps you through the process of working in your application and in LaunchDarkly to customize your application and its targeting.
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 have an application that provides chat support. When creating your generated content, you want to use one AI model for the content you provide to the customers who are paying you, and a different AI model for the content you provide to the customers on your free tier. You also want to understand whether your paying customers are getting a better experience.
First, install the Python AI SDK:
Then, set up credentials in your environment. The example below uses $Environment_SDK_KEY and $Environment_OPENAI_KEY to refer to an active LaunchDarkly SDK key and your OpenAI key, respectively.
You can find your SDK keys on the SDK keys page under Settings. To learn more, read SDK credentials.
Next, import the LaunchDarkly LDAIClient into your application code and initialize it:
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, create two variations. Every AgentControl config has one or more variations, each of which includes your AI messages and model configuration.
Here’s how:
Here’s how the two variations should look after you’ve set them up:


Next, set up targeting rules for your AgentControl config. These rules determine which of your customers receives which variation of your AgentControl config.
To specify the AgentControl config variation to use by default when the AgentControl config is toggled on:
To specify a different AgentControl config variation to use for premium customers:
customer_type. You’ll set this attribute in your code later.premium. You’ll set this value in your code later.By default, the AgentControl config is set to On. Click Review and save.
Here’s what the Targeting tab of your AgentControl config should look like:

In your code, use the completion_config function in the LaunchDarkly AI SDK to customize the config. You need to call completion_config each time you generate content from your AI model.
The completion_config function returns the customized config, which includes the messages and model configuration for the variation that the end user should receive. Customization is what happens when your application’s code sends the LaunchDarkly AI SDK information about a particular config and the end user that has encountered it in your app, and the SDK sends back the value of the variation that end user should receive.
To call the completion_config function, you need to provide information about the end user who is working in your application. For example, you may have this information in a user profile within your app.
Create a tracker for each generation by calling config.create_tracker(), then wrap your chat completion call with tracker.track_metrics_of. The track_metrics_of method runs the wrapped function, then uses the extractor (get_ai_metrics_from_response) to record duration, token usage, and success or error for that generation.
Here’s how:
As customers encounter chat support in your application, LaunchDarkly monitors the performance of your AgentControl config: the tracker created from the result of the completion_config function automatically records various metrics. To view them, select the Monitoring tab for your config in LaunchDarkly.
In this example, you can review the results to determine:
You could use this information to make a business decision about whether the performance differences are worth the cost differences of running each model for your different customer tiers.
To learn more, read Monitor AgentControl configs.
In this guide, you learned how to manage AI model usage by customer tier in an OpenAI-powered application, and how to review the performance of those models based on customer feedback and token usage.
For additional examples, read the other AgentControl guides in this section. To learn more, read AgentControl and AI SDKs.