Kernel Builder Integration
UseClaudeCodeChatCompletion() is the primary extension point. It registers an
IChatCompletionService backed by Claude Code authentication into the Semantic Kernel service
collection.
Availability:
net8.0andnet10.0only.netstandard2.0targets can useAddClaudeCodeAuthenticationand build the kernel manually.
Overloads
Auto-resolved session (no arguments)
var kernel = Kernel.CreateBuilder()
.UseClaudeCodeChatCompletion()
.Build();
Reads credentials via the full resolution chain. Defaults to model
claude-sonnet-4-6.
Custom model
using JD.SemanticKernel.Connectors.ClaudeCode;
var kernel = Kernel.CreateBuilder()
.UseClaudeCodeChatCompletion(ClaudeModels.Opus)
.Build();
The modelId parameter is the first positional argument. Well-known constants are available via
ClaudeModels:
| Constant | Model ID | Description |
|---|---|---|
ClaudeModels.Opus |
claude-opus-4-6 |
Most capable, 1M context |
ClaudeModels.Sonnet |
claude-sonnet-4-6 |
Balanced (default) |
ClaudeModels.Haiku |
claude-haiku-4-5-20251001 |
Fastest, lowest cost |
ClaudeModels.Default |
claude-sonnet-4-6 |
Alias for Sonnet |
Explicit API key override
var kernel = Kernel.CreateBuilder()
.UseClaudeCodeChatCompletion(apiKey: "sk-ant-api...")
.Build();
Skips credential file and environment variable lookup entirely.
Explicit OAuth token override
var kernel = Kernel.CreateBuilder()
.UseClaudeCodeChatCompletion(apiKey: "sk-ant-oat...")
.Build();
The handler detects the sk-ant-oat prefix and switches to Bearer + CLI header authentication
automatically.
Custom options via delegate
var kernel = Kernel.CreateBuilder()
.UseClaudeCodeChatCompletion(configure: o =>
{
o.CredentialsPath = "/run/secrets/claude-credentials.json";
})
.Build();
The configure delegate receives a ClaudeCodeSessionOptions instance. It runs after apiKey
is applied, so you can combine both:
var kernel = Kernel.CreateBuilder()
.UseClaudeCodeChatCompletion(
modelId: "claude-opus-4-6",
apiKey: Environment.GetEnvironmentVariable("MY_CLAUDE_KEY"),
configure: o => o.CredentialsPath = "/custom/path")
.Build();
What the extension registers
Internally, UseClaudeCodeChatCompletion performs the following steps:
- Builds a
ClaudeCodeSessionOptionsfrom the supplied arguments. - Creates a
ClaudeCodeSessionProvider(not registered in DI — it is owned by the handler). - Creates an
HttpClientwithClaudeCodeSessionHttpHandleras the message handler. - Constructs an
AnthropicClientwith a placeholder key (the handler strips and replaces it at request time). - Wraps the client with
ChatClientBuilder(enabling automatic function invocation). - Calls
.AsChatCompletionService()to bridgeMicrosoft.Extensions.AI.IChatClient→ SK'sIChatCompletionService. - Registers the result as a singleton
IChatCompletionServiceinbuilder.Services.
Using the registered service
Once the kernel is built, retrieve the service via SK's standard APIs:
var chat = kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
history.AddUserMessage("Hello Claude!");
var response = await chat.GetChatMessageContentsAsync(history);
Or use InvokePromptAsync for simple one-shot calls:
var result = await kernel.InvokePromptAsync("Summarise the Liskov Substitution Principle.");
Chaining with other builder calls
UseClaudeCodeChatCompletion returns IKernelBuilder so it chains naturally:
var builder = Kernel.CreateBuilder()
.UseClaudeCodeChatCompletion(ClaudeModels.Sonnet, apiKey: myKey);
builder.Plugins.AddFromType<MyPlugin>();
var kernel = builder.Build();