Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Microsoft Semantic kernel IKernelBuilder Build

From Leeroopedia
Knowledge Sources
Domains AI_Orchestration, Object_Lifecycle_Management
Last Updated 2026-02-11 19:00 GMT

Overview

Concrete tool for materializing a configured kernel from the builder provided by the Microsoft Semantic Kernel library.

Description

IKernelBuilder.Build() is the terminal method in the kernel builder pipeline. It compiles all registered services, plugins, and configuration into a new Kernel instance backed by an IServiceProvider. After this call, the kernel is fully constructed and ready to invoke prompts, execute functions, and orchestrate AI workflows.

The method internally calls BuildServiceProvider() on the accumulated IServiceCollection, producing an IServiceProvider that is injected into the new Kernel object. The resulting kernel uses this provider to resolve AI services (such as IChatCompletionService), plugins, loggers, and any other registered dependencies at runtime.

Usage

Call Build() as the final step in the kernel configuration pipeline, after all AI services and plugins have been registered. This should typically happen once during application initialization. The returned kernel is thread-safe for concurrent prompt invocations and can be reused across the application's lifetime.

Code Reference

Source Location

  • Repository: semantic-kernel
  • File: dotnet/src/SemanticKernel.Core/KernelExtensions.cs:L1636

Signature

public Kernel Build()

Import

using Microsoft.SemanticKernel;

I/O Contract

Inputs

Name Type Required Description
(none) N/A N/A The method operates on the builder's internal state (accumulated service registrations). No explicit parameters are required.

Outputs

Name Type Description
return Kernel A fully constructed kernel instance with all registered services, plugins, and configuration resolved through its internal IServiceProvider.

Usage Examples

Basic Build

using Microsoft.SemanticKernel;

Kernel kernel = Kernel.CreateBuilder()
    .AddOpenAIChatClient(
        modelId: "gpt-4o",
        apiKey: "your-api-key")
    .Build();

Build with Intermediate Configuration

using Microsoft.SemanticKernel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

IKernelBuilder builder = Kernel.CreateBuilder();

// Register AI service
builder.AddOpenAIChatClient(
    modelId: "gpt-4o",
    apiKey: "your-api-key");

// Register logging
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Debug));

// Build the kernel
Kernel kernel = builder.Build();

// Kernel is now ready for use
Console.WriteLine(await kernel.InvokePromptAsync("Hello, world!"));

Building Multiple Kernels from Separate Builders

using Microsoft.SemanticKernel;

// Each builder produces an independent kernel
Kernel productionKernel = Kernel.CreateBuilder()
    .AddOpenAIChatClient(modelId: "gpt-4o", apiKey: "prod-key")
    .Build();

Kernel testKernel = Kernel.CreateBuilder()
    .AddOpenAIChatClient(modelId: "gpt-4o-mini", apiKey: "test-key")
    .Build();

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment