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

# Manage agent configs as code and gate prompt changes in CI

> This tutorial explains how to use agent configs as code to synchronize LaunchDarkly prompt variations and tools with a Git repository using ldcli sync, enforce code ownership in the LaunchDarkly UI, and gate every prompt change in GitHub Actions with offline evaluations before the change reaches LaunchDarkly.

**Developer**

**Federal docs**

**EU docs**

*Published November 23, 2026*

Portrait of Scarlett Attensil.

by Scarlett Attensil

This tutorial explains how to use agent configs as code to do the following:

* Synchronize LaunchDarkly prompt variations and tools with a Git repository using `ldcli sync`.
* Enforce code ownership in the LaunchDarkly UI.
* Gate every prompt change in GitHub Actions with offline evaluations before the change reaches LaunchDarkly.

<video width="100%" controls>
  <source src="https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/configs-in-code-walkthrough.mp4?fit=max&auto=format&n=_JmedtXPej1UIC-K&q=85&s=95c57f421eb19ca93da534598131c4f6" type="video/mp4" />

  Full walkthrough of managing LaunchDarkly agent configs as code in Git using ldcli sync and gating prompt changes in CI.
</video>

Full walkthrough: Manage agent configs as code and gate prompt changes in CI.

In the companion tutorial, [Run AgentControl offline evaluations in CI](https://launchdarkly.com/docs/tutorials/aic-cicd.md), a pull request workflow runs your application's handler and tools against a versioned dataset and blocks regressions before merge. That workflow catches changes inside the repository, such as a modified policy document or retriever, on every pull request. However, when someone edits a variation's prompt directly in LaunchDarkly, the change takes effect immediately without opening a pull request or running your pull request check.

By managing your agent configs as code in Git with `ldcli sync`, you close that gap. After you finish this tutorial, your repository and LaunchDarkly share a single ownership contract:

* **Git owns the prompt content.** Synced variations are marked "Defined in code" and locked against accidental edits in the LaunchDarkly UI, while teammates can still click **Duplicate variation** to run experiments in the UI.
* **Pull requests preview and test every prompt change.** Your CI workflow runs `ldcli sync all --dry-run --diff` alongside your offline evaluation suite, blocking any prompt edit that degrades answer accuracy before `ldcli sync` applies it.
* **LaunchDarkly still owns traffic.** Merging a prompt change to `main` publishes the new variation version to LaunchDarkly while leaving targeting rules, segments, and guarded rollouts untouched.

## Prerequisites

To complete this tutorial, you need:

* A LaunchDarkly [access token](https://launchdarkly.com/docs/home/account/api-create.md) with write access to your project. You use this token to authenticate the CLI and to set `LD_API_TOKEN` in your `.env` file.
* A local checkout of the [offline-evals-ci repository](https://github.com/launchdarkly-labs/offline-evals-ci). Clone the repository, copy `.env.example` to `.env`, and set these environment variables:
  * `LD_SDK_KEY`
  * `LD_API_TOKEN`
  * `LD_PROJECT_KEY`
  * `OPENAI_API_KEY`
* A LaunchDarkly project with the resources from [Run AgentControl offline evaluations in CI](https://launchdarkly.com/docs/tutorials/aic-cicd.md). Run `python -m bootstrap.bootstrap` from your checkout to create these resources in your [project](https://launchdarkly.com/docs/home/account/project):
  * `policy-agent` config with its `grounded` and `concise` variations.
  * `search_member_policy` tool.
  * `policy-accuracy` and `policy-tool-use` judges.
* The [LaunchDarkly CLI](https://launchdarkly.com/docs/home/getting-started/ldcli) with `sync` support, authenticated with your access token.

## Concepts

Before you run `ldcli sync`, review the three mechanisms that govern how agent configs as code keeps Git and LaunchDarkly in agreement:

<table>
  <thead>
    <tr>
      <th>Mechanism</th>
      <th>How it works</th>
      <th>How to use it</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Two-owner model</td>
      <td>Every variation, tool, and skill has a single owner. Resources created in the LaunchDarkly UI start owned by LaunchDarkly. When you sync a resource from your repository with `ldcli sync`, LaunchDarkly marks it "Defined in code" and makes its content read-only in the UI.</td>
      <td>To experiment in the UI without changing the governed variation, click **Duplicate variation** on the variation card to create a LaunchDarkly-owned copy.</td>
    </tr>

    <tr>
      <td>Three-way fingerprint baseline</td>
      <td>Each working copy stores the SHA-256 fingerprint of its last synchronized state in `.launchdarkly/sync.lock`.</td>
      <td>Commit `sync.lock` to Git alongside your prompt files so `ldcli sync` can compare your local working copy, the committed baseline, and LaunchDarkly.</td>
    </tr>

    <tr>
      <td>Governance and traffic separation</td>
      <td>`ldcli sync` manages what a variation says, including its messages, model parameters, and attached tools or skills.</td>
      <td>Use LaunchDarkly [targeting rules](https://launchdarkly.com/docs/home/agentcontrol/target.md) and [guarded rollouts](https://launchdarkly.com/docs/home/releases/guarded-rollouts) to control which variation serves production traffic.</td>
    </tr>
  </tbody>
</table>

## Step 1: Bootstrap existing variations into your repository

Start by pulling the `policy-agent` variations and their attached `search_member_policy` tool from LaunchDarkly into `.launchdarkly/` so your repository has a tracked baseline.

1. Preview the files that `ldcli sync prompts add` creates without writing to disk by running the following command from the root of your `offline-evals-ci` checkout:

```bash title="Terminal" lines wrap theme={null}
ldcli sync prompts add \
--project "$LD_PROJECT_KEY" \
--config-key policy-agent \
--key grounded \
--key concise \
--dry-run --diff
```

2. Review the preview output. The CLI lists the tool file and the two variation files it generates, as in the following example:

```text title="Output" lines wrap theme={null}
3 files to create:
.launchdarkly/offline-evals-ci/tools/search_member_policy.json           +23
.launchdarkly/offline-evals-ci/configs/policy-agent/grounded.prompt.md   +23
.launchdarkly/offline-evals-ci/configs/policy-agent/concise.prompt.md    +25
```

3. Run the command with `--yes --no-input` to write the files and record their initial SHA-256 fingerprints in `.launchdarkly/sync.lock`. Here is the command:

```bash title="Terminal" lines wrap theme={null}
ldcli sync prompts add \
--project "$LD_PROJECT_KEY" \
--config-key policy-agent \
--key grounded \
--key concise \
--yes --no-input
```

![Terminal output showing ldcli sync prompts add creating search\_member\_policy.json, grounded.prompt.md, and concise.prompt.md and recording SHA-256 fingerprints in sync.lock.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/ldcli-sync-prompts-add.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=3d2aac042159bda63d3e172741471986)

Running ldcli sync prompts add to pull the grounded and concise variations and their search\_member\_policy tool into .launchdarkly/.

4. Open `.launchdarkly/<your-project-key>/configs/policy-agent/concise.prompt.md` to inspect the generated format. The YAML frontmatter stores the variation key, mode, model configuration, and tool references, and the body holds the role blocks:

```markdown title=".launchdarkly/offline-evals-ci/configs/policy-agent/concise.prompt.md" lines wrap theme={null}
---
formatVersion: 1
upsert: true
mode: completion
key: concise
name: concise
modelConfigKey: OpenAI.gpt-4.1-mini
modelConfigVersion: 1
model:
modelName: gpt-4.1-mini
tools:
- key: search_member_policy
---

<system>
You are ToggleHealth's policy assistant. You are helping member {{member_id}}.

1. Call search_member_policy with member_id {{member_id}} and a short query before you answer.
 Never answer from memory, and never guess the member's plan.
2. Answer only from the excerpts it returns. Quote dollar amounts exactly.
3. If the excerpts don't answer the question, say so and suggest calling Member Services.
4. End with "Source:" followed by the doc_id of each excerpt you used.

Keep the answer to two sentences before the Source line.
</system>
```

![Editor view of .launchdarkly/offline-evals-ci/configs/policy-agent/concise.prompt.md showing YAML frontmatter and the system prompt block.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/concise-prompt-md.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=bf9bd6f858bc2d8cfe136db407686d33)

The generated concise.prompt.md file storing YAML frontmatter for the model and tool bindings alongside the system prompt role block.

## Step 2: Link an existing Python prompt in place without refactoring

If your team already stores prompts in Python, TypeScript, JavaScript, or Markdown files, you do not have to move them into `.launchdarkly/` markdown files. Use `ldcli sync prompts link` to bind a variation directly to the existing constant or agent definition in your code.

1. Remove the generated `grounded.prompt.md` file so you can link the `grounded` variation directly to `SYSTEM_PROMPT` in `bootstrap/bootstrap.py`. Use these commands:

```bash title="Terminal" lines wrap theme={null}
rm .launchdarkly/"$LD_PROJECT_KEY"/configs/policy-agent/grounded.prompt.md

ldcli sync prompts link \
--file bootstrap/bootstrap.py \
--format python \
--select 'constants[id="SYSTEM_PROMPT"]' \
--map 'prompt=$' \
--project "$LD_PROJECT_KEY" \
--config-key policy-agent \
--key grounded \
--yes --no-input
```

2. Confirm that `ldcli` writes a `.ref.yaml` descriptor and records the variation as synchronized. The output is below:

```text title="Output" lines wrap theme={null}
Proposed descriptor .launchdarkly/offline-evals-ci/configs/policy-agent/grounded.ref.yaml:
formatVersion: 1
file: bootstrap/bootstrap.py
format: python
select: constants[id="SYSTEM_PROMPT"]
map:
prompt: $

Mapped dependency: app/tools.py
Linked .launchdarkly/offline-evals-ci/configs/policy-agent/grounded.ref.yaml.
Prompts
recorded   offline-evals-ci/policy-agent/grounded   same as LaunchDarkly

1 applied.
```

![Terminal output of ldcli sync prompts link writing grounded.ref.yaml and recording policy-agent/grounded as synchronized.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/ldcli-sync-prompts-link.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=ca9e180970d347d3ce0ace9f0386cbe4)

Linking SYSTEM\_PROMPT in bootstrap/bootstrap.py directly to the grounded variation with ldcli sync prompts link.

![Editor view of grounded.ref.yaml alongside bootstrap/bootstrap.py showing the linked SYSTEM\_PROMPT constant.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/grounded-ref-yaml.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=9122f4bbd485974285e069b4fde9a10f)

The grounded.ref.yaml reference descriptor pointing to constants\[id="SYSTEM\_PROMPT"] in bootstrap/bootstrap.py.

3. Inspect `.launchdarkly/sync.lock` to verify that all three resources are tracked. Here is the lockfile:

```yaml title=".launchdarkly/sync.lock" lines wrap theme={null}
# Written by ldcli sync. Commit this file with the .launchdarkly files.
formatVersion: 1
resources:
- kind: tool
  project: offline-evals-ci
  key: search_member_policy
  fingerprint: sha256:e9fd0eb3b727f6f2ffb39c717c5f40603ee2ad68a8ec17fb22428ca121840859
- kind: variation
  project: offline-evals-ci
  key: policy-agent/concise
  fingerprint: sha256:b6eff700286d1d80d8597609614dc3c7717ba4e0af7ea5249c46a2e16b62391b
- kind: variation
  project: offline-evals-ci
  key: policy-agent/grounded
  fingerprint: sha256:455b9e0b328fd2092842b353d4e85ad0f360330113b1cba04e211ef6b337fefd
  fingerprintSchema: launchdarkly.config.projection/1
  referenceDigest: sha256:f73ffe2227836723a07515e0afa5a06ff530ad32c690556517b9409f95a50aa7
  adapterBehavior: python/1
```

![Editor view of .launchdarkly/sync.lock showing SHA-256 fingerprints for search\_member\_policy, policy-agent/concise, and policy-agent/grounded.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/sync-lock-fingerprints.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=9fe92a8a9fed01c42214aabb6de67ee3)

The .launchdarkly/sync.lock file storing SHA-256 baseline fingerprints for each synced tool and variation.

## Step 3: Verify the read-only lock and Duplicate variation flow in LaunchDarkly

After `ldcli sync` registers your repository in the project's sync manifest, LaunchDarkly locks the synced variations against direct UI edits so no one can silently overwrite what your repository governs. To verify the lock in LaunchDarkly:

1. In LaunchDarkly, click **Agent management** > **Agents**. The Agents list appears.
2. Click **Policy agent** and open the **Variations** tab.
3. Locate the `grounded` variation card. The card displays a "Defined in code" alert naming `bootstrap/bootstrap.py`. The prompt and model fields are read-only.

![LaunchDarkly UI showing the policy-agent grounded variation card with the Defined in code banner and locked prompt fields.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/defined-in-code-locked.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=7cfd08d135c52a490492440ecb66e4f7)

The grounded variation in LaunchDarkly marked Defined in code with read-only prompt and model fields.

4. Click **Duplicate variation** inside the "Defined in code" alert when you want to test a candidate prompt or model in the Playground or an experiment. LaunchDarkly creates a new variation named `grounded-copy` owned by LaunchDarkly while keeping `grounded` locked to Git.

![LaunchDarkly Duplicate variation card creating a LaunchDarkly-owned grounded-copy variation while keeping grounded locked to Git.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/defined-in-code-duplicate.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=69650082422d3084bb73ac8e08549e9a)

Clicking Duplicate variation on a Defined in code variation creates an editable LaunchDarkly-owned copy for experimentation.

## Step 4: Preview drift and block a bad prompt edit in pull request CI

When an engineer edits a synced prompt in a pull request, your CI workflow previews the exact LaunchDarkly diff and runs your offline evaluation dataset before any write reaches LaunchDarkly.

1. Simulate a faulty prompt edit in `bootstrap/bootstrap.py` by changing Rule 2 of `SYSTEM_PROMPT` so the assistant rounds copay amounts instead of quoting them. Here is the updated constant:

```python title="bootstrap/bootstrap.py" lines wrap theme={null}
SYSTEM_PROMPT = """You are ToggleHealth's policy assistant. You are helping member {{member_id}}.

1. Call search_member_policy with member_id {{member_id}} and a short query before you answer.
 Never answer from memory, and never guess the member's plan.
2. Summarize the member's benefits in your own words. Round copays to the nearest ten dollars.
3. If the excerpts don't answer the question, say so and suggest calling Member Services.
4. End with "Source:" followed by the doc_id of each excerpt you used."""
```

2. Run `ldcli sync all --dry-run --diff` locally to preview how `ldcli` compares the edited Python constant against LaunchDarkly without applying it. Here is the command:

```bash title="Terminal" lines wrap theme={null}
ldcli sync all --dry-run --diff
```

3. Review the diff output. The CLI reports `1 prompt push` for `policy-agent/grounded` and prints the exact line change:

```text title="Output" lines wrap theme={null}
1 change: 1 prompt push. 2 in sync.

Prompts
push   offline-evals-ci/policy-agent/grounded   messages changed   +1 -1

    projection (changed)

      --- LaunchDarkly now
      +++ LaunchDarkly after sync
      @@ -5,3 +5,3 @@
             "role": "system",
      -      "content": "...2. Answer only from the excerpts it returns. Quote dollar amounts exactly..."
      +      "content": "...2. Summarize the member's benefits in your own words. Round copays to the nearest ten dollars..."
           }
```

![Terminal output of ldcli sync all --dry-run --diff showing the +1 -1 unified diff on policy-agent/grounded.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/ldcli-sync-dry-run-diff.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=9fee380a02df83e1751a73b4bdf3f92d)

Running ldcli sync all --dry-run --diff to preview the exact prompt projection change before applying.

4. Update `.github/workflows/evals-pr.yml` so pull requests run `ldcli sync all --dry-run --diff` alongside `evals/run_evals.py`, and merges to `main` run `ldcli sync all --yes --no-input` only after the checks pass:

```yaml title=".github/workflows/evals-pr.yml" lines wrap theme={null}
sync-preview:
  needs: [changes, offline]
  if: github.event_name == 'pull_request' && needs.changes.outputs.app == 'true' && github.event.pull_request.head.repo.full_name == github.repository
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v4
    - name: Preview config sync plan
      env:
        LD_ACCESS_TOKEN: ${{ secrets.LD_API_TOKEN }}
      run: |
        ldcli sync all --dry-run --diff --output markdown >> "$GITHUB_STEP_SUMMARY"

evals:
  needs: [changes, offline]
  if: needs.changes.outputs.app == 'true' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-python@v5
      with: { python-version: "3.12", cache: pip }
    - run: pip install -r requirements.txt
    - name: Run the PR eval set
      env:
        LD_SDK_KEY: ${{ secrets.LD_SDK_KEY }}
        LD_API_TOKEN: ${{ secrets.LD_API_TOKEN }}
        LD_PROJECT_KEY: ${{ vars.LD_PROJECT_KEY }}
        OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
      run: python -m evals.run_evals --dataset evals/datasets/pr.jsonl ${{ vars.EVALS_BLOCKING == 'true' && '--blocking' || '' }}

evals-gate:
  needs: [changes, offline, sync-preview, evals]
  if: ${{ !cancelled() && github.event_name == 'pull_request' }}
  runs-on: ubuntu-latest
  steps:
    - run: |
        echo "offline: ${{ needs.offline.result }}, sync-preview: ${{ needs.sync-preview.result }}, evals: ${{ needs.evals.result }}"
        [ "${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}" = "false" ]
```

When a pull request contains the rounded-copay prompt edit, the `policy-accuracy` judge fails row 1 because the assistant quotes `$30` instead of the expected `$25` Gold plan primary care copay. As a result, `evals-gate` fails and GitHub blocks the merge before `sync-apply` can run.

![GitHub Pull Request showing sync-preview diff summary, failed policy-accuracy evaluation for member M-1001, and Merging is blocked.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/gh-pr-evals-gate-blocked.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=5cc1d5f88b370fdd863e8aab4a8a8870)

GitHub Pull Request #4 blocked by evals-gate after policy-accuracy flags the $30 rounded copay against the $25 reference dataset.

## Step 5: Reconcile approved changes on merge

After you restore Rule 2 in `bootstrap/bootstrap.py` to `Quote dollar amounts exactly.` and merge the pull request to `main`, `ldcli sync all --yes --no-input` publishes the approved variation to LaunchDarkly.

![GitHub Pull Request showing passing offline, sync-preview, evals, and evals-gate checks ready for merge.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/gh-pr-evals-gate-merged.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=475c22188df5d3b842893a1fdd39a153)

After restoring the exact-dollar rule in bootstrap/bootstrap.py, all pull request evaluation checks pass and the pull request merges to main.

1. Run `ldcli sync all --yes --no-input` to apply the approved local state and update the remote manifest and `.launchdarkly/sync.lock`. Here is the command:

```bash title="Terminal" lines wrap theme={null}
ldcli sync all --yes --no-input
```

2. Confirm that the CLI verifies state before writing, publishes the updated variation, and leaves all three resources in sync. Here is the output:

```text title="Output" lines wrap theme={null}
Prompts
pushed   offline-evals-ci/policy-agent/grounded   pins search_member_policy v1

1 applied. 2 in sync.
```

![Terminal output of ldcli sync all --yes --no-input publishing policy-agent/grounded to LaunchDarkly while keeping targeting rules untouched.](https://mintcdn.com/launchdarkly/_JmedtXPej1UIC-K/images/tutorials/configs-in-code/ldcli-sync-apply-main.png?fit=max\&auto=format\&n=_JmedtXPej1UIC-K\&q=85\&s=659cd969d03323e5437c218ebab68fa4)

Running ldcli sync all --yes --no-input on merge to main to publish the approved variation version and update the sync manifest.

## Troubleshooting

Use the table below to diagnose common `ldcli sync` messages:

<table>
  <thead>
    <tr>
      <th>Message or code</th>
      <th>Cause</th>
      <th>Fix</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`variation_managed_in_code`</td>
      <td>A user or script attempted to edit a code-owned variation through the LaunchDarkly UI or REST API without `ldcli sync`, returning HTTP 403.</td>
      <td>Edit the prompt in your Git repository and run `ldcli sync`, or click **Duplicate variation** in the UI to experiment on a copy.</td>
    </tr>

    <tr>
      <td>`Note: <key> is managed in LaunchDarkly; pulled, not pushed`</td>
      <td>Someone switched the variation's source of truth to LaunchDarkly in the UI.</td>
      <td>Open the variation menu in LaunchDarkly and enable **Code sync** to return ownership to your repository.</td>
    </tr>

    <tr>
      <td>`Another working copy synced <key> after your sync.lock`</td>
      <td>A teammate or CI job pushed a newer change to LaunchDarkly from another clone of the repository.</td>
      <td>Run `git pull` to update your local `.launchdarkly/` files and `sync.lock`, then run `ldcli sync` again.</td>
    </tr>
  </tbody>
</table>
