Skip to main content
This topic explains how to configure the SDK to use agent optimization. After you enable agent optimization, you can start tuning AI agent prompts, models, and parameters.

Install agent optimization

Agent optimization is available for the Python AI SDK. Here’s how to install it:

Add your LaunchDarkly API key

If you want to pull configurations from LaunchDarkly or commit variations, metrics, and scores back to LaunchDarkly, you must provide your LAUNCHDARKLY_API_KEY as an environment variable. The agent optimization SDK automatically ingests the API key if you include it. To learn how to get your LaunchDarkly API token, read API access tokens.

Running your first optimization

There are two ways to run an optimization: Regardless of which option you use, the same data is emitted and returned. To learn more, read Output.

Use a LaunchDarkly config object

optimize_from_config pulls judges, model choices, and evaluation parameters from a LaunchDarkly configuration. First, you create an optimization config in the LaunchDarkly UI. Then you point your code at the config to bring its specifications into the run. To learn how to create the config, read Performing optimization runs. Here’s how to pull in the config:

Do it in your code

optimize_from_options lets you define all judges, model choices, and evaluation parameters directly in code without a LaunchDarkly configuration object. To learn more about optimize_from_options, read Optimization runs without a config.

Output

This call returns an OptimizationContext representing the final output of the optimization. You can access OptimizationContext.history to get all of the historical OptimizationContexts generated during the run. To learn more, read the OptimizationContext reference.

Wrapping up

These are the two core ways to run an optimization, but there is even more optimization functionality. Read these other resources to get more information: