> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Getting started with LangChain and AI Configs

> This guide shows how to use LaunchDarkly AI Configs with the official LangChain provider to manage model configuration, prompts, and metrics for a single-turn chat model interaction.

> For the complete documentation index, see [llms.txt](/llms.txt). For the full documentation snapshot, see [llms-full.txt](/llms-full.txt).

## Overview

This guide shows how to use a LangChain chat model with LaunchDarkly AI Configs. LangChain is a framework for building LLM-powered applications that abstracts away provider differences — choose it when you want to swap between OpenAI, Anthropic, Gemini, and other providers without changing your application code. By the end, you will have a working integration that retrieves model configuration and prompts from LaunchDarkly, invokes a LangChain model, and reports metrics back automatically.

This guide uses the official LaunchDarkly LangChain provider packages, which handle model creation, provider mapping, and metrics extraction. This guide uses the low-level model-creation flow so you can see how the pieces fit together. For higher-level helper methods such as direct provider creation and structured-output support, read the [Python](https://github.com/launchdarkly/python-server-sdk-ai/tree/main/packages/ai-providers/server-ai-langchain) or [Node.js](https://github.com/launchdarkly/js-core/tree/main/packages/ai-providers/server-ai-langchain) LangChain provider package documentation.AI Configs support two modes:

* **Completion mode** returns messages and roles (such as "system," "user," "assistant"). Use it for chat-style interactions and message-oriented workflows. Completion mode supports online evaluations with judges attached in the LaunchDarkly user interface (UI).
* **Agent mode** returns a single `instructions` string. Use it when your runtime or framework expects a goal/instructions input for a structured workflow. Agent mode changes the configuration shape from messages to instructions. Your application maps these instructions into your provider or framework's native input. Both modes support tool calling. LangChain's provider abstraction works with both modes, letting you swap models without changing your application code. This guide walks through completion mode as the main path, with an optional agent config section. To learn more about when to use each mode, read [When to use prompt-based vs agent mode](/guides/ai-configs/agent-vs-completion). This guide provides examples in both Python and Node.js (TypeScript).

<Tip title="Additional resources for AI Configs">
  If you are not familiar with AI Configs, start with the [Quickstart for AI Configs](/home/ai-configs/quickstart) and return to this guide when you are ready for a LangChain-specific example.

  You can find reference guides for each of the AI SDKs at [AI SDKs](/sdk/ai).
</Tip>

## Prerequisites

To complete this guide, you need the following:

* A [LaunchDarkly account](https://app.launchdarkly.com/).
* An API key for your chosen model provider (OpenAI, Anthropic, or another supported provider).
* A development environment:
  * **Python**: Python 3.10 or higher
  * **Node.js**: Node.js 20 or higher
* The LangChain integration package for your model provider installed locally. For example, `langchain-openai` for OpenAI models, `langchain-anthropic` for Anthropic models, or `langchain-google-genai` for Gemini. LangChain raises an `ImportError` if the provider package is not installed when the model is created.
* Familiarity with LaunchDarkly contexts. To learn more, read [Contexts and segments](/home/flags/contexts).

## Concepts

Before you begin, review these key concepts.

### AI Configs

An AI Config is a LaunchDarkly resource that controls how your application uses large language models. Each AI Config contains one or more variations. Each variation specifies:

* A model configuration, including the model name and parameters
* Messages that define the promptYou can update these settings in LaunchDarkly at any time without changing your application code.

### The Launch

Darkly LangChain providerLaunchDarkly publishes official LangChain provider packages for [Python](https://github.com/launchdarkly/python-server-sdk-ai/tree/main/packages/ai-providers/server-ai-langchain) and [Node.js](https://github.com/launchdarkly/js-core/tree/main/packages/ai-providers/server-ai-langchain). These packages handle model creation, message conversion, and metrics extraction so you do not need to write custom integration code.

The provider creates a LangChain chat model directly from an AI Config, passing through all model parameters and mapping provider names automatically. It also extracts token usage from LangChain responses for tracking in LaunchDarkly.

### Contexts

A context represents the end user interacting with your application. LaunchDarkly uses context attributes to:

* Determine which variation to serve based on targeting rules
* Populate template variables in your prompts

### The tracker

When you retrieve an AI Config, the SDK returns a config object with a `tracker` property. The tracker records metrics from your LangChain calls, including:

* Generation count
* Input and output tokens
* Latency
* Success and error ratesThese metrics appear on the **Monitoring** tab in LaunchDarkly.

## Step 1: Install the SDKInstall the Launch

Darkly AI SDK and the official LangChain provider package. AI Configs are supported by LaunchDarkly server-side SDKs only. The Node.js examples in this guide use the [server-side Node.js AI SDK](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/server-ai).

Here is how to install the required packages:

<CodeGroup>
  ```bash title="Python" theme={null}
      pip install launchdarkly-server-sdk
      pip install launchdarkly-server-sdk-ai
      pip install launchdarkly-server-sdk-ai-langchain
      pip install langchain
  ```

  ```bash title="Node.js" theme={null}
      npm install @launchdarkly/node-server-sdk
      npm install @launchdarkly/server-sdk-ai
      npm install @launchdarkly/server-sdk-ai-langchain
      npm install langchain @langchain/core
      npm install dotenv
  ```
</CodeGroup>

Then install the LangChain integration package for your model provider:

<CodeGroup>
  ```bash title="Python" theme={null}
      # Install one or more provider packages:
      pip install langchain-openai       # OpenAI models
      pip install langchain-anthropic    # Anthropic models
      pip install langchain-google-genai # Google Gemini models
  ```

  ```bash title="Node.js" theme={null}
      # Install one or more provider packages:
      npm install @langchain/openai       # OpenAI models
      npm install @langchain/anthropic    # Anthropic models
      npm install @langchain/google-genai # Google Gemini models
  ```
</CodeGroup>

If your AI Config targets a provider whose LangChain package is not installed, the model creation step raises an error at runtime.

Create a `.env` file in your project root with your credentials:

```bash theme={null}
# .env
LAUNCHDARKLY_SDK_KEY=<your-launchdarkly-sdk-key>
OPENAI_API_KEY=<your-openai-api-key>
```

## Step 2: Initialize the clients

Initialize the LaunchDarkly client and the AI client. Store your SDK key in an environment variable.

Here is the initialization code:

<CodeGroup>
  ```python title="Python" theme={null}
      import os
      import asyncio
      import ldclient
      from ldclient import Context
      from ldclient.config import Config
      from ldai.client import LDAIClient
      from ldai_langchain import (
          create_langchain_model,
          convert_messages_to_langchain,
          get_ai_metrics_from_response,
      )
      from langchain_core.messages import HumanMessage

      # Initialize LaunchDarkly
      sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY')
      ldclient.set_config(Config(sdk_key))

      if not ldclient.get().is_initialized():
          print("LaunchDarkly SDK failed to initialize")
          exit(1)

      # Initialize the AI client
      aiclient = LDAIClient(ldclient.get())
  ```

  ```typescript title="Node.js (TypeScript)" theme={null}
      import dotenv from 'dotenv';
      dotenv.config();

      import * as LaunchDarkly from '@launchdarkly/node-server-sdk';
      import { initAi } from '@launchdarkly/server-sdk-ai';
      import { LangChainProvider } from '@launchdarkly/server-sdk-ai-langchain';

      // Initialize LaunchDarkly
      const ldClient = LaunchDarkly.init(process.env.LAUNCHDARKLY_SDK_KEY as string);

      try {
        await ldClient.waitForInitialization({ timeout: 10 });
      } catch (error) {
        console.error('LaunchDarkly SDK failed to initialize');
        process.exit(1);
      }

      // Initialize the AI client
      const aiClient = initAi(ldClient);
  ```
</CodeGroup>

Replace `LAUNCHDARKLY_SDK_KEY` with your LaunchDarkly SDK key. You can find your SDK key from the **Environments** list for your LaunchDarkly project. To learn how, read [SDK credentials](/home/account/environment/keys#sdk-credentials).

## Step 3: Create an AI Config in Launch

DarklyCreate an AI Config to store your model settings and prompts. You can do this through the LaunchDarkly UI, or programmatically using the [LaunchDarkly MCP server](/home/getting-started/mcp) from an AI coding assistant such as Claude Code or Cursor.

<Tip title="Using the MCP server or agent skills">
  If you have the [LaunchDarkly MCP server](https://github.com/launchdarkly/mcp-server) or [agent skills](https://github.com/launchdarkly/agent-skills) configured, prompt your coding assistant to create the AI Config for you. For example:

  "Create a completion mode AI Config called 'LangChain assistant' with a GPT-4o variation that has a system message: 'You are a helpful assistant. Provide clear, concise answers.' Set temperature to 0.7 and enable targeting."
</Tip>

To create the AI Config in the UI:

1. Click **Create** and select **AI Config**.

2. In the **Create AI Config** dialog, select **Completion**.

3. Enter a name, such as "LangChain assistant".

4. Click **Create**.To create a variation:

5. On the **Variations** tab, replace "Untitled variation" with a name, such as "GPT-4o variation".

6. Click **Select a model** and choose a model, such as "gpt-4o" from OpenAI.

7. Click **Parameters** to configure model parameters such as temperature and max tokens.

8. Add a **system** message to define your assistant's behavior. Here is an example:

<CodeGroup>
  ```title="System message" theme={null}
      You are a helpful assistant. Provide clear, concise answers.
  ```
</CodeGroup>

5. Click **Review and save**.

<Frame caption="A completed variation with model configuration and system message.">
  ![A completed variation with model configuration and system message.](https://files.buildwithfern.com/https://launchdarkly.docs.buildwithfern.com/docs/ba733ae36d048e2d1fb3fbd9e30b44fa1b85790e910cdd5440f0d411e5fb5a09/assets/images/auto/ai-config-variation-complete.auto.png)
</Frame>

To enable targeting:

1. Select the **Targeting** tab.
2. In the "Default rule" section, click **Edit**.
3. Set the default rule to serve your variation.
4. Click **Review and save**.

<Frame caption="The default targeting rule configured to serve a variation.">
  ![The default targeting rule configured to serve a variation.](https://files.buildwithfern.com/https://launchdarkly.docs.buildwithfern.com/docs/5dd50b9ee0b404e60eae3ce10489d1a1048069ffabe139d33b0c655a9da56ec2/assets/images/auto/guide-ai-config-model-config-update-default-targeting-rule.auto.png)
</Frame>

## Step 4: Get the AI Config in your application

Retrieve the AI Config from LaunchDarkly by calling the completion config function. Pass a context that represents the current user.

Here is how to get the AI Config:

<CodeGroup>
  ```python title="Python" theme={null}
      # Define the context for the current user
      context = (
          Context
          .builder('example-user-key')
          .kind('user')
          .name('Sandy')
          .build()
      )

      # Get the AI Config
      config_value = aiclient.completion_config(
          'langchain-assistant',
          context,
          variables={'myVariable': 'value'}
      )
      tracker = config_value.tracker
  ```

  ```typescript title="Node.js (TypeScript)" theme={null}
      // Define the context for the current user
      const context: LaunchDarkly.LDContext = {
        kind: 'user',
        key: 'example-user-key',
        name: 'Sandy',
      };

      // Define a fallback configuration
      const fallbackConfig = { enabled: false };

      // Get the AI Config
      const config = await aiClient.completionConfig(
        'langchain-assistant',
        context,
        fallbackConfig,
        { myVariable: 'value' }
      );
  ```
</CodeGroup>

In Node.js, the SDK uses the provided fallback configuration when LaunchDarkly is unreachable. In Python, the `default` parameter is optional; if omitted, the SDK returns a disabled config as the fallback. Check the `enabled` property and handle the disabled case in your application.

<Note title="Best practices">
  For production use:

  * Retrieve the AI Config each time you generate content so LaunchDarkly can evaluate the latest targeting rules and prompt changes.
  * Provide a fallback configuration when possible so your application can fail gracefully if LaunchDarkly is unavailable.
  * Avoid sending personally identifiable information in contexts unless you have a specific need and an approved handling pattern. To learn more, read [Privacy in AI Configs](/home/ai-configs/privacy).
</Note>

## Step 5: Create a Lang

Chain model from the AI ConfigUse the LaunchDarkly LangChain provider to create a chat model from the AI Config. The provider passes all model parameters from the variation (temperature, max tokens, and others) through to LangChain, and maps provider names automatically.

Here is how to create the model:

<CodeGroup>
  ```python title="Python" theme={null}
      if config_value.enabled:
          # Creates a LangChain BaseChatModel with all parameters from the AI Config
          llm = create_langchain_model(config_value)
  ```

  ```typescript title="Node.js (TypeScript)" theme={null}
      if (config.enabled) {
        // Creates a LangChain BaseChatModel with all parameters from the AI Config
        const llm = await LangChainProvider.createLangChainModel(config);
      }
  ```
</CodeGroup>

The provider maps LaunchDarkly provider names to LangChain equivalents. Both Python and Node.js map "Gemini" to `google-genai`. The Python provider additionally maps "Bedrock" variants (such as "Bedrock:Anthropic") to `bedrock_converse` and passes the sub-provider as a parameter. Other provider names pass through as-is in both languages.

## Step 6: Call the model and track metrics

Combine the messages from your AI Config with user input, then use the tracker to call the model and record metrics automatically.

Here is how to make the API call:

<CodeGroup>
  ```python title="Python" theme={null}
      from langchain_core.messages import HumanMessage

      async def run_completion():
          if not config_value.enabled:
              print("AI Config is disabled")
              return

          # Convert AI Config messages to LangChain format and add user input
          messages = convert_messages_to_langchain(config_value.messages or [])
          messages.append(HumanMessage(content='What can you help me with?'))

          # Track the completion with LaunchDarkly metrics
          completion = await tracker.track_metrics_of_async(
              lambda: llm.ainvoke(messages),
              get_ai_metrics_from_response,
          )

          print(completion.content)

      asyncio.run(run_completion())
  ```

  ```typescript title="Node.js (TypeScript)" theme={null}
      if (config.enabled) {
        const { tracker } = config;

        // Convert messages to LangChain format and add user input
        const langchainMessages = LangChainProvider.convertMessagesToLangChain(
          config.messages || []
        );
        langchainMessages.push(
          LangChainProvider.convertMessagesToLangChain([
            { role: 'user', content: 'What can you help me with?' }
          ])[0]
        );

        // Track the completion with LaunchDarkly metrics
        const completion = await tracker.trackMetricsOf(
          LangChainProvider.getAIMetricsFromResponse,
          () => llm.invoke(langchainMessages),
        );

        const content = typeof completion.content === 'string'
          ? completion.content
          : JSON.stringify(completion.content);
        console.log(content);
      } else {
        console.log('AI Config is disabled');
      }
  ```
</CodeGroup>

The tracker wraps the model call and automatically records duration, token usage, and success or error status.

## Step 7: Use template variables

Use template variables in your AI Config messages to customize prompts at runtime. Variables use double curly brace syntax: `{{ variable_name }}`.To use context attributes, prefix with `ldctx`:

```
Address the user as {{ ldctx.name }}.
```

To use custom variables, pass them when calling the completion config function.

Here is an example AI Config message with variables:

<CodeGroup>
  ```title="AI Config message in LaunchDarkly" theme={null}
      You are an expert assistant for {{ topic }}. Provide detailed answers with examples.
  ```
</CodeGroup>

Here is how to pass variables in your application code:

<CodeGroup>
  ```python title="Python" theme={null}
      config_value = aiclient.completion_config(
          'langchain-assistant',
          context,
          variables={
              'topic': 'machine learning'
          }
      )
  ```

  ```typescript title="Node.js (TypeScript)" theme={null}
      const config = await aiClient.completionConfig(
        'langchain-assistant',
        context,
        fallbackConfig,
        { topic: 'machine learning' }
      );
  ```
</CodeGroup>

The SDK replaces variables with their values before returning the configuration.

## Step 8: Monitor your AI Config

View metrics for your AI Configs in the LaunchDarkly UI.To view aggregated metrics across all your AI Configs, navigate to **Insights** in the left navigation under the **AI** section. The Insights overview page displays cost, latency, error rate, invocation counts, and model distribution across your organization. To learn more, read [Trends Explorer](/home/ai-configs/trends).

To view metrics for a specific AI Config:

1. Navigate to your AI Config.
2. Select the **Monitoring** tab.

The **Monitoring** tab displays:

* **Generation count**: number of successful model invocations
* **Token usage**: input and output tokens per variation
* **Time to generate**: average latency per generation
* **Error rate**: percentage of failed invocations
* **Costs**: estimated spend based on token usage and model pricingMetrics update approximately every minute. Use these metrics to compare variations and optimize your prompts. To learn more, read [Monitor AI Configs](/home/ai-configs/monitor). To run statistical comparisons between variations, read [Run experiments with AI Configs](/home/ai-configs/experimentation). To score response quality automatically using judges, read [Online evaluations in AI Configs](/home/ai-configs/online-evaluations).

<Note title="Observability">
  LaunchDarkly provides metrics for AI Config invocations, including latency, token usage, costs, and error rates. If you also want traces associated with an evaluated AI Config, run the model request inside an active OpenTelemetry parent span. To learn more, read [Observability](/home/observability) and [LLM observability](/home/observability/llm-observability).
</Note>

## Optional: Retrieve an agent-based AI Config

Agent mode returns a single `instructions` string instead of a messages array. Use it when your runtime or framework expects a goal/instructions input for a structured workflow. Agent mode changes the configuration shape from messages to instructions. Your application maps these instructions into your provider or framework's native input.

To use agent mode, create an AI Config with mode set to **Agent** in the LaunchDarkly UI, then retrieve it with the agent config function. Map the LaunchDarkly `instructions` field into LangChain's message abstraction.

<CodeGroup>
  ```python title="Python" theme={null}
      from langchain_core.messages import SystemMessage

      agent = aiclient.agent_config(
          'langchain-agent',
          context,
          variables={'topic': 'Python'}
      )

      if agent.enabled:
          llm = create_langchain_model(agent)

          # Map instructions into LangChain's SystemMessage
          messages = [
              SystemMessage(content=agent.instructions),
              HumanMessage(content='Triage this support request: my order has not arrived'),
          ]

          tracker = agent.tracker
          completion = await tracker.track_metrics_of_async(
              lambda: llm.ainvoke(messages),
              get_ai_metrics_from_response
          )
          print(completion.content)
  ```

  ```typescript title="Node.js (TypeScript)" theme={null}
      import { type LDAIAgentConfig } from '@launchdarkly/server-sdk-ai';

      const agentConfig: LDAIAgentConfig = await aiClient.agentConfig(
        'langchain-agent',
        context,
        { enabled: false },
        { topic: 'Python' }
      );

      if (agentConfig.enabled && agentConfig.tracker && agentConfig.instructions) {
        const { tracker } = agentConfig;
        const llm = await LangChainProvider.createLangChainModel(agentConfig);

        // Map instructions into LangChain's message format
        const messages = LangChainProvider.convertMessagesToLangChain([
          { role: 'system', content: agentConfig.instructions },
          { role: 'user', content: 'Triage this support request: my order has not arrived' },
        ]);

        const completion = await tracker.trackMetricsOf(
          LangChainProvider.getAIMetricsFromResponse,
          () => llm.invoke(messages),
        );

        const content = typeof completion.content === 'string'
          ? completion.content
          : JSON.stringify(completion.content);
        console.log(content);
      }
  ```
</CodeGroup>

In completion mode, you can attach judges to variations in the LaunchDarkly UI for automatic evaluation. In agent mode, invoke judges programmatically through the AI SDK.To learn more, read [When to use prompt-based vs agent mode](/guides/ai-configs/agent-vs-completion) and [Agents in AI Configs](/home/ai-configs/agents). For multi-agent workflows with LangGraph, read [Compare AI orchestrators](/tutorials/ai-orchestrators).

## Step 9: Close the client

Close the LaunchDarkly client when your application shuts down to flush pending events.

Here is how to close the client:

<CodeGroup>
  ```python title="Python" theme={null}
      ldclient.get().flush()
      ldclient.get().close()
  ```

  ```typescript title="Node.js (TypeScript)" theme={null}
      await ldClient.flush();
      await ldClient.close();
  ```
</CodeGroup>

For short-lived applications such as scripts, explicitly flush events before closing.

## Complete example

Here is a complete working example that combines all the steps.

<CodeGroup>
  ```python title="Python" theme={null}
      import os
      import asyncio
      import ldclient
      from ldclient import Context
      from ldclient.config import Config
      from ldai.client import LDAIClient
      from ldai_langchain import (
          create_langchain_model,
          convert_messages_to_langchain,
          get_ai_metrics_from_response,
      )
      from langchain_core.messages import HumanMessage

      # Set sdk_key to your LaunchDarkly SDK key.
      sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY')

      # Set config_key to the AI Config key you want to evaluate.
      ai_config_key = os.getenv('LAUNCHDARKLY_AI_CONFIG_KEY', 'sample-ai-config')

      async def async_main():
          if not sdk_key:
              print("*** Please set the LAUNCHDARKLY_SDK_KEY env first")
              exit()

          ldclient.set_config(Config(sdk_key))
          if not ldclient.get().is_initialized():
              print("*** SDK failed to initialize.")
              exit()

          aiclient = LDAIClient(ldclient.get())
          print("*** SDK successfully initialized")

          # Set up the evaluation context.
          context = (
              Context
              .builder('example-user-key')
              .kind('user')
              .name('Sandy')
              .build()
          )

          config_value = aiclient.completion_config(
              ai_config_key,
              context,
              variables={'myUserVariable': "Testing Variable"}
          )
          tracker = config_value.tracker

          if not config_value.enabled:
              print("AI Config is disabled")
              return

          try:
              # Create LangChain model from AI Config
              print("Model:", config_value.model.name,
                    "Provider:", config_value.provider.name)
              llm = create_langchain_model(config_value)

              # Convert messages and add user input
              messages = convert_messages_to_langchain(
                  config_value.messages or []
              )
              USER_INPUT = "What can you help me with?"
              print("User Input:\n", USER_INPUT)
              messages.append(HumanMessage(content=USER_INPUT))

              # Track the LangChain completion with LaunchDarkly metrics
              completion = await tracker.track_metrics_of_async(
                  lambda: llm.ainvoke(messages),
                  get_ai_metrics_from_response,
              )
              ai_response = completion.content
              print("AI Response:\n", ai_response)
              print("Success.")

          except Exception as e:
              print(f"Error during completion: {e}")

          # Flush pending events and close the connection.
          ldclient.get().flush()
          ldclient.get().close()

      def main():
          asyncio.run(async_main())

      if __name__ == "__main__":
          main()
  ```

  ```typescript title="Node.js (TypeScript)" theme={null}
      import dotenv from 'dotenv';
      dotenv.config();

      import * as LaunchDarkly from '@launchdarkly/node-server-sdk';
      import { initAi, type LDAICompletionConfig } from '@launchdarkly/server-sdk-ai';
      import { LangChainProvider } from '@launchdarkly/server-sdk-ai-langchain';

      async function main(): Promise<void> {
        const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY;
        if (!sdkKey) {
          console.error('Please set the LAUNCHDARKLY_SDK_KEY env first');
          return;
        }

        // Initialize LaunchDarkly client
        const ldClient = LaunchDarkly.init(sdkKey);

        try {
          await ldClient.waitForInitialization({ timeout: 10 });
          console.log('LaunchDarkly SDK initialized');
        } catch (error) {
          console.error('LaunchDarkly SDK failed to initialize');
          return;
        }

        // Initialize AI client
        const aiClient = initAi(ldClient);

        // Define context
        const context: LaunchDarkly.LDContext = {
          kind: 'user',
          key: 'example-user-key',
          name: 'Sandy',
        };

        // Get AI Config
        const fallbackConfig = { enabled: false };
        const config: LDAICompletionConfig = await aiClient.completionConfig(
          'langchain-assistant',
          context,
          fallbackConfig,
          { myUserVariable: 'Testing Variable' }
        );

        if (!config.enabled || !config.tracker) {
          console.log('AI Config is disabled');
          await ldClient.close();
          return;
        }

        console.log(`Model: ${config.model?.name}`);

        const { tracker } = config;

        // Create LangChain model from AI Config
        const llm = await LangChainProvider.createLangChainModel(config);

        // Convert messages to LangChain format and add user input
        const langchainMessages = LangChainProvider.convertMessagesToLangChain(
          config.messages || []
        );
        langchainMessages.push(
          LangChainProvider.convertMessagesToLangChain([
            { role: 'user', content: 'What can you help me with?' }
          ])[0]
        );

        try {
          // Track the completion with LaunchDarkly metrics
          const completion = await tracker.trackMetricsOf(
            LangChainProvider.getAIMetricsFromResponse,
            () => llm.invoke(langchainMessages),
          );

          const content = typeof completion.content === 'string'
            ? completion.content
            : JSON.stringify(completion.content);
          console.log('AI Response:', content);
          console.log('Success.');
        } catch (error) {
          console.error('Error during completion:', error);
        }

        // Close client
        await ldClient.flush();
        await ldClient.close();
      }

      main().catch(console.error);
  ```
</CodeGroup>

## What to explore next

After you have the basic integration working, you can extend it with:

* [Tools](/home/ai-configs/tools-library) for calling external functions from your workflows
* [Online evaluations](/home/ai-configs/online-evaluations) to score response quality automatically
* [Experiments](/home/ai-configs/experimentation) to compare AI Config variations statistically
* [Agents](/home/ai-configs/agents) for multi-step workflows with LangChain or LangGraphFor structured outputs and higher-level helper methods, read the [Python](https://github.com/launchdarkly/python-server-sdk-ai/tree/main/packages/ai-providers/server-ai-langchain) or [Node.js](https://github.com/launchdarkly/js-core/tree/main/packages/ai-providers/server-ai-langchain) LangChain provider package documentation.

## Troubleshooting

Some solutions for common problems are outlined below.

### Provider package not installed

If you receive `ImportError` (Python) or `Error` (Node.js) when creating the model, verify that the LangChain integration package for your provider is installed. For example, if your AI Config uses an OpenAI model, install `langchain-openai` (Python) or `@langchain/openai` (Node.js). The LaunchDarkly LangChain provider creates models using LangChain's `init_chat_model`, which requires the correct provider package at runtime.

### Metrics not appearing

If metrics do not appear on the **Monitoring** tab:

* Verify that you are calling `tracker.track_metrics_of_async()` (Python) or `tracker.trackMetricsOf()` (Node.js) with the LangChain metrics extractor.
* Ensure you call `flush()` before closing the client, especially for short-lived scripts.
* Wait at least one minute for metrics to process.

### SDK initialization failures

If the LaunchDarkly SDK fails to initialize:

* Verify your SDK key is correct and matches the environment you are targeting.
* Check that your network can reach LaunchDarkly servers.
* Review the SDK logs for specific error messages.

### Config returns fallback value

If you always receive the fallback configuration:

* Verify targeting is enabled for your AI Config.
* Check that the AI Config key in your code matches the key in LaunchDarkly.
* Ensure your context matches the targeting rules.

### Package import errors

If you receive `ModuleNotFoundError` (Python) or `Cannot find module` (Node.js) for the LaunchDarkly packages:

* Verify the LangChain provider package is installed. The Python package is `launchdarkly-server-sdk-ai-langchain` (imported as `ldai_langchain`). The Node.js package is `@launchdarkly/server-sdk-ai-langchain`.
* Ensure you are using compatible current versions of `launchdarkly-server-sdk-ai`, `launchdarkly-server-sdk-ai-langchain`, and your LangChain provider package. Check the package documentation or release notes if you encounter import or runtime errors.

## Conclusion

In this guide, you connected a LangChain chat model to LaunchDarkly AI Configs using the official LangChain provider packages. You can now:

* Manage prompts and model settings in LaunchDarkly without code changes
* Track token usage, latency, and success rates automatically
* Use template variables to customize prompts per user
