Installation

Install as Agent Plugin Recommended
copilot plugin marketplace add samueltauil/skills-hub
copilot plugin install elasticsearch-anomaly-detection-explainer@skills-hub

Works with GitHub Copilot CLI and VS Code (enable chat.plugins.enabled). Plugin elasticsearch-anomaly-detection-explainer v1.0.0 follows the Agent Plugins spec.

Install with the gh extension
gh skills-hub install elasticsearch-anomaly-detection-explainer

Don't have the extension? Run gh extension install samueltauil/skills-hub first.

Download and extract to your repository:

.github/skills/elasticsearch-anomaly-detection-explainer/

Extract the ZIP to .github/skills/ in your repo. The folder name must match elasticsearch-anomaly-detection-explainer for Copilot to auto-discover it.

Skill Files (2)

SKILL.md 17.4 KB
---
name: elasticsearch-anomaly-detection-explainer
description: >
  Explain Elasticsearch ML anomaly detection scores, model behavior, and result interpretation.
  Use when the user asks why a score is high or low, how the model learns, what the
  numbers mean, or how to troubleshoot unexpected anomaly scores.
compatibility: >
  Requires Elasticsearch 8.x+ or Elastic Cloud Serverless with ML anomaly detection.
  Uses the ML REST API and the standard _search API against .ml-anomalies-* — no ES|QL.
  User needs monitor_ml privilege for ML APIs.
metadata:
  author: elastic
  version: 0.3.0
  universal: true
---

# Anomaly Detection Score Explainer

Explain anomaly scores, model behavior, and why results look the way they do. Use the **ML REST API** for job config and
the **standard `_search` API** against `.ml-anomalies-*` for results — no ES|QL, fully compatible with Elastic
Serverless. For job lifecycle (create, start, stop), use the `elasticsearch-anomaly-detection` skill.

<!-- begin-partial: preamble -->

## Environment Configuration

This skill executes Elasticsearch operations through the `elastic` CLI. If the
[`elastic` CLI](https://github.com/elastic/cli#configuration) is not installed, tell the user what it is needed for. Do
not guess credentials, call the HTTP API directly, or attempt other workarounds.

This skill references operations in HTTP-shorthand form (e.g., `GET /`, `GET /_cat/indices`, `GET /{index}/_mapping`,
`GET /{index}/_settings/index.mode`, `POST /_query`). The [Operations](#operations) table at the end of this document
maps each shorthand to the equivalent `elastic` CLI command — always use the CLI rather than calling the HTTP API
directly.

<!-- end-partial: preamble -->

> **Prerequisite:** ML anomaly detection requires a Platinum-equivalent license on self-managed clusters. Serverless
> projects include ML. The caller needs `monitor_ml` to read job config and anomaly results.
>
> **Serverless note:** The `_ml/.../results/*` REST endpoints return HTTP 410 in Elastic Serverless. Always use
> `POST /.ml-anomalies-*/_search` for result queries instead — fully supported everywhere this skill runs.

## Process

1. **Decide whether to fetch data or interpret what the user supplied.** If the user embeds an anomaly record (or job
   config) in the prompt, interpret it directly using the domain knowledge below — do not call APIs to re-fetch fields
   already present. If the job ID, time range, or record is missing, retrieve it from the cluster.

   The decision: proceed with judgment-only explanation when the record contains `record_score`, `initial_record_score`,
   `actual`, `typical`, and `function`; otherwise fetch the missing pieces before explaining.

2. **Verify connectivity when calling the cluster.** Call `GET /`. If the call fails, stop and surface the connection
   error — do not guess endpoints or credentials.

3. **Resolve the job ID and load config.** When the job ID is unknown, call `GET /_ml/anomaly_detectors` to list
   candidates. Call `GET /_ml/anomaly_detectors/{job_id}` for full `analysis_config` (bucket_span, detectors,
   custom_rules, use_null, model_plot_config) and `GET /_ml/anomaly_detectors/{job_id}/_stats` for `state`,
   `model_size_stats.memory_status`, and data counts.

   The decision: confirm detector function and direction match the user's question before interpreting scores. A
   `low_count` job legitimately fires on drops; a `high_count` job does not.

4. **Retrieve anomaly records for the time range.** Call `POST /.ml-anomalies-*/_search` with `result_type: record`, the
   job ID, a timestamp range, and optional `record_score` filter. Read `initial_record_score`, `record_score`, `actual`,
   `typical`, `function`, `multi_bucket_impact`, and `anomaly_score_explanation`.

   Always show **both** `initial_record_score` and `record_score`. The gap is the renormalization story.

5. **Classify the score pattern before speculating on causes.**
   - **`initial_record_score` >> `record_score`** — **Renormalization.** A later, more extreme anomaly rescale this
     record downward. This is expected, healthy model behavior — not a broken model or reason to distrust the detection.
     Use `initial_record_score` for alerting severity; show both scores and explain the gap explicitly.
   - **`initial_record_score` == `record_score`** — No renormalization has occurred since detection.
   - **`actual` << `typical` with `low_count`, `count`, or `low_mean`** — **Absence / drop anomaly.** A high score is
     legitimate — the job detected an outage, pipeline stall, or service failure. This is **not** a false positive.
     Recommend incident investigation, not score tuning.
   - **`actual` >> `typical` with `high_count` or `high_mean`** — Spike anomaly; confirm with `single_bucket_impact`.

   **Only cite `anomaly_score_explanation` factors present in the record.** If `high_variance_penalty` is `false`, do
   not blame variance. If a factor is absent, note that it was not returned — do not invent it.

6. **Quantify renormalization across the job (optional).** Re-query `POST /.ml-anomalies-*/_search` for records in the
   time range sorted by `timestamp` ascending. Compute `score_drift = initial_record_score − record_score` per record
   and filter to `|score_drift| ≥ 20`. Large negative drift (initial >> record) confirms renormalization after a more
   extreme anomaly appeared later.

7. **Add context when the user asks "what caused this?" or "why so low/high?"**
   - **Model bounds** — If `model_plot_config.enabled` is true, call `POST /.ml-anomalies-*/_search` with
     `result_type: model_plot` for the same job and time range. Compare `actual` to `model_lower` / `model_upper`.
   - **Influencers** — Call `POST /.ml-anomalies-*/_search` with `result_type: influencer` for the bucket time range;
     sort by `influencer_score` descending.
   - **Categorization jobs** — Call `POST /.ml-anomalies-*/_search` with `result_type: category_definition` to list
     learned log patterns (`terms`, `regex`, `examples` per `category_id`).

   For aggregations, cross-job queries, bucket-level results, or custom filters beyond score and time, see
   [references/explainer-reference.md](references/explainer-reference.md).

## Common multi-step workflows

| Task                            | Steps (in order)                                                                                     |
| ------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **Explain a specific anomaly**  | job config → records (job + exact time) → show initial_record_score vs record_score + score factors. |
| **Why is my score low?**        | job config → records → renormalization check → model plot (if enabled) → explain score factors.      |
| **Why is my score high?**       | job config → records → check function direction, insufficient history, use_null, cardinality.        |
| **Renormalization drift**       | records (timestamp sort) → compute score_drift → list records where initial >> record.               |
| **Which entities contributed?** | influencers (job + time range) → sort by influencer_score.                                           |
| **Visualize model bounds**      | model plot (job + time range) → compare model_lower/model_upper vs actual.                           |
| **Categorization job patterns** | category_definition (job_id) → terms, regex, examples per category.                                  |

## Critical principles

- **Retrieve the record first** (or use the one the user supplied). Never explain scores without `initial_record_score`,
  `record_score`, `actual`, `typical`, and `function`.
- **Renormalization is healthy.** When `initial_record_score >> record_score`, a more extreme anomaly appeared later and
  lowered this score — expected behavior, not a model failure.
- **Direction matters.** `low_count` fires when values drop; `high_count` fires on spikes. A high score on a traffic
  stop with `low_count` is correct detection, not a false positive.
- **Explain factors before speculating.** Read `anomaly_score_explanation` from the record. Only address factors that
  are present and relevant.
- **Job config is essential.** `bucket_span`, detector function, `custom_rules`, `use_null`, and memory status all
  affect scores. Inspect job config when a score is surprising.
- **Model plot is the most visual explanation.** When enabled, show model bounds to illustrate where the actual value
  falls relative to the expected range.
- For job health ("missing documents", "memory limit", "datafeed not running") use the `elasticsearch-anomaly-detection`
  skill.

## Domain knowledge

### Score types

| Term                     | Meaning                                                                                  |
| ------------------------ | ---------------------------------------------------------------------------------------- |
| **record_score**         | Normalized 0–100 for a single anomaly record; updated by renormalization. >75 critical.  |
| **initial_record_score** | Score assigned at detection time, before renormalization. Use for alerting.              |
| **anomaly_score**        | Bucket-level severity aggregated across all detectors in a job.                          |
| **influencer_score**     | How unusual a specific entity (host, user, service) is in a bucket; high = likely cause. |
| **multi_bucket_impact**  | 0–5; how much sustained, multi-bucket behavior raised the score. ≥3 = behavioral shift.  |

### anomaly_score_explanation factors

The `anomaly_score_explanation` field on each record breaks the score into components:

| Factor                             | Direction | Meaning                                                                 |
| ---------------------------------- | --------- | ----------------------------------------------------------------------- |
| **anomaly_length**                 | Raises    | Number of consecutive buckets the anomaly spans. Longer → higher score. |
| **single_bucket_impact**           | Raises    | Extremity of this single bucket. Lower probability → higher impact.     |
| **multi_bucket_impact**            | Raises    | Contribution of sustained multi-bucket pattern.                         |
| **anomaly_characteristics_impact** | Raises    | Whether the anomaly is a mean shift vs. variance change.                |
| **high_variance_penalty**          | Lowers    | Noisy data or early training → wide confidence bounds → score reduced.  |
| **incomplete_bucket_penalty**      | Lowers    | Bucket had less data than expected (delayed data, sparse events).       |

### Why a score might be unexpectedly low

- **high_variance_penalty:** The metric is historically noisy — wide confidence bounds absorb the spike.
- **Renormalization:** A more extreme anomaly appeared later and pushed this score down (`initial_record_score` >>
  `record_score`).
- **Insufficient training history:** Need ≥3 weeks for weekly seasonality, ≥2 full cycles for any detected period.
- **bucket_span too large:** Short-duration spikes get smoothed. Use a smaller `bucket_span` for high-frequency events.
- **Detector function mismatch:** `mean` vs `high_mean`, `count` vs `high_count` — only one direction fires.
- **incomplete_bucket_penalty:** Bucket received less data than expected (ingest latency or gaps).
- **custom_rules:** A detector filter may be suppressing the anomaly.

### Why a score might be unexpectedly high

- **Insufficient history:** Model hasn't learned the normal pattern yet — early anomalies are unreliable.
- **Model split thin:** High-cardinality `partition_field` or `by_field` → very few points per entity → unreliable
  probabilities.
- **use_null:** If `use_null: true`, missing entities produce "null" anomalies that may not be meaningful.
- **Absence / drop detection:** With `low_count` or `low_mean`, `actual << typical` produces a legitimately high score —
  treat as a real incident, not a false positive.

### Model behavior concepts

| Concept             | Meaning                                                                                                |
| ------------------- | ------------------------------------------------------------------------------------------------------ |
| **actual**          | Observed value. **typical** is what the model expected. The direction matters.                         |
| **Absence anomaly** | `actual << typical` with `count`, `low_count`, or `low_mean` → outage, pipeline stop, service failure. |
| **by_field**        | Independent baseline per entity (e.g., per host). Each entity compared to its own history.             |
| **over_field**      | Population analysis — entity compared to its peer group in the same bucket, not its own history.       |
| **partition_field** | Fully independent sub-models with separate score normalization per partition.                          |

### Model plot and categories

- **Model plot:** Shows the model's learned upper and lower bounds at each time point. If `actual` is within bounds, no
  anomaly; if outside, the score depends on the distance from bounds. Only available when `model_plot_config` is enabled
  on the job. Query via `POST /.ml-anomalies-*/_search` with `result_type: model_plot`.
- **Categories:** For jobs with a `categorization_field_name`, query `result_type: category_definition` to show log
  message patterns (terms, regex, examples per `category_id`). Anomaly records use `by_field_value = <category_id>`.

## Score troubleshooting protocol

1. **List jobs** — Call `GET /_ml/anomaly_detectors` when the job ID is unknown.
2. **Get job config and stats** — Call `GET /_ml/anomaly_detectors/{job_id}` and
   `GET /_ml/anomaly_detectors/{job_id}/_stats`. Verify `bucket_span`, detector function, `custom_rules`, `use_null`,
   job state (opened/closed/failed), and `model_size_stats.memory_status`.
3. **Retrieve the record** — Call `POST /.ml-anomalies-*/_search` with `result_type: record`, the job ID, time range,
   and optional minimum `record_score`. Inspect `initial_record_score`, `record_score`, `actual`, `typical`, `function`,
   `multi_bucket_impact`, and `anomaly_score_explanation`.
4. **Check renormalization** — Compare `initial_record_score` vs `record_score`. If initial >> record, re-query records
   sorted by timestamp and compute `score_drift` to quantify renormalization across the job.
5. **Visualize model bounds** — If `model_plot_config` is enabled, query `result_type: model_plot` and show where the
   actual value fell relative to `model_lower` and `model_upper`.
6. **Influencers** — Query `result_type: influencer` for the anomaly bucket time range; sort by `influencer_score`.
7. **Explain factors** — From the record's `anomaly_score_explanation`, address each **present** relevant factor:
   `high_variance_penalty`, `incomplete_bucket_penalty`, `anomaly_length`, `single_bucket_impact`,
   `multi_bucket_impact`. Do not cite factors absent from the record.

## Examples

- "Why is my anomaly score only 15 when the spike looks huge?" → Check renormalization: `initial_record_score` (~92) >>
  `record_score` (~15). The spike was real; the current score was rescaled down. Use the initial score for alerting.
- "Traffic stopped and I got a HIGH score — false positive?" → No. `low_count` with `actual` far below `typical` is
  legitimate absence detection. Investigate the outage.
- "Which entities contributed most to the anomalies in job X last night?" → Query influencers for the time range.
- "Show me the model bounds for this job." → Query model plot when `model_plot_config` is enabled.
- "List records where the score was renormalized down a lot." → Records sorted by timestamp; filter large
  `initial_record_score − record_score`.

## Guidelines

- Report only what the API or the user-supplied record contains; do not invent scores, timestamps, entity values, or
  explanation factors.
- Always show both `initial_record_score` and `record_score` when explaining a record; state explicitly whether
  renormalization occurred.
- When a score factor is missing from the record, do not assert it; note that the field was not returned.
- Do not attribute low scores to `high_variance_penalty` or `incomplete_bucket_penalty` when those flags are `false` or
  absent in the record.
- For investigation ("what caused this?", "which service is responsible?") query influencers or construct cross-job
  searches via [references/explainer-reference.md](references/explainer-reference.md).
- For job health use the `elasticsearch-anomaly-detection` skill.

## Operations

| HTTP API (shorthand)                         | `elastic` CLI command                                                           |
| -------------------------------------------- | ------------------------------------------------------------------------------- |
| `GET /`                                      | `elastic es info`                                                               |
| `GET /_ml/anomaly_detectors`                 | `elastic es ml get-jobs`                                                        |
| `GET /_ml/anomaly_detectors/{job_id}`        | `elastic es ml get-jobs --job-id '<job_id>'`                                    |
| `GET /_ml/anomaly_detectors/{job_id}/_stats` | `elastic es ml get-job-stats --job-id '<job_id>'`                               |
| `POST /.ml-anomalies-*/_search`              | `elastic es search --index '.ml-anomalies-*' --input-file '<search-body.json>'` |

Query body shapes for each `result_type` (`record`, `influencer`, `model_plot`, `category_definition`) are documented in
[references/explainer-reference.md](references/explainer-reference.md).
references/
explainer-reference.md 6.7 KB
# Anomaly Detection Explainer — API Reference

This reference documents the HTTP API calls used to explain anomaly scores and model behavior.

**Two API layers are used:**

- **ML REST API** (`GET /_ml/anomaly_detectors/...`) — for job config and stats. Available everywhere.
- **Standard `_search` API** (`POST /.ml-anomalies-*/_search`) — for all result queries. Fully supported in Elastic
  Serverless. Used instead of the ML results REST endpoints (`/_ml/anomaly_detectors/{job_id}/results/...`) which return
  HTTP 410 in Elastic Serverless.

Requires `monitor_ml` cluster privilege.

## Endpoint summary

| Operation          | HTTP API (shorthand)                         | Layer       | Purpose                                                 |
| ------------------ | -------------------------------------------- | ----------- | ------------------------------------------------------- |
| List jobs          | `GET /_ml/anomaly_detectors`                 | ML REST API | All job IDs and basic config.                           |
| Job config         | `GET /_ml/anomaly_detectors/{job_id}`        | ML REST API | Full analysis_config, datafeed_config, analysis_limits. |
| Job stats          | `GET /_ml/anomaly_detectors/{job_id}/_stats` | ML REST API | State, memory_status, data_counts.                      |
| Anomaly records    | `POST /.ml-anomalies-*/_search`              | Search API  | Records filtered by job_id, result_type, time, score.   |
| Influencers        | `POST /.ml-anomalies-*/_search`              | Search API  | Influencer documents filtered by job_id, time, score.   |
| Model plot         | `POST /.ml-anomalies-*/_search`              | Search API  | model_plot documents with bounds and actual.            |
| Categories         | `POST /.ml-anomalies-*/_search`              | Search API  | category_definition documents for categorization jobs.  |
| Score reassessment | `POST /.ml-anomalies-*/_search` (+ client)   | Search API  | Records, then client-side drift computation.            |

## ML REST API calls

### List jobs

`GET /_ml/anomaly_detectors`

Returns all jobs. No `size` parameter; API always returns all. Response: `{ count, jobs: [...] }`.

Useful fields per job: `job_id`, `analysis_config.bucket_span`, `analysis_config.detectors`,
`analysis_limits.model_memory_limit`.

### Job config and stats

`GET /_ml/anomaly_detectors/{job_id}`

`GET /_ml/anomaly_detectors/{job_id}/_stats`

Config response: `analysis_config` (bucket_span, detectors with function, field_name, by/over/partition fields,
custom_rules, use_null), `datafeed_config` (indices, query, query_delay), `analysis_limits.model_memory_limit`.

Stats response: `state` (opened/closed/failed), `data_counts` (processed_record_count, etc.), `model_size_stats`
(memory_status, model_bytes, peak_model_bytes).

## Search API — result queries

All result queries use `POST /.ml-anomalies-*/_search` with a bool filter query. Every query includes
`{ "term": { "result_type": "<type>" } }` and `{ "term": { "job_id": "<job_id>" } }`.

### Anomaly records

```json
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "result_type": "record" } },
        { "term": { "job_id": "<job_id>" } },
        { "range": { "timestamp": { "gte": "<start>", "lt": "<end>" } } },
        { "range": { "record_score": { "gte": <min_score> } } }
      ]
    }
  },
  "sort": [{ "record_score": "desc" }],
  "from": 0,
  "size": 100
}
```

Key response fields per record: `job_id`, `timestamp`, `record_score`, `initial_record_score`, `actual`, `typical`,
`function`, `field_name`, `by_field_name`, `by_field_value`, `partition_field_name`, `partition_field_value`,
`over_field_name`, `over_field_value`, `multi_bucket_impact`, `anomaly_score_explanation`, `probability`,
`detector_index`.

`anomaly_score_explanation` sub-fields: `anomaly_length`, `single_bucket_impact`, `multi_bucket_impact`,
`anomaly_characteristics_impact`, `high_variance_penalty`, `incomplete_bucket_penalty`.

### Influencers

```json
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "result_type": "influencer" } },
        { "term": { "job_id": "<job_id>" } },
        { "range": { "timestamp": { "gte": "<start>", "lt": "<end>" } } }
      ]
    }
  },
  "sort": [{ "influencer_score": "desc" }],
  "from": 0,
  "size": 100
}
```

Key response fields: `timestamp`, `influencer_field_name`, `influencer_field_value`, `influencer_score`,
`initial_influencer_score`.

### Model plot

```json
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "result_type": "model_plot" } },
        { "term": { "job_id": "<job_id>" } },
        { "range": { "timestamp": { "gte": "<start>", "lt": "<end>" } } }
      ]
    }
  },
  "sort": [{ "timestamp": "asc" }],
  "from": 0,
  "size": 500
}
```

Key response fields: `timestamp`, `model_lower`, `model_upper`, `model_median`, `actual`, `partition_field_value`,
`by_field_value`.

Only returns data if the job was created or updated with `model_plot_config: { enabled: true }`. Empty result means
model plot is not enabled for the job.

### Categories

```json
{
  "query": {
    "bool": {
      "filter": [{ "term": { "result_type": "category_definition" } }, { "term": { "job_id": "<job_id>" } }]
    }
  },
  "from": 0,
  "size": 100
}
```

Key response fields: `category_id`, `terms`, `regex`, `examples`, `max_matching_length`.

Only relevant for jobs with a `categorization_field_name`. Anomaly records for categorization jobs use
`by_field_value = <category_id>`.

### Score reassessment (client-side)

There is no dedicated endpoint. The workflow:

1. Fetch records for the job and time range (sorted by timestamp ascending) via `POST /.ml-anomalies-*/_search`.
2. Compute `score_drift = initial_record_score - record_score` per record.
3. Filter to records where `|score_drift| >= min_drift` (default 20).

Large negative drift (initial >> record) means renormalization lowered the score after a more extreme anomaly appeared.
Large positive drift is rare and indicates the model reconsidered upward.

## Parameter conventions

| Parameter  | Type   | Notes                                                                           |
| ---------- | ------ | ------------------------------------------------------------------------------- |
| job_id     | string | Exact job ID (no wildcards). ML API and `_search` filter are per-job.           |
| start, end | string | ISO 8601 or epoch ms. Used in `range` filter on `timestamp`.                    |
| min_score  | number | Minimum `record_score` (0–100). 50 = significant, 75 = critical.                |
| min_drift  | number | Minimum abs(initial_record_score − record_score) for reassessment (default 20). |
| size       | number | `size` in the `_search` request. For list-jobs: API returns all jobs.           |

License (Apache-2.0)

View full license text
Licensed under the Apache License, Version 2.0. See https://github.com/elastic/agent-skills/blob/main/LICENSE