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.

Principle:Microsoft Semantic kernel Chat Completion Agent Creation

From Leeroopedia

Overview

Chat Completion Agent Creation is the foundational principle in Microsoft Semantic Kernel's agent framework that describes how a chat completion service is wrapped as an autonomous agent with a distinct personality and set of capabilities. By instantiating a ChatCompletionAgent, developers transform a raw language model endpoint into a named, instructed entity that can participate in conversations, use tools, and collaborate with other agents within an orchestration.

This principle belongs to Workflow 3: Agent Conversation and Orchestration and represents the first step in building any agent-based application with Semantic Kernel.

Description

The Agent Abstraction

In traditional usage of Semantic Kernel, a developer interacts with an IChatCompletionService directly, passing messages and receiving completions. The agent abstraction elevates this interaction by encapsulating:

  • Identity -- A Name property that distinguishes this agent from others in multi-agent scenarios.
  • Behavior -- An Instructions property (the system prompt) that defines the agent's personality, tone, constraints, and domain expertise.
  • Capabilities -- A Kernel instance that carries plugins, services, and configuration the agent can leverage during invocation.
  • Execution Settings -- An optional Arguments property containing KernelArguments with PromptExecutionSettings that control parameters such as temperature, max tokens, and tool-calling behavior.

Together, these properties create a self-contained unit of AI behavior. The agent knows who it is, knows what it should do, and has access to the tools it needs.

Why Wrap a Chat Completion as an Agent?

Wrapping a chat completion service as an agent provides several architectural benefits:

  1. Reusability -- An agent definition can be instantiated once and invoked repeatedly across different threads and orchestrations without reconfiguring the underlying service.
  2. Composability -- Agents with different specializations can be combined in multi-agent patterns (concurrent, group chat, handoff) because they share a common interface.
  3. Separation of Concerns -- The agent's identity and instructions are decoupled from the invocation logic, making it easier to modify behavior without changing calling code.
  4. Thread Safety -- Each agent invocation produces its own response stream, and thread state is managed externally, so the same agent instance can serve multiple conversations.

The Role of the Kernel

The Kernel property is not merely a service locator. It serves as the agent's equipment bag:

  • Plugins registered on the kernel become tools the agent can call via function calling.
  • Services registered on the kernel (such as IChatCompletionService) provide the underlying AI capability.
  • Filters registered on the kernel can intercept and modify agent behavior (logging, content safety, telemetry).

When the agent is invoked, it retrieves the chat completion service from its kernel, constructs the message history (prepending its Instructions as a system message), and delegates to the service for completion.

Usage

Chat Completion Agent Creation is used whenever an application needs:

  • A single agent that responds to user queries with a specific personality or domain focus.
  • Multiple specialized agents that each handle a different aspect of a workflow (e.g., a writer agent, a reviewer agent, a translator agent).
  • An agent that can call external tools through plugins while maintaining a coherent conversational identity.

Typical Creation Pattern

// 1. Build a kernel with the desired chat completion service
Kernel kernel = Kernel.CreateBuilder()
    .AddOpenAIChatCompletion("gpt-4o", apiKey)
    .Build();

// 2. Create the agent with identity and behavior
ChatCompletionAgent agent = new()
{
    Name = "Parrot",
    Instructions = "Repeat the user message in the voice of a pirate and then end with a parrot sound.",
    Kernel = kernel
};

The agent is now ready for invocation via InvokeAsync.

Advanced Creation with Execution Settings

ChatCompletionAgent agent = new()
{
    Name = "CreativeWriter",
    Instructions = "You are a creative fiction writer. Write vivid, imaginative prose.",
    Kernel = kernel,
    Arguments = new KernelArguments(
        new PromptExecutionSettings
        {
            ExtensionData = new Dictionary<string, object>
            {
                ["temperature"] = 0.9,
                ["max_tokens"] = 2048
            }
        })
};

Theoretical Basis

The Chat Completion Agent Creation principle draws from the Actor Model in concurrent computing, where each actor is an autonomous entity with:

  • Its own state (instructions, name, kernel configuration).
  • A defined interface for receiving messages (the InvokeAsync method).
  • The ability to send messages to other actors (via orchestration patterns).

It also reflects the Single Responsibility Principle from software engineering: each agent is responsible for one well-defined behavioral role, making the system easier to reason about and maintain.

In the context of AI systems, this pattern aligns with the concept of role prompting, where assigning a specific identity to a language model (via system instructions) has been shown to improve task adherence and output quality.

Related Pages

Implemented By

Uses Heuristic

Page Connections

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