Semantic Kernel Integration
Integrate CoreValue AI Gateway with Microsoft Semantic Kernel to access LLM providers with unified observability.
Introduction
Semantic Kernel is Microsoft's open-source SDK for building AI agents and orchestrating LLM workflows across multiple languages (.NET, Python, Java). By integrating CoreValue AI Gateway with Semantic Kernel, you can:
- Route to different models & providers with automatic failover through a single endpoint
- Unified billing with pass-through billing or bring your own keys
- Monitor all requests with automatic cost tracking in one dashboard
This integration requires only one line change to your existing Semantic Kernel code - adding the AI Gateway endpoint.
Integration Steps
Sign up at corevalue.dev and generate an API key.
You'll also need to configure your provider API keys (OpenAI, Anthropic, etc.) at CoreValue Providers for BYOK (Bring Your Own Keys).
# Your CoreValue API key
export COVA_API_KEY=<your-corevalue-api-key>Create a .env file in your project:
COVA_API_KEY=sk-cova-XXXXXXXXXXXXXXXXusing Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using DotNetEnv;
// Load environment variables
Env.Load();
var corevalueApiKey = Environment.GetEnvironmentVariable("COVA_API_KEY");
// Create kernel builder
var builder = Kernel.CreateBuilder();
// Add OpenAI chat completion with CoreValue AI Gateway endpoint
builder.AddOpenAIChatCompletion(
modelId: "gpt-4.1-mini", // Any model from CoreValue registry
apiKey: corevalueApiKey, // Your CoreValue API key
endpoint: new Uri("https://gateway.corevalue.dev/v1") // CoreValue AI Gateway
);
var kernel = builder.Build();import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
import os
# Load environment variables
corevalue_api_key = os.getenv("COVA_API_KEY")
# Create kernel
kernel = sk.Kernel()
# Add OpenAI chat completion with CoreValue AI Gateway endpoint
kernel.add_service(
OpenAIChatCompletion(
service_id="corevalue-gateway",
ai_model_id="gpt-4.1-mini", # Any model from CoreValue registry
api_key=corevalue_api_key, # Your CoreValue API key
endpoint="https://gateway.corevalue.dev/v1" # CoreValue AI Gateway
)
)The only change from a standard Semantic Kernel setup is adding the endpoint parameter. Everything else stays the same!
Your existing Semantic Kernel code continues to work without any changes:
using Microsoft.SemanticKernel.ChatCompletion;
// Get the chat service
var chatService = kernel.GetRequiredService<IChatCompletionService>();
// Create chat history
var chatHistory = new ChatHistory();
chatHistory.AddUserMessage("What is the capital of France?");
// Get response
var response = await chatService.GetChatMessageContentAsync(chatHistory);
Console.WriteLine(response.Content);from semantic_kernel.contents import ChatHistory
# Get the chat service
chat_service = kernel.get_service("corevalue-gateway")
# Create chat history
chat_history = ChatHistory()
chat_history.add_user_message("What is the capital of France?")
# Get response
response = await chat_service.get_chat_message_content(
chat_history=chat_history
)
print(response.content)All your Semantic Kernel requests are now visible in your CoreValue dashboard:
- Request/response bodies
- Latency metrics
- Token usage and costs
- Model performance analytics
- Error tracking
While you're here, why not give us a star on GitHub? It helps us a lot!
Migration Example
Here's what migrating an existing Semantic Kernel application looks like:
Before (Direct OpenAI)
var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion(
modelId: "gpt-4o-mini",
apiKey: openAiApiKey
);
var kernel = builder.Build();After (CoreValue AI Gateway)
var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion(
modelId: "gpt-4.1-mini", // Use CoreValue model names
apiKey: corevalueApiKey, // Your CoreValue API key
endpoint: new Uri("https://gateway.corevalue.dev/v1") // Add this line!
);
var kernel = builder.Build();That's it! Just one additional parameter and you're routing through CoreValue's AI Gateway.
Complete Working Example
Here's a full example that tests multiple models:
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using DotNetEnv;
// Load environment
Env.Load();
var apiKey = Environment.GetEnvironmentVariable("COVA_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("❌ COVA_API_KEY not found in environment");
return;
}
Console.WriteLine("🚀 Testing multiple models through CoreValue AI Gateway\n");
// Test different models
await TestModel("gpt-4.1-mini", "OpenAI GPT-4.1 Mini");
await TestModel("anthropic/claude-opus-4-1", "Anthropic Claude Opus 4.1");
await TestModel("google/gemini-2.5-flash-lite", "Google Gemini 2.5 Flash Lite");
Console.WriteLine("\n✅ All models tested!");
Console.WriteLine("🔍 Check your dashboard: https://us.corevalue.dev/dashboard");
async Task TestModel(string modelId, string modelName)
{
try
{
var builder = Kernel.CreateBuilder();
// Configure with CoreValue AI Gateway
builder.AddOpenAIChatCompletion(
modelId: modelId,
apiKey: apiKey,
endpoint: new Uri("https://gateway.corevalue.dev/v1")
);
var kernel = builder.Build();
var chatService = kernel.GetRequiredService<IChatCompletionService>();
var chatHistory = new ChatHistory();
chatHistory.AddUserMessage("Say hello in one sentence.");
Console.Write($"🤖 Testing {modelName}... ");
var response = await chatService.GetChatMessageContentAsync(chatHistory);
Console.WriteLine("✅");
Console.WriteLine($" Response: {response.Content}\n");
}
catch (Exception ex)
{
Console.WriteLine("❌");
Console.WriteLine($" Error: {ex.Message}\n");
}
}import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.contents import ChatHistory
import os
import asyncio
# Load environment
corevalue_api_key = os.getenv("COVA_API_KEY")
if not corevalue_api_key:
print("❌ COVA_API_KEY not found in environment")
exit(1)
print("🚀 Testing multiple models through CoreValue AI Gateway\n")
async def test_model(model_id: str, model_name: str):
try:
# Create kernel
kernel = sk.Kernel()
# Configure with CoreValue AI Gateway
kernel.add_service(
OpenAIChatCompletion(
service_id="corevalue-gateway",
ai_model_id=model_id,
api_key=corevalue_api_key,
endpoint="https://gateway.corevalue.dev/v1"
)
)
chat_service = kernel.get_service("corevalue-gateway")
chat_history = ChatHistory()
chat_history.add_user_message("Say hello in one sentence.")
print(f"🤖 Testing {model_name}... ", end="")
response = await chat_service.get_chat_message_content(
chat_history=chat_history
)
print("✅")
print(f" Response: {response.content}\n")
except Exception as ex:
print("❌")
print(f" Error: {str(ex)}\n")
async def main():
# Test different models
await test_model("gpt-4.1-mini", "OpenAI GPT-4.1 Mini")
await test_model("anthropic/claude-opus-4-1", "Anthropic Claude Opus 4.1")
await test_model("google/gemini-2.5-flash-lite", "Google Gemini 2.5 Flash Lite")
print("\n✅ All models tested!")
print("🔍 Check your dashboard: https://us.corevalue.dev/dashboard")
if __name__ == "__main__":
asyncio.run(main())Request a CoreValue Integration
Looking for a framework or tool not listed here? Request it here!