Edit

Create a knowledge base in Azure AI Search

Note

This agentic retrieval feature is generally available in the 2026-04-01 REST API via programmatic access. The Azure portal and Microsoft Foundry portal continue to provide preview-only access to all agentic retrieval features. For migration guidance, see Migrate agentic retrieval code to the latest version.

If you choose to use a preview REST API, you can access capabilities that aren't yet generally available for this feature. Preview features are provided without a service-level agreement and aren't recommended for production workloads. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.

Important

These features and functionality are part of the 2026-05-01-preview REST API. The 2026-05-01-preview is licensed to you as part of your Azure subscription and is subject to the terms applicable to "Previews" in the Microsoft Product Terms, the Microsoft Products and Services Data Protection Addendum ("DPA"), and the Supplemental Terms of Use for Microsoft Azure Previews.

The 2026-05-01-preview supports connections to other Microsoft services and third-party services. Use of these services is subject to their respective terms and might result in data processing or storage outside of the Azure compliance boundary, as well as data flowing into the Azure compliance boundary.

It's your responsibility to manage whether your data will flow outside of your organization's compliance and geographic boundaries and any related implications, and that appropriate permissions, boundaries, and approvals are provisioned.

You're responsible for carefully reviewing and testing applications you build in the context of your specific use cases and making all appropriate decisions and customizations. This includes implementing your own responsible AI mitigations, such as metaprompts, content filters, or other safety systems, and ensuring your applications meet appropriate quality, reliability, security, and trustworthiness standards. For more information, see the Azure AI Search Transparency Note.

In Azure AI Search, a knowledge base is a top-level object that orchestrates agentic retrieval. It defines which knowledge sources to query and the default behavior for retrieval operations. At query time, the retrieve method targets the knowledge base to run the configured retrieval pipeline.

You can create a knowledge base in a Foundry IQ workload in the Microsoft Foundry (new) portal. You also need a knowledge base in any agentic solutions that you create using the Azure AI Search APIs.

A knowledge base specifies:

  • One or more knowledge sources that point to searchable content.
  • An optional LLM for query planning, answer synthesis, or web content summarization. Supported tasks vary by API version and knowledge source type.
  • Custom properties that control routing, source selection, and object encryption.

Usage support

Azure portal Microsoft Foundry portal .NET SDK Python SDK Java SDK JavaScript SDK REST API
✔️ ✔️ ✔️ ✔️ ✔️ ✔️ ✔️

Prerequisites

  • Azure AI Search in any region that provides agentic retrieval. If you're using a managed identity for role-based access to deployed models, your search service must be on the Basic tier or higher.

  • One or more knowledge sources. Use the 2026-05-01-preview API version for preview knowledge source types or to use an LLM with non-web knowledge sources. Use the 2026-04-01 API version for generally available knowledge source types and minimal, extractive retrieval.

  • (Conditional) Azure OpenAI with a supported LLM deployment. An LLM is required if your knowledge base includes a web knowledge source. For other knowledge source types, an LLM is optional in the 2026-05-01-preview API version and unsupported in the 2026-04-01 API version.

  • Permissions to create knowledge bases. Configure keyless authentication with the Search Service Contributor role assigned to your user account (recommended) or use an API key.

  • If the knowledge base specifies an LLM, the search service must have a managed identity with Cognitive Services User permissions on the Microsoft Foundry resource.

  • Required Azure.Search.Documents package:

    • For 2026-05-01-preview features, the latest preview package: dotnet add package Azure.Search.Documents --prerelease

    • For 2026-04-01 features, the latest stable package: dotnet add package Azure.Search.Documents

  • Required azure-search-documents package:

    • For 2026-05-01-preview features, the latest preview package: pip install --pre azure-search-documents

    • For 2026-04-01 features, the latest stable package: pip install azure-search-documents

Supported models

Use one of the following LLMs from Azure OpenAI in Foundry Models. Regional availability is determined by Azure OpenAI for the deployment you select. For deployment instructions, see Deploy Microsoft Foundry Models in the Foundry portal.

Model Supported API versions
gpt-4o 2025-11-01-preview, 2026-05-01-preview
gpt-4o-mini 2025-11-01-preview, 2026-05-01-preview
gpt-4.1 2025-11-01-preview, 2026-05-01-preview
gpt-4.1-mini 2025-11-01-preview, 2026-05-01-preview
gpt-4.1-nano 2025-11-01-preview, 2026-05-01-preview
gpt-5 2025-11-01-preview, 2026-05-01-preview
gpt-5-mini 2025-11-01-preview, 2026-05-01-preview
gpt-5-nano 2025-11-01-preview, 2026-05-01-preview
gpt-5.1 2026-05-01-preview
gpt-5.2 2026-05-01-preview
gpt-5.4 2026-05-01-preview
gpt-5.4-mini 2026-05-01-preview
gpt-5.4-nano 2026-05-01-preview

Configure access

Azure AI Search needs access to the LLM from Azure OpenAI in Foundry Models. We recommend Microsoft Entra ID for authentication and role-based access for authorization. To assign roles, you must be an Owner or User Access Administrator. If you can't use roles, use key-based authentication instead.

  1. Configure Azure AI Search to use a managed identity.

  2. On your model provider, assign Cognitive Services User to the managed identity of your search service. If you're testing locally, assign the same role to your user account.

  3. For local testing, follow the steps in Quickstart: Connect without keys to sign in to a specific subscription and tenant. Use DefaultAzureCredential instead of AzureKeyCredential in each request, which should look similar to the following example:

    // Authenticate using roles
    using Azure.Search.Documents.Indexes;
    using Azure.Identity;
    
    var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new DefaultAzureCredential());
    
  1. Configure Azure AI Search to use a managed identity.

  2. On your model provider, assign Cognitive Services User to the managed identity of your search service. If you're testing locally, assign the same role to your user account.

  3. For local testing, follow the steps in Quickstart: Connect without keys to sign in to a specific subscription and tenant. Use DefaultAzureCredential instead of AzureKeyCredential in each request, which should look similar to the following example:

    # Authenticate using roles
    from azure.identity import DefaultAzureCredential
    index_client = SearchIndexClient(endpoint = "search_url", credential = DefaultAzureCredential())
    
  1. Configure Azure AI Search to use a managed identity.

  2. On your model provider, assign Cognitive Services User to the managed identity of your search service. If you're testing locally, assign the same role to your user account.

  3. For local testing, follow the steps in Quickstart: Connect without keys to get a personal access token for a specific subscription and tenant. Specify your access token in each request, which should look similar to the following example:

    # List indexes using roles
    GET https://{{search-url}}/indexes?api-version=2026-04-01
    Content-Type: application/json
    Authorization: Bearer {{access-token}}
    

Important

Code snippets in this article use API keys. If you use role-based authentication, update each request accordingly. In a request that specifies both approaches, the API key takes precedence.

Check for existing knowledge bases

A knowledge base is a top-level, reusable object. Knowing about existing knowledge bases is helpful for either reuse or naming new objects.

Run the following code to list existing knowledge bases by name. The list includes all knowledge bases on your search service, regardless of which API version you used to create them.

// List knowledge bases by name
using Azure.Search.Documents.Indexes;

var indexClient = new SearchIndexClient(new Uri(searchEndpoint), credential);
var knowledgeBases = indexClient.GetKnowledgeBasesAsync();

Console.WriteLine("Knowledge Bases:");

await foreach (var kb in knowledgeBases)
{
    Console.WriteLine($"  - {kb.Name}");
}

Reference: SearchIndexClient

# List knowledge bases by name
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient

index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))

for kb in index_client.list_knowledge_bases():
    print(f"  - {kb.name}")

Reference: SearchIndexClient

# List knowledge bases
GET {{search-url}}/knowledgebases?api-version={{api-version}}&$select=name
Content-Type: application/json
api-key: {{search-api-key}}

Reference: Knowledge Bases - List

You can also return a single knowledge base by name to review its JSON definition.

using Azure.Search.Documents.Indexes;
using System.Text.Json;

var indexClient = new SearchIndexClient(new Uri(searchEndpoint), credential);

// Specify the knowledge base name to retrieve
string kbNameToGet = "earth-knowledge-base";

// Get a specific knowledge base definition
var knowledgeBaseResponse = await indexClient.GetKnowledgeBaseAsync(kbNameToGet);
var kb = knowledgeBaseResponse.Value;

// Serialize to JSON for display
string json = JsonSerializer.Serialize(kb, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json);

Reference: SearchIndexClient

# Get a knowledge base definition
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
import json

index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))

kb = index_client.get_knowledge_base("knowledge_base_name")
print(json.dumps(kb.as_dict(), indent = 2))

Reference: SearchIndexClient

# Get knowledge base
GET {{search-url}}/knowledgebases/{{knowledge-base-name}}?api-version={{api-version}}
Content-Type: application/json
api-key: {{search-api-key}}

Reference: Knowledge Bases - Get

The following JSON is an example response for a knowledge base.

{
  "name": "my-kb",
  "description": "A sample knowledge base.",
  "retrievalInstructions": null,
  "answerInstructions": null,
  "outputMode": null,
  "knowledgeSources": [
    {
      "name": "my-blob-ks"
    }
  ],
  "models": [],
  "encryptionKey": null,
  "retrievalReasoningEffort": {
    "kind": "low"
  }
}

Note

The response schema reflects the API version you used to create the knowledge base. A knowledge base created with the generally available 2026-04-01 API version returns a narrower definition than the 2026-05-01-preview. For more information about which properties each version supports, see the next section.

Create a knowledge base

Important

The 2026-04-01 API version only accepts generally available knowledge source types and supports minimal, extractive retrieval. Preview-only capabilities, such as query planning, answer synthesis, and configurable reasoning effort, aren't supported. For full functionality, use the 2026-05-01-preview.

A knowledge base connects one or more knowledge sources (searchable content) to an optional LLM from Azure OpenAI in Foundry Models. The properties you set establish defaults for query execution and the retrieval response.

After you create a knowledge base, you can update its properties at any time. If the knowledge base is in use, updates take effect on the next retrieval.

// Create a knowledge base
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Azure.Search.Documents.KnowledgeBases.Models;
using Azure;

var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new AzureKeyCredential(apiKey));

var aoaiParams = new AzureOpenAIVectorizerParameters
{
    ResourceUri = new Uri(aoaiEndpoint),
    DeploymentName = aoaiGptDeployment,
    ModelName = aoaiGptModel,
    ApiKey = aoaiApiKey
};

var knowledgeBase = new KnowledgeBase(
    name: "my-kb",
    knowledgeSources: new KnowledgeSourceReference[] 
    { 
        new KnowledgeSourceReference("hotels-ks"),
        new KnowledgeSourceReference("earth-at-night-ks")
    }
)
{
    Description = "This knowledge base handles questions directed at two unrelated sample indexes.",
    RetrievalInstructions = "Use the hotels knowledge source for queries about where to stay, otherwise use the earth at night knowledge source.",
    AnswerInstructions = "Provide a two sentence concise and informative answer based on the retrieved documents.",
    OutputMode = KnowledgeRetrievalOutputMode.AnswerSynthesis,
    Models = { new KnowledgeBaseAzureOpenAIModel(azureOpenAIParameters: aoaiParams) },
    RetrievalReasoningEffort = new KnowledgeRetrievalLowReasoningEffort()
};

await indexClient.CreateOrUpdateKnowledgeBaseAsync(knowledgeBase);
Console.WriteLine($"Knowledge base '{knowledgeBase.Name}' created or updated successfully.");

Reference: SearchIndexClient, KnowledgeBase

# Create a knowledge base
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
    AzureOpenAIVectorizerParameters,
    KnowledgeBase,
    KnowledgeBaseAzureOpenAIModel,
    KnowledgeSourceReference,
)
from azure.search.documents.knowledgebases.models import KnowledgeRetrievalLowReasoningEffort

index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))

aoai_params = AzureOpenAIVectorizerParameters(
    resource_url = "aoai_endpoint",
    api_key="aoai_api_key",
    deployment_name = "aoai_gpt_deployment",
    model_name = "aoai_gpt_model",
)

knowledge_base = KnowledgeBase(
    name = "my-kb",
    description = "This knowledge base handles questions directed at two unrelated sample indexes.",
    retrieval_instructions = "Use the hotels knowledge source for queries about where to stay, otherwise use the earth at night knowledge source.",
    answer_instructions = "Provide a two sentence concise and informative answer based on the retrieved documents.",
    output_mode = "answerSynthesis",
    knowledge_sources = [
        KnowledgeSourceReference(name = "hotels-ks"),
        KnowledgeSourceReference(name = "earth-at-night-ks"),
    ],
    models = [KnowledgeBaseAzureOpenAIModel(azure_open_ai_parameters = aoai_params)],
    encryption_key = None,
    retrieval_reasoning_effort = KnowledgeRetrievalLowReasoningEffort(),
)

index_client.create_or_update_knowledge_base(knowledge_base)
print(f"Knowledge base '{knowledge_base.name}' created or updated successfully.")

Reference: SearchIndexClient, KnowledgeBase

# Create a knowledge base
PUT {{search-url}}/knowledgebases/{{knowledge-base-name}}?api-version=2026-05-01-preview
Content-Type: application/json
api-key: {{search-api-key}}

{
    "name" : "my-kb",
    "description": "This knowledge base handles questions directed at two unrelated sample indexes.",
    "retrievalInstructions": "Use the hotels knowledge source for queries about where to stay, otherwise use the earth at night knowledge source.",
    "answerInstructions": null,
    "outputMode": "answerSynthesis",
    "knowledgeSources": [
        {
            "name": "hotels-ks"
        },
        {
            "name": "earth-at-night-ks"
        }
    ],
    "models" : [ 
        {
            "kind": "azureOpenAI",
            "azureOpenAIParameters": {
                "resourceUri": "{{model-provider-url}}",
                "apiKey": "{{model-api-key}}",
                "deploymentId": "gpt-5.4-mini",
                "modelName": "gpt-5.4-mini"
            }
        }
    ],
    "encryptionKey": null,
    "retrievalReasoningEffort": {
        "kind": "low"
    }
}

Reference: Knowledge Bases - Create or Update

Configure CORS for browser-based retrieve calls (preview)

Important

You can use the 2026-05-01-preview to enable cross-origin resource sharing (CORS), which allows browser-based applications to request data directly from the service. Depending on your CORS configuration, external web pages might be able to access or invoke the service and its data using the user's browser context, as well as create other security threats. Enabling CORS is at your own risk.

In the 2026-05-01-preview API, a knowledge base can define corsOptions for browser-based applications that call the retrieve action directly from JavaScript. The CORS policy identifies which browser origins can send retrieve requests to the knowledge base.

The following examples create a knowledge base that allows retrieve requests from one browser origin.

using Azure;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;

var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new AzureKeyCredential(apiKey));

var knowledgeBase = new KnowledgeBase(
    name: "browser-chat-kb",
    knowledgeSources: new[] { new KnowledgeSourceReference("product-docs-ks") }
)
{
    Description = "A knowledge base that allows one browser app origin.",
    CorsOptions = new CorsOptions(new[] { "https://myapp.example.com" })
    {
        MaxAgeInSeconds = 300
    }
};

await indexClient.CreateOrUpdateKnowledgeBaseAsync(knowledgeBase);

Reference: CorsOptions, KnowledgeBase

from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
    CorsOptions,
    KnowledgeBase,
    KnowledgeSourceReference,
)

index_client = SearchIndexClient(endpoint="search_url", credential=AzureKeyCredential("api_key"))

knowledge_base = KnowledgeBase(
    name="browser-chat-kb",
    description="A knowledge base that allows one browser app origin.",
    knowledge_sources=[KnowledgeSourceReference(name="product-docs-ks")],
    cors_options=CorsOptions(
        allowed_origins=["https://myapp.example.com"],
        max_age_in_seconds=300,
    ),
)

index_client.create_or_update_knowledge_base(knowledge_base)

Reference: CorsOptions, KnowledgeBase

PUT {{search-url}}/knowledgebases/browser-chat-kb?api-version=2026-05-01-preview
Content-Type: application/json
api-key: {{search-api-key}}

{
  "name": "browser-chat-kb",
  "description": "A knowledge base that allows one browser app origin.",
  "knowledgeSources": [
    {
      "name": "product-docs-ks"
    }
  ],
  "corsOptions": {
    "allowedOrigins": [
      "https://myapp.example.com"
    ],
    "maxAgeInSeconds": 300
  }
}

When corsOptions is present, allowedOrigins lists the origins that can call the knowledge base from a browser. maxAgeInSeconds is optional and controls how long the browser can cache the preflight response.

If corsOptions is omitted, the knowledge base has no CORS policy and browsers block cross-origin retrieve requests. Set allowedOrigins to an explicit list of browser origins for production applications. You can use "*" to allow all origins, but this setting isn't recommended for production. If maxAgeInSeconds is omitted, the preflight cache duration defaults to 300 seconds. CORS applies to knowledge base retrieve REST API calls from browsers.

Knowledge base properties

Pass the following properties to create a knowledge base.

Name Description Type Required
Name The name of the knowledge base. It must be unique within the knowledge bases collection and follow the naming guidelines for objects in Azure AI Search. String Yes
KnowledgeSources One or more supported knowledge sources. Array Yes
Description A description of the knowledge base. The LLM uses the description to inform query planning. String No
RetrievalInstructions A prompt for the LLM to determine whether a knowledge source should be in scope for a query. Include this prompt when you have multiple knowledge sources. This field influences both knowledge source selection and query formulation. For example, instructions could append information or prioritize a knowledge source. Instructions are passed directly to the LLM, which means it's possible to provide instructions that break query planning, such as instructions that result in bypassing an essential knowledge source. String No
AnswerInstructions Custom instructions to shape synthesized answers. The default is null. For more information, see Use answer synthesis for citation-backed responses. String No
OutputMode Valid values are AnswerSynthesis for an LLM-formulated answer or ExtractedData for full search results that you can pass to an LLM as a downstream step. String No
Models Required for web knowledge sources. Optional for other knowledge source types. Specifies a supported LLM for query planning or answer synthesis. Get connection details from the Microsoft Foundry portal or a command-line request, and then provide them by using the KnowledgeBaseAzureOpenAIModel class. You can use role-based access control instead of API keys for the Azure AI Search connection to the model. Array No
RetrievalReasoningEffort Determines the level of LLM-related query processing. Valid values are minimal, low (default), and medium. For more information, see Set the retrieval reasoning effort. Object No
Name Description Type Required
name The name of the knowledge base. It must be unique within the knowledge bases collection and follow the naming guidelines for objects in Azure AI Search. String Yes
description A description of the knowledge base. The LLM uses the description to inform query planning. String No
retrieval_instructions A prompt for the LLM to determine whether a knowledge source should be in scope for a query. Include this prompt when you have multiple knowledge sources. This field influences both knowledge source selection and query formulation. For example, instructions could append information or prioritize a knowledge source. Pass instructions directly to the LLM. It's possible to provide instructions that break query planning, such as instructions that result in bypassing an essential knowledge source. String No
answer_instructions Custom instructions to shape synthesized answers. The default is null. For more information, see Use answer synthesis for citation-backed responses. String No
output_mode Valid values are answerSynthesis for an LLM-formulated answer or extractedData for full search results that you can pass to an LLM as a downstream step. String No
knowledge_sources One or more supported knowledge sources. Array Yes
models Required for web knowledge sources. Optional for other knowledge source types. Specifies a supported LLM for query planning or answer synthesis. Get connection details from the Microsoft Foundry portal or a command-line request. You can use role-based access control instead of API keys for the Azure AI Search connection to the model. Array No
encryption_key A customer-managed key to encrypt sensitive information in both the knowledge base and the generated objects. Object No
retrieval_reasoning_effort Determines the level of LLM-related query processing. Valid values are minimal, low (default), and medium. For more information, see Set the retrieval reasoning effort. Object No
Name Description Type Required
name The name of the knowledge base. It must be unique within the knowledge bases collection and follow the naming guidelines for objects in Azure AI Search. String Yes
description A description of the knowledge base. The LLM uses the description to inform query planning. String No
retrievalInstructions A prompt for the LLM to determine whether a knowledge source should be in scope for a query. Include this prompt when you have multiple knowledge sources. This field influences both knowledge source selection and query formulation. For example, instructions could append information or prioritize a knowledge source. You pass instructions directly to the LLM, which means it's possible to provide instructions that break query planning, such as instructions that result in bypassing an essential knowledge source. String No
answerInstructions Custom instructions to shape synthesized answers. The default is null. For more information, see Use answer synthesis for citation-backed responses. String No
outputMode Valid values are answerSynthesis for an LLM-formulated answer or extractedData for full search results that you can pass to an LLM as a downstream step. String No
knowledgeSources One or more supported knowledge sources. Array Yes
models Required for web knowledge sources. Optional for other knowledge source types. Specifies a supported LLM for query planning or answer synthesis. Get connection details from the Microsoft Foundry portal or a command-line request. You can use role-based access control instead of API keys for the Azure AI Search connection to the model. Array No
encryptionKey A customer-managed key to encrypt sensitive information in both the knowledge base and the generated objects. Object No
retrievalReasoningEffort.kind Determines the level of LLM-related query processing. Valid values are minimal, low (default), and medium. For more information, see Set the retrieval reasoning effort. Object No

Query a knowledge base

After you create a knowledge base, use the retrieve action to query it and verify the LLM connection.

Delete a knowledge base

If you no longer need the knowledge base or need to rebuild it on your search service, run the following code to delete the object.

// Delete a knowledge base
using Azure.Search.Documents.Indexes;
var indexClient = new SearchIndexClient(new Uri(searchEndpoint), credential);

await indexClient.DeleteKnowledgeBaseAsync(knowledgeBaseName);
System.Console.WriteLine($"Knowledge base '{knowledgeBaseName}' deleted successfully.");

Reference: SearchIndexClient

# Delete a knowledge base
from azure.core.credentials import AzureKeyCredential 
from azure.search.documents.indexes import SearchIndexClient

index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))
index_client.delete_knowledge_base("knowledge_base_name")
print(f"Knowledge base deleted successfully.")

Reference: SearchIndexClient

# Delete a knowledge base
DELETE {{search-url}}/knowledgebases/{{knowledge-base-name}}?api-version={{api-version}}
api-key: {{search-api-key}}

Reference: Knowledge Bases - Delete