# How AntSK Leverages Semantic Kernel for AI Orchestration: A Technical Deep Dive

> Explore how AntSK uses Semantic Kernel for AI orchestration. Discover its role in managing AI interactions, plugin execution, and function calling for RAG and native code.

- Repository: [AIDotNet/antsk](https://github.com/aidotnet/antsk)
- Tags: deep-dive
- Published: 2026-02-23

---

**AntSK uses Microsoft’s Semantic Kernel as a centralized orchestration layer to manage AI model interactions, plugin execution, and function calling through a request-scoped Kernel instance that coordinates chat completion, RAG operations, and native code execution.**

AntSK is an open-source AI knowledge base and intelligent agent platform built on .NET. By leveraging Semantic Kernel, AntSK creates a unified architecture where all AI operations—from chat streaming to complex function orchestration—flow through a single, configurable Kernel instance. This design pattern enables developers to build extensible AI agents simply by registering custom plugins and native functions with the kernel, as implemented in the `aidotnet/antsk` repository.

## Core Orchestration Architecture

The orchestration pipeline in AntSK follows a layered architecture where the Semantic Kernel acts as the single source of truth for all AI interactions. When a user sends a chat message, the system instantiates a fresh Kernel instance configured for that specific application context.

The flow proceeds through four distinct phases:

- **Kernel Creation**: `KernelService` constructs a `Microsoft.SemanticKernel.Kernel` using `KernelBuilder` and configures the AI model (OpenAI, Azure OpenAI, or DashScope).
- **Plugin Registration**: Custom functions defined in user-created Apps are imported via `ImportFunctionsByApp`, `ImportApiFunction`, and `ImportNativeFunction`.
- **Chat Handling**: `ChatService` forwards `ChatHistory` to the kernel’s `ChatCompletion` service, enabling automatic function invocation when the model requests it.
- **Post-Processing**: Conversation summaries are generated using `ITextGenerationService` and stored in vector stores via Semantic Kernel’s text embedding utilities.

## Kernel Initialization and Configuration

The foundation of AntSK’s orchestration lies in [`KernelService.cs`](https://github.com/aidotnet/antsk/blob/main/KernelService.cs), where the `BuildKernel` method constructs a fully configured Kernel instance. This method registers the AI model, adds core plugins from `Microsoft.SemanticKernel.Plugins.Core`, and prepares the dependency injection container for function execution.

```csharp
// From KernelService.cs - simplified kernel construction
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Core;

public class KernelService : IKernelService
{
    public Kernel BuildKernel(Apps app)
    {
        var builder = new KernelBuilder();
        
        // Configure the AI model (Azure OpenAI, OpenAI, or DashScope)
        builder.WithCompletionService(app.ModelName, new AzureOpenAIChatCompletion(
            endpoint: app.Endpoint, 
            apiKey: app.ApiKey));
        
        // Register core plugins for time, memory, and embeddings
        builder.Plugins.AddFromType<TimePlugin>();
        builder.Plugins.AddFromType<TextMemoryPlugin>();
        
        var kernel = builder.Build();
        
        // Import user-defined functions for this specific App
        ImportFunctionsByApp(kernel, app);
        return kernel;
    }
}

```

According to the AntSK source code, the `KernelBuilder` pattern allows each request to receive an isolated Kernel instance with its own plugin context and model configuration. This ensures that multi-tenant applications maintain strict separation between different AI apps and their associated functions.

## Plugin Management and Function Registration

AntSK extends Semantic Kernel’s capabilities by dynamically importing functions defined within user-created Apps. The `ImportNativeFunction` method in [`KernelService.cs`](https://github.com/aidotnet/antsk/blob/main/KernelService.cs) uses reflection to convert .NET methods into `KernelFunction` instances that the AI can invoke.

```csharp
// From KernelService.cs - native function registration
public void ImportNativeFunction(Apps app, List<KernelFunction> functions)
{
    foreach (var func in app.NativeFunctions)
    {
        var kernelFunc = KernelFunctionFactory.CreateFromMethod(
            func.MethodInfo,
            description: func.Description);
        functions.Add(kernelFunc);
    }

    // Register the collected functions as a plugin named after the App
    foreach (var kf in functions)
        kernel.ImportPluginFromFunctions(new[] { kf }, app.Name);
}

```

For API-based functions, `ImportApiFunction` handles OpenAPI specifications, converting external REST endpoints into callable Kernel functions. This mechanism allows AntSK agents to interact with third-party services without writing custom integration code, as the Semantic Kernel automatically handles parameter mapping and JSON serialization.

## Chat Orchestration and Function Calling

The [`ChatService.cs`](https://github.com/aidotnet/antsk/blob/main/ChatService.cs) file implements the core chat orchestration logic through the `SendChatByAppAsync` method. This service coordinates between the Blazor frontend and the Semantic Kernel, managing `ChatHistory` objects and streaming responses back to the user.

```csharp
// From ChatService.cs - chat orchestration
public async Task<ChatResult> SendChatByAppAsync(Apps app, ChatHistory history)
{
    var kernel = _kernelService.BuildKernel(app);
    var chat = kernel.GetService<IChatCompletionService>();
    
    // Execute chat with automatic function calling enabled
    var response = await chat.GetChatMessageAsync(
        history,
        new OpenAIChatCompletionOptions 
        { 
            Temperature = 0.7,
            ToolChoice = "auto"  // Enable automatic function invocation
        });
    
    return new ChatResult
    {
        Message = response,
        UpdatedHistory = history
    };
}

```

When the AI model decides to invoke a function, Semantic Kernel automatically executes the registered native or API function and feeds the result back into the conversation context. This happens transparently within the `GetChatMessageAsync` call, allowing AntSK to support complex multi-step agent workflows without manual intervention in the controller layer.

## Conversation Summarization and RAG Integration

AntSK leverages Semantic Kernel’s text generation and embedding capabilities to implement Retrieval-Augmented Generation (RAG) and conversation management. The `HistorySummarize` method in [`KernelService.cs`](https://github.com/aidotnet/antsk/blob/main/KernelService.cs) uses `ITextGenerationService` to create concise summaries of long chat sessions.

```csharp
// From KernelService.cs - conversation summarization
public async Task<string> HistorySummarize(Kernel kernel, string chatId, string language)
{
    var summarizer = kernel.GetService<ITextGenerationService>();
    var prompt = $"Summarize the following conversation in {language}:\n{{CHAT_TEXT}}";
    
    var summary = await summarizer.GenerateAsync(prompt);
    return summary;
}

```

These summaries are then stored in vector stores using the `TextEmbeddingGeneration` service, enabling semantic search across historical conversations. The [`KMSController.cs`](https://github.com/aidotnet/antsk/blob/main/KMSController.cs) exposes these capabilities through HTTP endpoints, keeping the web layer thin while delegating all AI logic to the kernel services.

## Summary

- **AntSK instantiates a fresh Semantic Kernel per request** via `KernelService.BuildKernel()`, ensuring isolated execution contexts for different AI applications.
- **Dynamic plugin registration** allows .NET methods and OpenAPI endpoints to become callable AI functions through `ImportNativeFunction` and `ImportApiFunction`.
- **Automatic function calling** is handled entirely by Semantic Kernel’s `ChatCompletion` service, enabling complex agent workflows without manual orchestration code.
- **RAG and summarization** leverage `ITextGenerationService` and `TextEmbeddingGeneration` to maintain context across long-running conversations.
- **Thin controller architecture** in [`KMSController.cs`](https://github.com/aidotnet/antsk/blob/main/KMSController.cs) delegates all AI operations to kernel services, following the Single Responsibility Principle.

## Frequently Asked Questions

### How does AntSK initialize the Semantic Kernel for each chat session?

AntSK creates a new Kernel instance for each request through the `BuildKernel` method in [`KernelService.cs`](https://github.com/aidotnet/antsk/blob/main/KernelService.cs). This method uses `KernelBuilder` to configure the specific AI model (OpenAI, Azure, or DashScope) and registers only the plugins relevant to the current App context. This per-request instantiation ensures that different users and applications maintain complete isolation while still benefiting from Semantic Kernel’s centralized orchestration capabilities.

### What mechanism allows AntSK to convert custom code into AI-callable functions?

The platform uses the `ImportNativeFunction` method in [`KernelService.cs`](https://github.com/aidotnet/antsk/blob/main/KernelService.cs) to reflect over .NET methods and convert them into `KernelFunction` instances using `KernelFunctionFactory.CreateFromMethod()`. These functions are then registered with the kernel via `ImportPluginFromFunctions()`, making them available for automatic invocation when the AI model generates a function call request during chat completion.

### Can AntSK handle multiple AI providers simultaneously?

Yes. The `KernelBuilder` configuration in [`KernelService.cs`](https://github.com/aidotnet/antsk/blob/main/KernelService.cs) supports multiple connectors including Azure OpenAI, standard OpenAI, and DashScope (Alibaba Cloud). Each App can specify its own model configuration, and the `BuildKernel` method instantiates the appropriate `IChatCompletionService` and `ITextGenerationService` implementations based on the App’s stored credentials and endpoint settings.

### How does conversation summarization work in AntSK?

The `HistorySummarize` method retrieves the `ITextGenerationService` from the kernel and prompts it to condense chat history into a concise summary. This summary is then embedded using Semantic Kernel’s `TextEmbeddingGeneration` service and stored in a vector database. This RAG pattern allows AntSK to maintain context in long conversations without exceeding token limits, as older messages can be replaced by their semantic summaries.