Skip to main content

launchdarkly-ai-langchain-agents

LangChain handler for launchdarkly-ai-server using LangGraph's StateGraph (langgraph). Delegates the full agentic loop to the LangGraph ReAct agent. Works with any BaseChatModel — defaults to ChatOpenAI.

provides_for: ['*', 'agent'] — matches any flag variation where meta.mode is "agent" and no more-specific handler is registered. LangChain is a framework adapter, not a provider: it routes through langchain-anthropic, langchain-openai, and others at runtime based on config.provider.name. Use '*' so that flags configured with provider.name = "Anthropic" or "OpenAI" are automatically handled without requiring a separate native handler.

Installation

pip install launchdarkly-ai-server launchdarkly-ai-langchain-agents

The default model is ChatOpenAI, so set OPENAI_API_KEY unless you pass a custom BaseChatModel.

Usage

With the default model (ChatOpenAI)

import asyncio
from launchdarkly_ai_server import config, shutdown
from launchdarkly_ai_langchain_agents import create_langchain_agents_handler

async def main():
    result = await config(
        key="my-ai-config-flag",
        handler=create_langchain_agents_handler(),
        tool_handlers={"search": lambda q: "..."},
    ).invoke(
        "Research and summarize feature flagging best practices",
        {"kind": "user", "key": "user-123"},
    )

    print(result.response)
    await shutdown()

asyncio.run(main())

Tool handlers may be sync or async. Sync handlers run on the event-loop thread, so blocking I/O stalls the agent. Keep graph __handoff_* handlers synchronous: routing records the selected edge on the call itself, and moving them onto asyncio.to_thread would break that.

With a custom BaseChatModel

from langchain_anthropic import ChatAnthropic
from launchdarkly_ai_langchain_agents import create_langchain_agents_handler

handler = create_langchain_agents_handler(ChatAnthropic(model="claude-opus-4-5"))

A constructed instance cannot see flag parameters. Pass a function instead if the model should be built after evaluation:

handler = create_langchain_agents_handler(
    lambda config: ChatAnthropic(
        **{
            **(config["model"].get("parameters") or {}),
            "model": config["model"]["name"],
        }
    )
)

Convenience wrapper

import asyncio
from launchdarkly_ai_langchain_agents import langchain_agents

async def main():
    user_input = "Research feature flagging best practices"
    result = await langchain_agents(
        user_input,
        {"kind": "user", "key": "user-123"},
        {"key": "my-ai-config-flag"},
        variables={"user_input": user_input},
    )
    print(result.response)

asyncio.run(main())

Agent graphs — langchain_graph()

Runs a LaunchDarkly agent graph with the LangChain agent handler pre-bound. Equivalent to calling the base graph() with handlers=[create_langchain_agents_handler()]. See the core client docs for the full graph() API.

import asyncio
from launchdarkly_ai_langchain_agents import langchain_graph

async def main():
    result = await langchain_graph("support-graph").invoke(
        "I was double charged",
        {"kind": "user", "key": "user-123"},
    )
    print(result["response"])

asyncio.run(main())

Native graph adapter — to_lang_graph()

Converts a resolve_graph() result into a framework-native LangGraph StateGraph. Pre-order traversal (root → leaves) builds a compiled StateGraph. Single-child edges become direct edges after a tool loop; multi-child edges use Command-returning handoff tools (bound with parallel_tool_calls=False) so the model picks exactly one target.

import asyncio
from launchdarkly_ai_server import resolve_graph
from launchdarkly_ai_langchain_agents import to_lang_graph

async def main():
    ctx = {"kind": "user", "key": "user-123"}
    result = await to_lang_graph(
        resolve_graph("support-graph", context=ctx),
        {
            "tool_handlers": registry.tools,
            "context": ctx,
            # optional: supply your own model per node
            "model_factory": lambda node: ChatOpenAI(model=node["config"]["model"]["name"]),
        },
    ).invoke("I was double charged")
    print(result["response"])

asyncio.run(main())

How It Works

  • Uses the system prompt and conversation history defined in your LaunchDarkly flag config.
  • Template placeholders ({{variable}}) in the prompt are substituted using variables before the call.
  • The LangGraph ReAct agent manages the full reasoning and tool-call loop autonomously — reasoning through steps, calling tools, and deciding when to stop.
  • Emits an OTel span and LaunchDarkly telemetry for every call.

Choosing Between langchain-agents and langchain-messages

langchain-agents langchain-messages
Orchestration LangGraph StateGraph Manual tool loop
Reasoning style ReAct (reason + act cycles) Single invoke per tool round-trip
Best for Complex multi-step reasoning Straightforward tool calls

Environment Variables

Variable Description
OPENAI_API_KEY Required when using the default ChatOpenAI model
LD_SDK_KEY LaunchDarkly server-side SDK key
LD_SERVICE_NAME OTel service.name resource attribute (default: python-sdk)
LD_ENVIRONMENT deployment.environment attribute attached to telemetry
OTEL_EXPORTER_OTLP_ENDPOINT OTLP endpoint override (default: LaunchDarkly Observability backend)

Metadata

Release files for launchdarkly-ai-langchain-agents 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for launchdarkly-ai-langchain-agents 0.2.6
File Size Uploaded Provenance Actions
launchdarkly_ai_langchain_agents-0.2.6.tar.gz 54.7 kB Without provenance Details for launchdarkly_ai_langchain_agents-0.2.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for launchdarkly-ai-langchain-agents 0.2.6
File Interpreter ABI Platform Actions
launchdarkly_ai_langchain_agents-0.2.6-py3-none-any.whl Python 3 none any Details for launchdarkly_ai_langchain_agents-0.2.6-py3-none-any.whl

Total release size: 79.0 kB

Release files / launchdarkly_ai_langchain_agents-0.2.6.tar.gz

Download URL launchdarkly_ai_langchain_agents-0.2.6.tar.gz
Size 54.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c8c7436b9ef45f73497094ca1bc94e0c90b8d85027b8b24c119c76904cc409fd
BLAKE2b-256 checksum
How to use checksums
f153b00ce197add94147a382790db2348c7326eee58633f56c0242760c77db56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / launchdarkly_ai_langchain_agents-0.2.6-py3-none-any.whl

Download URL launchdarkly_ai_langchain_agents-0.2.6-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea504b3500136d15052265887819d73d5d00094d9092d4bdc533af275c0b6500
BLAKE2b-256 checksum
How to use checksums
2b51d50059f2dda0bdc5db5eb965852941c9bed93ba99696b7ff2cf01c06455d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for launchdarkly-ai-langchain-agents 0.2.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page