---
type: api
title: "Create or update an AI Index"
description: "Creates an AI Index with the given ID, or replaces an existing one. The request body replaces the whole record: omitted fields are removed, and omitted arrays become empty. A managed AI Index cannot be replaced and returns a 409."
url: https://www.elastic.co/elastic/docs-builder/docs/5066/api/kibana-serverless/operation/operation-put-context-engine-ai-index-aiindexid
resource: https://www.elastic.co/elastic/docs-builder/docs/5066/api/kibana-serverless/operation/operation-put-context-engine-ai-index-aiindexid
products:
  - Kibana
---

# Create or update an AI Index

Authentication: required

Availability: Experimental

- Server: `https://{kibana_url}`

## Paths

- `PUT` `/api/context_engine/ai_index/{aiIndexId}`
- `PUT` `/s/{space_id}/api/context_engine/ai_index/{aiIndexId}`

#### Path parameters

- `space_id` (string) — optional
  An identifier for the space. If `space_id` is not provided in the URL, the default space is used. Refer to [Spaces](https://www.elastic.co/docs/deploy-manage/manage-spaces) for more information.
- `aiIndexId` (string · min length: 1 · max length: 256) — required
  The unique identifier of the AI Index.

## Description

Creates an AI Index with the given ID, or replaces an existing one. The request body replaces the whole record: omitted fields are removed, and omitted arrays become empty. A managed AI Index cannot be replaced and returns a 409.

Each ES|QL source added also gets an index metadata automation, installed as a separate workflow and attached to the AI Index as the calling user, who needs the workflow create and update privileges for it; removing the source deletes the workflow, which needs the delete privilege. The save succeeds whether or not that works. When an automation could not be installed or removed, the response lists it under `warnings`.

Returns a 404 response when Context Engine is turned off in this space (`contextEngine:enabled`).

**For more information, refer to the [Context Engine documentation](https://www.elastic.co/docs/explore-analyze/ai-features/context-engine).**

## Required privileges

- Privileges: `contextEngine:write`

## Header parameters

- `kbn-xsrf` (string) — required
  A required header to protect against CSRF attacks

## Request body

`application/json`

- `automations` (object[] · max items: 100 · default: []) — optional
  Automations associated with the AI Index. Defaults to an empty array when omitted.
  - `type` (enum) — required
    Values: `workflow`
  - `value` (string · min length: 1 · max length: 1024) — required
    The workflow ID.
- `description` (string · max length: 2048) — optional
  Human-readable description of the AI Index.
- `dest` (object) — required
  The data stream or index that backs the AI Index.
  - `type` (enum) — required
    The type of the backing store. `data_stream` for a data stream, or `index` for an index.
    Values: `data_stream`, `index`
  - `value` (string · min length: 1 · max length: 1024) — required
    The data stream or index (e.g. `ai-index-ds-foo`, `ai-index-idx-foo`) the AI Index is attached to. Must name a single data stream or index (no wildcards or comma-separated lists), match `type`, and start with `ai-index-ds-` (for `data_stream`) or `ai-index-idx-` (for `index`). The rest of the value must be a valid AI Index ID. System indices are not allowed.
- `feedback_analysis` (object) — optional
  The recurring feedback analysis, which reads agent signals and proposes improvement actions for the AI Index.
  - `agent_id` (string · max length: 256) — optional
    Agent Builder agent ID that runs this index’s feedback-loop analysis.
  - `allowed_actions` (enum[] · max items: 9) — optional
    Improvement actions the analysis may propose. An empty list is observe-only.
    Values: `add_ki`, `edit_ki`, `remove_ki`, `add_workflow`, `edit_workflow`, `remove_workflow`, `add_source`, `edit_source`, `remove_source`
    Default: `["add_ki","edit_ki","remove_ki","add_workflow","edit_workflow","remove_workflow","add_source","edit_source","remove_source"]`
  - `enabled` (boolean) — required
    Desired state of the recurring analysis. The scheduler stays authoritative for whether it is actually running.
  - `schedule` (object · default: {"interval":"24h"}) — optional
    When the analysis runs.
    - `interval` (string · max length: 16) — required
      How often to analyze, for example `1h` or `24h`. At least 15 minutes.
  - `signal_filter` (string · max length: 4096) — optional
    KQL narrowing which signals this index analyzes, for example `tags: query_error`.
  - `signal_time_range` (union anyOf · default: {"from":"now-30d","type":"relative"}) — optional
    Which signals the analysis reads. A read filter only.
    - **type: relative**
      - `type` (enum) — required
        Values: `relative`
      - `from` (string · max length: 64) — required
        Date math relative to now, for example `now-30d`.
    - **type: absolute**
      - `type` (enum) — required
        Values: `absolute`
      - `from` (string · max length: 64) — required
        ISO 8601 date to analyze signals since.
- `memory_enabled` (boolean) — optional — deprecated
  Whether this AI index accepts memory writes. Defaults to true when omitted. Deprecated and ignored in Serverless and in Elastic 9.7 and later.
- `sources` (union anyOf[] · max items: 100 · default: []) — optional
  Additional sources that provide context for the AI Index. Defaults to an empty array when omitted.
  - **type: esql**
    - `type` (enum) — required
      Values: `esql`
    - `value` (string · min length: 1 · max length: 10240) — required
      The source value; an ES|QL query when `type` is `esql`. Must be valid ES|QL.
  - **type: connector**
    - `type` (enum) — required
      Values: `connector`
    - `value` (string · min length: 1 · max length: 10240) — required
      The source value; a connector ID when `type` is `connector`.
- `traces` (union anyOf[] · max items: 100 · default: []) — optional
  Trace sources linked to this AI Index. A write replaces the whole array. Defaults to an empty array when omitted.
  - **type: elastic_agent**
    - `type` (enum) — required
      Values: `elastic_agent`
    - `value` (string · min length: 1 · max length: 10240) — required
      The Agent Builder agent ID.
  - **type: index**
    - `type` (enum) — required
      Values: `index`
    - `value` (string · min length: 1 · max length: 10240) — required
      An index or data stream name or pattern to read traces from.
  - **type: esql**
    - `type` (enum) — required
      Values: `esql`
    - `value` (string · min length: 1 · max length: 10240) — required
      An ES|QL query to select traces.

## Responses

#### `200`

The AI Index was updated.

`application/json`

- `status` (enum)
  Values: `updated`
- `warnings` (object[] · max items: 100)
  Things the save was meant to do beyond storing the AI Index that did not happen, such as installing the index metadata automation of an ES|QL source. The AI Index was saved either way. Missing when nothing went wrong.
  - `message` (string)
    What did not happen for the source, and why.
  - `source` (object)
    - `type` (enum)
      `esql` for an ES|QL query, or `connector` for a data connector.
      Values: `esql`, `connector`
    - `value` (string)
      An ES|QL query for `esql`, or a connector ID for `connector`.
#### `201`

The AI Index was created.

`application/json`

- `status` (enum)
  Values: `created`
- `warnings` (object[] · max items: 100)
  Things the save was meant to do beyond storing the AI Index that did not happen, such as installing the index metadata automation of an ES|QL source. The AI Index was saved either way. Missing when nothing went wrong.
  - `message` (string)
    What did not happen for the source, and why.
  - `source` (object)
    - `type` (enum)
      `esql` for an ES|QL query, or `connector` for a data connector.
      Values: `esql`, `connector`
    - `value` (string)
      An ES|QL query for `esql`, or a connector ID for `connector`.
#### `400`

The request was invalid, for example a malformed `dest`, an invalid ES|QL source, or an unresolvable connector source or trace.

`application/json`

- `error` (string)
  The HTTP status text.
- `message` (string)
  A human-readable error message.
- `statusCode` (number)
  The HTTP status code.
#### `403`

Elasticsearch denied the `index` trace lookup outright; the caller lacks index privileges for it.

`application/json`

- `error` (string)
  The HTTP status text.
- `message` (string)
  A human-readable error message.
- `statusCode` (number)
  The HTTP status code.
#### `404`

Context Engine is turned off in this space.

`application/json`

- `error` (string)
  The HTTP status text.
- `message` (string)
  A human-readable error message.
- `statusCode` (number)
  The HTTP status code.
#### `409`

The AI Index is managed and immutable, or the write conflicted.

`application/json`

- `error` (string)
  The HTTP status text.
- `message` (string)
  A human-readable error message.
- `statusCode` (number)
  The HTTP status code.
## Code examples

#### Console

```console
PUT kbn:/api/context_engine/ai_index/customer_support
{
  "description": "Knowledge about customer support cases.",
  "dest": { "type": "data_stream", "value": "ai-index-ds-customer-support" },
  "automations": [{ "type": "workflow", "value": "8f1c2d3e-4b5a-6c7d-8e9f-0a1b2c3d4e5f" }],
  "sources": [{ "type": "esql", "value": "FROM support-cases | LIMIT 100" }],
  "traces": [{ "type": "index", "value": "traces-support" }]
}
```

#### curl

```curl
curl \
  -X PUT "${KIBANA_URL}/api/context_engine/ai_index/customer_support" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "kbn-xsrf: true" \
  -H "Content-Type: application/json" \
  -d '{
    "description": "Knowledge about customer support cases.",
    "dest": { "type": "data_stream", "value": "ai-index-ds-customer-support" },
    "automations": [{ "type": "workflow", "value": "8f1c2d3e-4b5a-6c7d-8e9f-0a1b2c3d4e5f" }],
    "sources": [{ "type": "esql", "value": "FROM support-cases | LIMIT 100" }],
    "traces": [{ "type": "index", "value": "traces-support" }]
  }'
```

## Response examples

#### AI Index updated

`application/json`

Example response after an existing AI Index is replaced

```json
{
  "status": "updated"
}
```

#### AI Index updated, with a warning

`application/json`

The AI Index was replaced, but the index metadata automation of one of its ES|QL sources could not be installed

```json
{
  "status": "updated",
  "warnings": [
    {
      "message": "No index matching support-cases-* was found, or you do not have read access to it.",
      "source": {
        "type": "esql",
        "value": "FROM support-cases-* | LIMIT 100"
      }
    }
  ]
}
```

#### AI Index created

`application/json`

Example response after a new AI Index is created

```json
{
  "status": "created"
}
```

#### Invalid dest

`application/json`

The dest value does not follow the naming convention

```json
{
  "error": "Bad Request",
  "message": "dest.value \u0027support-cases\u0027 is not allowed: it must start with \u0027ai-index-ds-\u0027",
  "statusCode": 400
}
```

#### Index trace lookup denied

`application/json`

The caller lacks Elasticsearch index privileges to look up an index trace

```json
{
  "error": "Forbidden",
  "message": "security_exception: action [indices:admin/resolve/index] is unauthorized for user [jdoe] with effective roles [support_reader] on indices [traces-support], this action is granted by the index privileges [view_index_metadata,manage,read,all]",
  "statusCode": 403
}
```

#### Context Engine turned off

`application/json`

Context Engine is turned off in this space

```json
{
  "error": "Not Found",
  "message": "Not Found",
  "statusCode": 404
}
```

#### Managed AI Index

`application/json`

The AI Index is managed by a plugin and cannot be replaced

```json
{
  "error": "Conflict",
  "message": "AI index \u0027customer_support\u0027 is managed and cannot be modified via the API",
  "statusCode": 409
}
```

