This guide shows you how to build a simple AI-powered chatbot using LaunchDarkly AgentControl with multiple AI providers, including Anthropic, OpenAI, and Google.
Using AgentControl, you can manage models and prompts outside of code, switch providers without redeploying, and monitor performance in real time.
You’ll learn how to:
By the end of this tutorial, you’ll have a working chatbot that demonstrates LaunchDarkly’s AgentControl capabilities across multiple providers.
The complete code for this tutorial is available in the simple-chatbot repository. For additional code examples and implementations, check out the LaunchDarkly Python AI Examples repository, which includes practical examples of AgentControl configs with various providers and use cases.
Before you begin, you need the following:
Access to the following accounts:
A development environment with:
The following keys:
This guide builds a chatbot using completion-based AgentControl configs in a messages array format. If you use LangGraph or CrewAI, you may want to use agent mode instead.
The following sections include best practices to help you avoid common issues and reduce debugging time.
Reusing configs across users breaks targeting. Instead, fetch a fresh config for each request:
Provide a fallback so your application does not crash when unexpected issues occur, such as LaunchDarkly being unavailable or API keys being incorrect:
Check if the config is enabled before using it:
Never send PII to LaunchDarkly. Here’s a bad and a good example:
Your chat history grows with every turn. After 50 exchanges, each request may include thousands of tokens.
Here’s how to limit it:
Without tracking, it is difficult to understand how token usage affects cost.
Here’s how to track token usage:
Your provider methods should return the full response object, not just text, so you can access usage metadata. The code examples here return full responses where tracking is needed.
Start by building a minimal chatbot application using a LaunchDarkly AgentControl config with Anthropic’s Claude.
First, create a new directory for your project:
Then, create a virtual environment and activate it:
Install the required packages:
Create a requirements.txt file:
First, add .env to your .gitignore file to keep credentials secure:
Now create a .env file in your project root:
Create a file called simple_chatbot.py and add the following:
Click to expand the complete simple_chatbot.py
Run the chatbot:
You should see output like this:
Try asking questions and chatting with the AI. The chatbot automatically uses whichever AI provider you configured.
You now have a working chatbot with multi-provider support.
Now, create an AgentControl config in LaunchDarkly with two variations to demonstrate how to dynamically control AI behavior.
For detailed guidance on creating AgentControl configs, read the AgentControl Quickstart.
To create an AgentControl config:
Create project.simple-chatbot.

Update your .env file with this key:
Then, create a new AgentControl config:
simple-config.Variation Name: friendly
Model Provider: Anthropic, or your preferred provider
Model: claude-3-haiku-20240307
System Prompt:
Parameters:
0.8 (more creative)500

To copy and add your AgentControl config key:
At the top of your AgentControl config page, copy the Config Key.
Update your .env file with this key:
To edit the AgentControl config’s targeting:
update in the Comment field and Production in the Confirm field.
Now create a new file called simple_chatbot_with_targeting.py that adds persona selection capabilities and LaunchDarkly integration.
Click to expand the complete simple_chatbot_with_targeting.py code
Run the chatbot:
Try asking: “What’s the best way to learn a new programming language?”
The response is warm and casual, possibly with an emoji.
LaunchDarkly AgentControl let you change AI behavior instantly without redeploying your application. This example changes the response language.
Keep your chatbot running from Step 2.4 and follow these steps to update the system prompt to respond in Portuguese:
In LaunchDarkly, navigate to your AgentControl config.
Go to your simple-config.
Click the Variations tab.
Select the friendly variation.
Change the system prompt to:

Now, test the change without restarting the chatbot. In your still-running chatbot, type a new message:
Notice how the chatbot’s behavior changed instantly without:
This demonstrates how AgentControl configs support real-time experimentation and iteration. You can update prompts, adjust behaviors, or switch languages based on user feedback or business needs.
Now, you can explore advanced targeting capabilities by creating persona-based variations. This demonstrates how to deliver different AI experiences to different user segments.
To learn more about targeting capabilities, read Config targeting.
Create three persona variations in LaunchDarkly:
Navigate to your simple-config AgentControl config.
Click the Variations tab.
Click Add Variation and add a business persona:
Variation Name: business
Model: claude-3-haiku-20240307
System Prompt:
Temperature: 0.4
Repeat the process for a creative persona variation:
Variation Name: creative
Model: chatgpt-4o-latest (OpenAI)
System Prompt:
Temperature: 0.9

Now, configure persona-based targeting:
simple-config.Persona-based targeting.personais one ofbusinessbusiness variationpersona is one of creative, serve creative.friendly.
Run the chatbot with persona support:
Test the business persona:
Type switch and select option 2 for the creative persona:
In this section, you learned:
LaunchDarkly provides monitoring for AgentControl configs. Next, ensure your data flows correctly.
To learn more about monitoring capabilities, read Monitor AgentControl configs.
LaunchDarkly AI SDKs provide comprehensive metrics tracking to help you monitor and optimize your AI model performance. The SDK includes both individual track* methods and provider-specific convenience methods for recording metrics.
Available metrics include:
The AI SDKs provide two approaches to recording metrics:
track_metrics_of(extractor, func) runs the wrapped chat completion call, then applies a provider-specific extractor (such as get_ai_metrics_from_response from ldai_openai) to record duration, token usage, and success or error in one call.track_duration(), track_tokens(), track_success(), track_error(), and track_feedback() for manual metric recording.Create a tracker for each generation by calling config.create_tracker() on the result of completion_config(). The tracker is specific to that config variation, so always call completion_config() and create a fresh tracker each time you generate content to ensure metrics are correctly associated with the right variation.
For delayed feedback, such as user ratings that arrive after generation, persist the tracker’s resumption token and use it to recreate the tracker later:
create_tracker returns an ldclient.Result. Check result.is_success() before reading result.value.
To learn more about tracking AI metrics, read Tracking AI metrics.
Create a file called simple_chatbot_with_targeting_and_tracking.py:
Click to expand the complete simple_chatbot_with_targeting_and_tracking.py code
To test the monitoring flow:
After running the monitored chatbot:
simple-chatbot-config.The monitoring dashboard provides real-time insights into your AgentControl config’s performance:

In the dashboard, you see several key sections:

The token metrics include:
max_tokens parameter and the model’s verbosity.To learn more about monitoring and metrics, read Monitor AgentControl configs.
You built a working chatbot. When building your own application, check the following before releasing it to end users.
If you always see the fallback model, your LaunchDarkly connection is not working.
AI providers can become unavailable. Your application should continue to function.
Do not roll out changes to all users at once:
LaunchDarkly makes this easy with percentage rollouts on the Targeting tab.
You will not know if your AI is giving good answers unless you measure it. Consider adding online evaluations once you are live. Read when to add online evals for guidance.
AgentControl configs are not the right fit for every project. Here are some common use cases:
If you are updating prompts frequently, hardcoding them can slow iteration. AgentControl lets you test different prompts without redeploying.
Example: You run a customer support chatbot. You want to test whether a formal tone or casual tone works better. With AgentControl configs, you create two variations and switch between them from the dashboard.
Free users get faster, lower-cost responses. Paid users get slower, higher-quality responses.
Example: A SaaS application with tiered pricing. The free tier uses gpt-4o-mini with temperature 0.3. The premium tier uses claude-3-5-sonnet with temperature 0.7. You target based on the tier attribute in the user context.
Your primary provider is unavailable. You need to switch to a backup immediately.
Example: Anthropic has an outage. You log in to LaunchDarkly, change the default variation from Anthropic to OpenAI, and save. All requests now use OpenAI, with no redeployment needed.
You think a lower-cost model might perform well enough for most queries. You want to test it with real traffic.
Example: You create one variation using claude-3-haiku and another using claude-3-5-sonnet. You roll out the lower-cost model to 20% of users and compare quality metrics.
If you’re using LangGraph or CrewAI, use agent mode instead of the completion mode shown in this tutorial.
This tutorial uses completion mode with messages in array format. If you’re building:
The choice depends on your architecture. If you’re calling client.chat.completions.create() or a similar method, completion mode is probably the right choice.
In this guide, you learned how to:
LaunchDarkly AgentControl let you manage AI behavior across multiple providers without code changes, so you can iterate more quickly and deploy more safely.