This guide shows how to connect a Google Gemini-powered application to LaunchDarkly AgentControl. Gemini offers native multimodal capabilities, long context windows, and competitive pricing with Gemini Flash. By the end, you will be able to manage your model configuration and prompts outside of your application code, and track metrics automatically.
AgentControl supports two modes:
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. 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 completion mode vs agent mode.
This guide provides examples in both Python and Node.js (TypeScript).
If you’re new to AgentControl, start with the Quickstart and return to this guide when you are ready for a more detailed example.
To learn more about AgentControl-specific SDKs, read AI SDKs. For Python-specific details, read the Python AI SDK reference.
To complete this guide, you need the following:
Before you begin, review these key concepts.
An AgentControl config is a LaunchDarkly resource that controls how your application uses large language models. Each AgentControl config contains one or more variations. Each variation specifies:
You can update these settings in LaunchDarkly at any time without changing your application code.
A context represents the end user interacting with your application. LaunchDarkly uses context attributes to:
{{ ldctx.* }} placeholders in your prompts with context attribute valuesOther placeholders, such as {{ topic }}, are populated from the variables argument you pass at runtime.
When you retrieve an AgentControl config, call createTracker() (Node.js) or create_tracker() (Python) on the returned object to mint a tracker. The tracker records metrics from your Gemini calls, including:
These metrics appear on the AI Insights dashboard in LaunchDarkly.
Install the LaunchDarkly AI SDK and the Google GenAI SDK in your application. AgentControl is supported by LaunchDarkly server-side SDKs only. The Node.js examples in this guide use the server-side Node.js AI SDK.
Here is how to install the required packages:
Create a .env file in your project root to store your API keys:
Add .env to your .gitignore to keep credentials out of version control.
Initialize both the LaunchDarkly client and the Google GenAI client. Store your API keys in environment variables.
Here is the initialization code:
Create an AgentControl config in the LaunchDarkly UI to store your Gemini model settings and prompts.
If you have the LaunchDarkly MCP server or agent skills configured, prompt your coding assistant to create the AgentControl config for you. For example:
“Create a completion mode AgentControl config called ‘Gemini assistant’ with a ‘Gemini 2.0 Flash’ variation using the gemini-2.0-flash model, temperature 0.7, max_tokens 1024, and the system message: ‘You are a helpful assistant. Answer questions about {{topic}}.’ Enable targeting.”
To create the AgentControl config:
To create a variation:
gemini-2.0-flash Gemini model.temperature to 0.7 and max_tokens to 1024.
To enable targeting:

Retrieve the AgentControl config from LaunchDarkly by calling the completion config function. Pass a context that represents the current user and a fallback configuration.
Here is how to get the AgentControl config:
The SDK uses the fallback configuration when LaunchDarkly is unreachable. Check the enabled property and handle the disabled case in your application.
For production use:
Google’s GenAI SDK treats system_instruction as a top-level config field on GenerateContentConfig, separate from the contents array. Define a helper to split LD messages into a (system_instruction, contents) tuple: system messages are concatenated into a single string, user messages become role: "user" content items, and assistant messages become role: "model" content items. Append the user’s question to the contents list, then call generate_content directly instead of creating a chat session.
Gemini’s parameter names differ slightly from the LaunchDarkly AgentControl config field names. The max_tokens parameter stored in a variation becomes max_output_tokens in the Gemini SDK. Define a mapping helper to handle this rename, then define a converter function that translates a Gemini response into an LDAIMetrics object. Pass the converter to the tracker’s generic wrapper, which handles duration, success, and error tracking automatically:
Then call Gemini with the config. Use map_to_google_ai_messages / mapToGoogleAIMessages to split the LD messages into a system instruction and a contents array, append the user’s question, then call generate_content / generateContent directly through the tracker wrapper:
Agent-mode AgentControl configs return a single instructions string instead of a message list, and they let you attach reusable tools from the LaunchDarkly tools library. With Gemini, the instructions map to the system_instruction field on GenerateContentConfig and tools pass through as a functionDeclarations array.
First, define the tool in LaunchDarkly so the AgentControl config variation can reference it:
get_order_status as the Key.

gemini-2.0-flash.max_tokens to 1024.get_order_status.
To learn more about managing tools, read Tools in AgentControl.
Use agent_config() instead of completion_config(). The SDK returns the attached tools under parameters.tools in OpenAI’s type=function shape, so convert them to Gemini’s {functionDeclarations: [{name, description, parameters}]} shape and pass them on the tools field of GenerateContentConfig. Tool handler functions stay in your application code — LaunchDarkly stores the schema, your application owns the behavior.
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 completion mode vs agent mode and Agents in AgentControl.
Use the LaunchDarkly UI to monitor how your applications are performing across all AgentControl configs and for individual configs.
To view aggregated metrics across all your AgentControl 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 AI Insights.
To view metrics for a specific AgentControl config:
The dashboard displays the following metrics:
Metrics update approximately every minute. Use these metrics to compare variations and optimize your prompts. To learn more, read Monitor AgentControl configs.

The AI SDKs emit OpenTelemetry-compatible spans for each generation call. You can forward these spans to your existing observability stack for deeper analysis. To learn more, read Observability and LLM observability.
Close the LaunchDarkly client when your application shuts down to flush pending events.
Here is how to close the client:
Always flush events before closing. Trailing events are at risk of being lost otherwise, in short-lived scripts and long-running services alike.
Here is how to flush events:
Here is a complete working example that combines all the steps.
After you have the basic integration working, you can extend it with:
For more AgentControl guides, read the other guides in the AgentControl guides section.
If you are experiencing problems with your configuration, this section lists common errors and solutions.
If metrics do not appear on the AI Insights dashboard:
tracker.track_metrics_of() (Python) or tracker.trackMetricsOf() (Node.js) with the correct metrics converter.flush() before closing the client, especially for short-lived scripts.If the LaunchDarkly SDK fails to initialize:
If you always receive the fallback configuration:
If you receive Google API errors:
In this guide, you connected a Google Gemini-powered application to LaunchDarkly AgentControl. You can now: