$npx -y skills add launchdarkly/ai-tooling --skill custom-metricsCreate, track, retrieve, update, and delete custom business metrics for configs. Covers full lifecycle: define metric kinds via API, emit events via SDK, and query results.
| 1 | # Custom Metrics for Configs |
| 2 | |
| 3 | Full lifecycle management of custom business metrics: create metric definitions via API, track events via SDK, retrieve metric data, and manage metrics programmatically. |
| 4 | |
| 5 | ## Prerequisites |
| 6 | |
| 7 | - LaunchDarkly SDK initialized (see `sdk`) |
| 8 | - LaunchDarkly API token with `writer` role for metric management |
| 9 | - Understanding of built-in agent metrics (see `built-in-metrics`) |
| 10 | |
| 11 | ## API Key Detection |
| 12 | |
| 13 | Before prompting the user for an API key, try to detect it automatically: |
| 14 | |
| 15 | 1. **Check Claude MCP config** - Read `~/.claude/config.json` and look for `mcpServers.launchdarkly.env.LAUNCHDARKLY_API_KEY` |
| 16 | 2. **Check environment variables** - Look for `LAUNCHDARKLY_API_KEY`, `LAUNCHDARKLY_API_TOKEN`, or `LD_API_KEY` |
| 17 | 3. **Prompt user** - Only if detection fails, ask the user for their API key |
| 18 | |
| 19 | ```python |
| 20 | import os |
| 21 | import json |
| 22 | from pathlib import Path |
| 23 | |
| 24 | def get_launchdarkly_api_key(): |
| 25 | """Auto-detect LaunchDarkly API key from Claude config or environment.""" |
| 26 | # 1. Check Claude MCP config |
| 27 | claude_config = Path.home() / ".claude" / "config.json" |
| 28 | if claude_config.exists(): |
| 29 | try: |
| 30 | config = json.load(open(claude_config)) |
| 31 | api_key = config.get("mcpServers", {}).get("launchdarkly", {}).get("env", {}).get("LAUNCHDARKLY_API_KEY") |
| 32 | if api_key: |
| 33 | return api_key |
| 34 | except (json.JSONDecodeError, IOError): |
| 35 | pass |
| 36 | |
| 37 | # 2. Check environment variables |
| 38 | for var in ["LAUNCHDARKLY_API_KEY", "LAUNCHDARKLY_API_TOKEN", "LD_API_KEY"]: |
| 39 | if os.environ.get(var): |
| 40 | return os.environ[var] |
| 41 | |
| 42 | return None |
| 43 | ``` |
| 44 | |
| 45 | ## Metrics Lifecycle Overview |
| 46 | |
| 47 | | Step | Method | Purpose | |
| 48 | |------|--------|---------| |
| 49 | | 1. Create | API | Define metric in LaunchDarkly | |
| 50 | | 2. Track | SDK | Send events to the metric | |
| 51 | | 3. Get | API | Retrieve metric definition/data | |
| 52 | | 4. Update | API | Modify metric properties | |
| 53 | | 5. Delete | API | Remove metric | |
| 54 | |
| 55 | ## 1. Create Metric (API) |
| 56 | |
| 57 | **Required fields for numeric custom metrics:** |
| 58 | - `successCriteria` - Must be one of: `"HigherThanBaseline"`, `"LowerThanBaseline"` |
| 59 | - `unit` - e.g., `"count"`, `"percent"`, `"milliseconds"` |
| 60 | |
| 61 | The API will return `400 Bad Request` if these are missing for numeric metrics. |
| 62 | |
| 63 | ```python |
| 64 | import requests |
| 65 | import os |
| 66 | |
| 67 | def create_metric( |
| 68 | project_key: str, |
| 69 | metric_key: str, |
| 70 | name: str, |
| 71 | kind: str = "custom", |
| 72 | is_numeric: bool = True, |
| 73 | unit: str = "count", |
| 74 | success_criteria: str = "HigherThanBaseline", |
| 75 | event_key: str = None, |
| 76 | description: str = None |
| 77 | ): |
| 78 | """Create a new metric definition in LaunchDarkly.""" |
| 79 | API_TOKEN = os.environ.get("LAUNCHDARKLY_API_TOKEN") |
| 80 | |
| 81 | url = f"https://app.launchdarkly.com/api/v2/metrics/{project_key}" |
| 82 | |
| 83 | payload = { |
| 84 | "key": metric_key, |
| 85 | "name": name, |
| 86 | "kind": kind, |
| 87 | "isNumeric": is_numeric, |
| 88 | "eventKey": event_key or metric_key |
| 89 | } |
| 90 | |
| 91 | # Unit and successCriteria are required for numeric custom metrics |
| 92 | if is_numeric and kind == "custom": |
| 93 | payload["unit"] = unit |
| 94 | payload["successCriteria"] = success_criteria |
| 95 | |
| 96 | if description: |
| 97 | payload["description"] = description |
| 98 | |
| 99 | headers = { |
| 100 | "Authorization": API_TOKEN, |
| 101 | "Content-Type": "application/json" |
| 102 | } |
| 103 | |
| 104 | response = requests.post(url, json=payload, headers=headers) |
| 105 | |
| 106 | if response.status_code == 201: |
| 107 | print(f"[OK] Created metric: {metric_key}") |
| 108 | return response.json() |
| 109 | elif response.status_code == 409: |
| 110 | print(f"[INFO] Metric already exists: {metric_key}") |
| 111 | return None |
| 112 | else: |
| 113 | print(f"[ERROR] Failed to create metric: {response.status_code}") |
| 114 | print(f" {response.text}") |
| 115 | return None |
| 116 | ``` |
| 117 | |
| 118 | **Metric Kinds:** |
| 119 | - `custom` - Track any event (most common for agent metrics) |
| 120 | - `pageview` - Track page views |
| 121 | - `click` - Track click events |
| 122 | |
| 123 | **Success Criteria** (for numeric metrics): |
| 124 | - `HigherThanBaseline` - Higher values are better (e.g., revenue, satisfaction) |
| 125 | - `LowerThanBaseline` - Lower values are better (e.g., errors, latency) |
| 126 | |
| 127 | **Common Units:** |
| 128 | - `count` - Generic count |
| 129 | - `milliseconds` - Time duration |
| 130 | - `percent` - Percentage values |
| 131 | - `dollars` - Currency |
| 132 | |
| 133 | ## 2. Track Events (SDK) |
| 134 | |
| 135 | Once the metric is created, track events using the SDK: |
| 136 | |
| 137 | ```python |
| 138 | from ldclient import Context |
| 139 | from ldclient.config import Config |
| 140 | import ldclient |
| 141 | |
| 142 | # Initialize (see sdk for details) |
| 143 | ldclient.set_config(Config("your-sdk-key")) |
| 144 | ld_client = ldclient.get() |
| 145 | |
| 146 | def track_metric(ld_client, user_id: str, metric_key: str, value: float, data: dict = None): |
| 147 | """Tra |