LaunchDarkly AI SDK — optimization
This package provides helpers for running iterative AI prompt optimization workflows from within LaunchDarkly SDK-based applications. It drives the optimization loop — generating candidate variations, evaluating them with judges, and optionally committing winners back to LaunchDarkly — while delegating all LLM calls to your own handler functions.
Requirements
- Python
>=3.9 - A configured LaunchDarkly server-side SDK client
- The LaunchDarkly AI package (
launchdarkly-server-sdk-ai>=0.16.0) — pulled in automatically as a dependency LAUNCHDARKLY_API_KEYenvironment variable — required only when usingauto_commit=Trueoroptimize_from_config. Not needed for basicoptimize_from_optionsruns without auto-commit.
Installation
pip install launchdarkly-ai-optimizer
Quick Start
Basic optimization (optimize_from_options)
No LAUNCHDARKLY_API_KEY required unless auto_commit=True.
import ldclient
from ldai import LDAIClient
from ldai_optimizer import (
OptimizationClient,
OptimizationJudge,
OptimizationOptions,
OptimizationResponse,
LLMCallConfig,
LLMCallContext,
)
ldclient.set_config(ldclient.Config("sdk-your-sdk-key"))
ld = LDAIClient(ldclient.get())
client = OptimizationClient(ld)
def handle_llm_call(
run_id: str,
config: LLMCallConfig,
context: LLMCallContext,
is_evaluation: bool,
) -> OptimizationResponse:
# config.model, config.instructions, config.key are available
# context.user_input, context.current_variables are available
response = your_llm_client.chat(
model=config.model.name if config.model else "gpt-4o",
system=config.instructions,
user=context.user_input or "",
)
return OptimizationResponse(completion=response.text)
result = await client.optimize_from_options(
OptimizationOptions(
agent_key="my-agent",
handle_agent_call=handle_llm_call,
judge_model="gpt-4o-mini",
judges={
"quality": OptimizationJudge(
threshold=1.0,
acceptance_statement="The response is accurate and concise.",
)
},
model_choices=["gpt-4o", "gpt-4o-mini"],
variable_choices=[{"user_id": "user-123"}],
user_input_choices=["What is my account balance?"],
)
)
Ground truth optimization
from ldai_optimizer import GroundTruthOptimizationOptions, GroundTruthSample
result = await client.optimize_from_options(
GroundTruthOptimizationOptions(
agent_key="my-agent",
handle_agent_call=handle_llm_call,
judge_model="gpt-4o-mini",
judges={
"accuracy": OptimizationJudge(
threshold=1.0,
acceptance_statement="The response matches the expected answer.",
)
},
model_choices=["gpt-4o", "gpt-4o-mini"],
ground_truth_responses=[
GroundTruthSample(
user_input="What is 2+2?",
ground_truth_response="4",
)
],
)
)
Config-driven optimization (optimize_from_config)
Requires LAUNCHDARKLY_API_KEY.
from ldai_optimizer import OptimizationFromConfigOptions
result = await client.optimize_from_config(
OptimizationFromConfigOptions(
config_key="my-optimization-config",
project_key="my-project",
handle_agent_call=handle_llm_call,
auto_commit=True,
)
)
License
Apache-2.0
Metadata
Release files for launchdarkly-ai-optimizer 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| launchdarkly_ai_optimizer-0.2.1.tar.gz | 111.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| launchdarkly_ai_optimizer-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 174.9 kB
Release files / launchdarkly_ai_optimizer-0.2.1.tar.gz
| Download URL | launchdarkly_ai_optimizer-0.2.1.tar.gz |
|---|---|
| Size | 111.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73950f137cda84c8e751df7edbd3a67d3fc3ba736fd08746b6b6e14ec400f48b
|
|
BLAKE2b-256 checksum How to use checksums |
9c303cdd5e023eef0f4b298a586025c8c384d016854616321f72e54b24d0d0d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / launchdarkly_ai_optimizer-0.2.1-py3-none-any.whl
| Download URL | launchdarkly_ai_optimizer-0.2.1-py3-none-any.whl |
|---|---|
| Size | 63.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc3264788bbcf4c344ab2168a67684534b79bb2e31c016566a9ed2fd47cb6ca8
|
|
BLAKE2b-256 checksum How to use checksums |
e31ec2d7e45156fe2dea96c13ab3963fc4c843fb443a4a47a4b9e3067ae54da8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|