Skip to main content

LaunchDarkly AI SDK — optimization

PyPI

[!CAUTION] This package is in pre-release and not subject to backwards compatibility guarantees. The API may change based on feedback.

Pin to a specific minor version and review the changelog before upgrading.

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_KEY environment variable — required only when using auto_commit=True or optimize_from_config. Not needed for basic optimize_from_options runs without auto-commit.

[!NOTE] LAUNCHDARKLY_API_KEY is used exclusively for discrete LaunchDarkly REST API calls (fetching configs, publishing results). It is never included in any LLM prompt and is never forwarded to your handler callbacks. All API calls made by this package are isolated; they have no access to your runtime environment beyond the key you explicitly provide via the environment variable.

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

launchdarkly_ai_optimizer-0.2.1.tar.gz (111.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

launchdarkly_ai_optimizer-0.2.1-py3-none-any.whl (63.9 kB view details)

Uploaded Python 3

File details

Details for the file launchdarkly_ai_optimizer-0.2.1.tar.gz.

File metadata

File hashes

Hashes for launchdarkly_ai_optimizer-0.2.1.tar.gz
Algorithm Hash digest
SHA256 73950f137cda84c8e751df7edbd3a67d3fc3ba736fd08746b6b6e14ec400f48b
MD5 fb0811f5300c49e9e75dcbc382f4d473
BLAKE2b-256 9c303cdd5e023eef0f4b298a586025c8c384d016854616321f72e54b24d0d0d8

See more details on using hashes here.

File details

Details for the file launchdarkly_ai_optimizer-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for launchdarkly_ai_optimizer-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bc3264788bbcf4c344ab2168a67684534b79bb2e31c016566a9ed2fd47cb6ca8
MD5 139ef2e25207f15b1abe69b70eb71aa8
BLAKE2b-256 e31ec2d7e45156fe2dea96c13ab3963fc4c843fb443a4a47a4b9e3067ae54da8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 files

0.2.0

2 files

0.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page