Use Context Engine with agents
Agents use Context Engine to retrieve reusable context from AI indices before spending time and model tokens interpreting source data. A Knowledge Indicator (KI) can answer a question directly or give the agent tested guidance for finding current details in the source.
You can use Context Engine with an Elastic Agent Builder agent or connect an agent built with another framework.
An agent retrieves context from an AI index in three stages:
- List the AI indices it can access and select one whose name and description match the question.
- Describe the selected AI index to learn what it contains, which KI types and fields are available, and how to query it.
- Query the AI index for relevant KIs.
The agent can answer from the retrieved KI when it contains enough information. If the question requires current or more detailed data, the KI can provide a verified query pattern or other guidance for reaching the source.
Access to an AI index and access to its source data are separate. The integration must provide both the Context Engine retrieval operations and any tools and permissions the agent needs to query the source.
| Approach | Use it when | How the agent accesses Context Engine |
|---|---|---|
| Elastic Agent Builder | You want to build and run the agent in Kibana. | Assign one or more AI indices to the agent. Elastic Agent Builder adds the Context Engine retrieval tools and describes the assigned indices in the agent's instructions. |
| LangChain | You are building an agent with LangChain or LangGraph. | Load the Context Engine tools through the Elastic Agent Builder MCP server, or wrap the Context Engine APIs as LangChain tools. |
Both approaches list and describe AI indices before querying them. The agent only discovers AI indices that its credentials can read in the current Kibana space.
The AI index name and description help the agent decide whether its KIs are relevant. Write them around the subject and questions the AI index supports, rather than the implementation that produced it.
Agent instructions can further define when to use the context, when to query source data, and how to communicate source limitations. Keep these instructions specific to the agent's task. The retrieval tools already tell the agent how to list, describe, and query AI indices.
Elastic Agent Builder also includes Context Engine skills for planning AI indices, configuring sources and automations, evaluating retrieval results, and retrieving KIs. These skills support Context Engine work, but they do not replace the AI index assignment or the tools required to access source data.
Test the agent with questions that exercise both paths:
- Ask a recurring question that a KI should answer directly.
- Ask for current or detailed information that requires the agent to follow the KI's guidance and query the source.
- Ask an out-of-scope question to confirm that the agent recognizes the AI index's limits.
Inspect the agent's tool calls to confirm that it selected the expected AI index, retrieved a relevant KI, and queried source data only when needed. If the generated context is incomplete or misleading, evaluate and improve the KIs instead of compensating with increasingly detailed agent instructions.