﻿---
title: Use Context Engine with Claude Code
description: Use the Context Engine skill and Elastic CLI to retrieve context from an AI index in Claude Code.
url: https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/use-context-engine-with-claude-code
products:
  - Elastic Documentation
  - Elastic Observability
  - Elastic Security
  - Elasticsearch
  - Kibana
applies_to:
  - Elastic Cloud Serverless: Experimental
  - Elastic Stack: Planned
---

# Use Context Engine with Claude Code
<important>
  This page is currently hidden from the documentation navigation.The following Context Engine pages are live:
  - [Landing page](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine)
    - [Context Engine quickstart](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/quickstart)
  - [Context Engine concepts](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/concepts)
  - [Build and maintain an AI index](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/build-and-maintain-ai-index)
    - [Create and manage AI indices](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/create-and-manage-ai-indices)
  - [Add and manage sources](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/add-and-manage-sources)
  - [Create and manage automations](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/create-and-manage-automations)
  - [Manage KIs](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/inspect-and-manage-knowledge-indicators)
  - [Evaluate and improve KIs](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/evaluate-and-improve-knowledge-indicators)
  - [Use an AI index](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/use-an-ai-index)
    - [Retrieve context](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/retrieve-context-from-ai-index)
  - [Save and retrieve memories](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/save-and-retrieve-memories)
  - [Configure agents to use an AI index](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/use-context-engine-with-agents)
    - [Use Context Engine with Agent Builder](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/use-context-engine-with-agent-builder)
  - [Query AI indices from LangChain](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/langchain-integration)
  - [Use Context Engine with Claude Code](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/use-context-engine-with-claude-code)
  - [Context Engine APIs](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/context-engine-api)
</important>

Use Context Engine with Claude Code to bring context distilled from your organization's data into coding sessions. By retrieving relevant [Knowledge Indicators (KIs)](/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/concepts#knowledge-indicators) from an [AI index](/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/concepts#ai-indices) instead of repeatedly discovering and interpreting source data, Claude Code can complete tasks faster and use fewer model tokens.
The Context Engine skill teaches Claude Code when and how to retrieve context. Claude Code runs the locally installed Elastic CLI, which calls the Context Engine APIs. This integration does not require an MCP server.
For guidance that applies to every agent integration, refer to [Configure agents to use an AI index](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/use-context-engine-with-agents). For the list, describe, and query flow, refer to [Retrieve context from an AI index](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/retrieve-context-from-ai-index).

## Before you begin

You need:
- An Elastic Stack 9.6 deployment with an Enterprise license, or an Elastic Cloud Serverless project.
- Claude Code installed.
- Node.js 22.12.0 or later to install the Elastic CLI.
- The `contextEngine:enabled` advanced setting turned on in the Kibana space you want to query.
- An AI index that contains at least one Knowledge Indicator (KI).
- The Context Engine **Read** privilege in that space.
- The Elasticsearch `read` and `view_index_metadata` privileges for the AI index's backing indices.
- Permission to create an API key with those Kibana and Elasticsearch privileges.


## Install the Elastic CLI

Install [Elastic CLI](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/4665/reference/elastic-cli/cli/installation) 0.7.0 or later globally so Claude Code can run it from its shell environment:
```bash
npm install -g @elastic/cli@latest
elastic --version
```

The Elastic CLI is in technical preview. The Context Engine command group is available in version 0.7.0 and later.

## Confirm the Context Engine commands

Run the following command in the same environment as Claude Code:
```bash
elastic kb context-engine --help
```

The command output should include operations for listing, describing, and querying AI indices. If the command group is unavailable, update the Elastic CLI before continuing.

## Create an API key

Create the API key that the Elastic CLI will use:
Create an API key with the required privileges:
1. In Kibana, open **Stack Management** → **API Keys**, then select **Create API key**.
2. Leave **Control security privileges** off and define a role with the following privileges:
   - **Index privileges**: `read` and `view_index_metadata` on `ai-index-*`.
- **Kibana privileges**: **Context Engine** at **Read** in the space you want to query.
   <dropdown title="Example role definition for the default space">
   ```json
   {
   "contextEngineRole": {
   "cluster": [],
   "indices": [
   {
   "names": [
   "ai-index-*"
   ],
   "privileges": [
   "read",
   "view_index_metadata"
   ],
   "field_security": {
   "grant": [
   "*"
   ],
   "except": []
   },
   "allow_restricted_indices": false
   }
   ],
   "applications": [
   {
   "application": "kibana-.kibana",
   "privileges": [
   "feature_contextEngine.read"
   ],
   "resources": [
   "space:default"
   ]
   }
   ],
   "run_as": [],
   "metadata": {},
   "transient_metadata": {
   "enabled": true
   }
   }
   }
   ```
   </dropdown>
3. Create the API key and copy its `encoded` value.


## Configure an Elastic CLI context

Add a named context with the Kibana URL and API key for the deployment:
```bash
elastic config context add context-engine \
  --kb-url https://my-deployment.kb.us-east-1.aws.elastic.cloud \
  --kb-api-key <api-key>
```

The Elastic CLI stores the API key in the operating system's credential store when one is available. Set the new context as the default, then verify that the CLI can connect:
```bash
elastic config current-context set context-engine
elastic status
```

To target a non-default Kibana space, include `/s/<space-id>` in the Kibana URL.

## Install the Context Engine skill

The [`kibana-context-engine` skill](https://github.com/elastic/agent-skills/tree/main/skills/kibana/kibana-context-engine) supplies the retrieval procedure and the Elastic CLI commands that Claude Code needs. The skill is included in the `elastic-kibana` plugin in the [Elastic agent skills repository](https://github.com/elastic/agent-skills).
Add the repository as a Claude Code marketplace, then install the `elastic-kibana` plugin. For more information about marketplaces and plugins, refer to [Install and manage plugins](https://code.claude.com/docs/en/discover-plugins) in the Claude Code documentation.
```bash
claude plugin marketplace add https://github.com/elastic/agent-skills
claude plugin install elastic-kibana@elastic-agent-skills
```

If the plugin is already installed, update it to the latest version:
```bash
claude plugin update elastic-kibana
```

After installing or updating the plugin, start a new Claude Code session. Run `/skills` and confirm that `kibana-context-engine` is available.

## Retrieve context

Ask a question covered by one of your AI indices. Tell Claude Code to use Context Engine if you want to exercise the integration explicitly. For example:
```text
Use Context Engine to explain <subject covered by your AI index>. Tell me which AI index and Knowledge Indicators you used.
```

Claude Code should load the skill and run these operations in order:
1. **List** the available AI indices and select one whose description matches the question.
2. **Describe** that AI index to obtain its ES|QL target, fields, KI types, tags, and example queries.
3. **Query** the AI index for relevant KIs and use the results to answer the question.

For the underlying retrieval sequence, refer to [Retrieve context from an AI index](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/retrieve-context-from-ai-index).

## Verify the retrieval

Review the commands Claude Code ran and confirm that it:
- Used `elastic kb context-engine` commands rather than querying the backing index directly.
- Listed the available AI indices before selecting one, unless the prompt already identified the AI index.
- Described the selected AI index before constructing the ES|QL query.
- Queried the target and fields returned by the describe operation.
- Used KIs from the expected AI index to answer the question.

The commands should correspond to the following operations:

| Operation            | Elastic CLI command                                                                             |
|----------------------|-------------------------------------------------------------------------------------------------|
| List AI indices      | `elastic kb context-engine get-context-engine-ai-index`                                         |
| Describe an AI index | `elastic kb context-engine get-context-engine-ai-index-aiindexid-describe --ai-index-id '<id>'` |
| Query AI indices     | `elastic kb context-engine post-context-engine-ai-index-query --query '<esql>'`                 |


## Troubleshoot the integration

Use the following table to resolve common setup and retrieval problems:

| Symptom                                                  | Resolution                                                                                                        |
|----------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------|
| Claude Code does not load `kibana-context-engine`.       | Confirm that the `elastic-kibana` plugin is installed and up to date, then start a new session and run `/skills`. |
| `elastic` is not found.                                  | Install the CLI globally and confirm that its executable is on the `PATH` available to Claude Code.               |
| `elastic kb context-engine` is not a recognized command. | Update the Elastic CLI to version 0.7.0 or later.                                                                 |

For additional API-level failures, refer to [Troubleshoot retrieval](/elastic/docs-builder/docs/4665/explore-analyze/ai-features/context-engine/retrieve-context-from-ai-index#troubleshoot-retrieval).

## Related Claude integrations

- The [Elastic plugin for Claude](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/claude-plugin) installs public Elastic agent skills and connects Claude to the Elastic Docs MCP server. It does not connect Claude to your deployment data.
- <applies-to>Elastic Cloud Serverless: Experimental</applies-to> The [Elastic Cloud Serverless connector for Claude](https://www.elastic.co/elastic/docs-builder/docs/4665/explore-analyze/ai-features/claude-connector) connects Claude on the web or desktop to the Elastic Agent Builder MCP server for a Elastic Cloud Serverless project. This is a separate access path from the local skill and Elastic CLI described on this page.