CrestApps.Core AI Runtime
The runtime is the provider-agnostic layer every other AI feature builds on. A connection holds credentials and an endpoint, a deployment names a model on a connection and declares what that model can do, and a slot says what the installation uses a deployment for.
Guidelines
- Prefer
AddCrestAppsCore(...).AddAISuite(...)for application composition. UseAddCoreAIServices()only when the host composes raw service registrations itself. - Program against
IAIClientFactory,IAICompletionService,IAICompletionContextBuilderandIAIDeploymentManager. Never create a provider SDK client in business code. - Every configured deployment declares its model capabilities through
Features. A configured entry with no features and no legacy purpose is skipped at startup with the warning "declares no model capabilities". - Pick deployments through slots (
AIDeploymentSlotNames) andResolveSlotAsync(...). There is no deployment purpose or type in 2.0. - Use
crestapps-core-ai-model-capabilitiesfor registering features, parameters and slots,crestapps-core-ai-usage-meteringfor usage records,crestapps-core-observabilityfor traces and metrics, and the provider skills (crestapps-core-openai,crestapps-core-azure-openai,crestapps-core-ollama,crestapps-core-azure-ai-inference) for provider-specific settings.
Registration
using CrestApps.Core.AI;
using CrestApps.Core.AI.OpenAI;
builder.Services.AddCrestAppsCore(crestApps => crestApps
.AddAISuite(ai => ai
.AddOpenAI()
)
);
AddAISuite(...) calls AddCoreServices(), AddCoreAIServices() and AddCoreAIOrchestration(). Without the builder:
builder.Services
.AddCoreAIServices()
.AddCoreAIOrchestration()
.AddCoreAIOpenAI();
AddCoreAIServices() also chains AddCoreAITemplating() and AddCoreAIDeploymentCapabilities() (features, parameters and slots). IAIDeploymentManager is registered by AddCoreAIOrchestration(), so register orchestration whenever code resolves deployments.
Connections and Deployments
{
"CrestApps": {
"AI": {
"Connections": [
{
"Name": "openai",
"ClientName": "OpenAI",
"ApiKey": "sk-..."
}
],
"Deployments": [
{
"ClientName": "OpenAI",
"Name": "gpt-5-chat",
"ModelName": "gpt-5",
"ConnectionName": "openai",
"Features": [ "textGeneration", "toolCalling", "streaming", "reasoning" ],
"Parameters": {
"reasoningEffort": { "AllowedValues": [ "Low", "Medium", "High" ], "DefaultValue": "Medium" }
}
},
{
"ClientName": "OpenAI",
"Name": "embeddings",
"ModelName": "text-embedding-3-large",
"ConnectionName": "openai",
"Features": [ "textEmbedding" ]
}
]
}
}
}
FeaturesandParameterson the entry are shorthand forProperties:AIDeploymentMetadata:Featuresand...:Parameters. Both forms work and merge.- Configured deployments are read-only in admin UIs; stored deployments come from the YesSql or EntityCore stores and are merged with configuration through
IAIDeploymentStore. - A connection name that differs from another only by case is a duplicate. The first definition wins and the second is logged as a warning.
Resolving a Deployment
| Slot (AIDeploymentSlotNames) | Required feature | Site default (DefaultAIDeploymentSettings) |
|---|---|---|
| Chat | textGeneration (excludes realtime) | DefaultChatDeploymentName |
| Utility | textGeneration (excludes realtime), falls back to Chat | DefaultUtilityDeploymentName |
| Embedding | textEmbedding | DefaultEmbeddingDeploymentName |
| Image | imageOutput | DefaultImageDeploymentName |
| Vision | imageInput | DefaultVisionDeploymentName |
| SpeechToText | speechToText | DefaultSpeechToTextDeploymentName |
| TextToSpeech | textToSpeech | DefaultTextToSpeechDeploymentName |
| Realtime | realtime | DefaultRealtimeDeploymentName |
Resolution walks one chain: the explicit name, the slot's site default, the fallback slot's explicit name and default, then the first capable deployment.
using CrestApps.Core.AI.Deployments;
using CrestApps.Core.AI.Models;
// Named deployment if it qualifies, otherwise the site default, otherwise the first capable one.
var chat = await deploymentManager.ResolveSlotAsync(AIDeploymentSlotNames.Chat, deploymentName: "gpt-5-chat");
// Background work: explicit utility, site utility, explicit chat, site chat, then first text-capable.
var utility = await deploymentManager.ResolveUtilityOrDefaultAsync(
utilityDeploymentName: profile.UtilityDeploymentName,
chatDeploymentName: profile.ChatDeploymentName);
// Throws InvalidOperationException when nothing can fill the slot.
var embedding = await deploymentManager.ResolveSlotOrThrowAsync(AIDeploymentSlotNames.Embedding);
// Everything eligible for a picker.
var candidates = await deploymentManager.GetAllBySlotAsync(AIDeploymentSlotNames.Chat);
DefaultAIDeploymentSettings is a plain options class. The framework does not bind it from configuration; configure it from your own section or site settings:
builder.Services.Configure<DefaultAIDeploymentSettings>(settings =>
{
settings.DefaultChatDeploymentName = "gpt-5-chat";
settings.DefaultUtilityDeploymentName = "gpt-5-mini";
settings.DefaultEmbeddingDeploymentName = "embeddings";
});
Key Runtime Services
| Service | Use |
|---|---|
| IAIClientFactory | Create IChatClient, IEmbeddingGenerator, IImageGenerator, ISpeechToTextClient, ITextToSpeechClient and IRealtimeClient for a deployment. Every overload except realtime accepts an Action<...Builder> pipeline callback, and the factory owns the final Build(serviceProvider) |
| IAICompletionService | CompleteAsync / CompleteStreamingAsync(deployment, messages, context, cancellationToken) without the orchestration loop |
| IAICompletionContextBuilder | BuildAsync(resource, configure, cancellationToken) runs the context handlers for a profile, interaction or other resource |
| IAIDeploymentManager | Slot resolution and deployment CRUD |
| IAIDeploymentCapabilityService | What a deployment's model declares |
| IAIProviderConnectionStore / IAIDeploymentStore | Merged configuration and store views |
Every client the factory creates is metered, and every chat client is wrapped in CapabilityEnforcingChatClient, which strips tools, JSON response formats and reasoning options the deployment does not declare. Chat clients stream tool calls and results (FunctionCallContent, FunctionResultContent) through FunctionInvokingChatClient, which the AG-UI chat shows as tool cards; NamedAICompletionClient passes stored tool result messages to the provider and drops results whose calls a past-messages limit cut off. CreateRealtimeClientAsync returns a metering wrapper; use GetService to reach the provider client.
Telemetry Options
The framework registers a scoped DefaultAIOptions that applies the site's GeneralAIOptions overrides. Two of its options control the Microsoft.Extensions.AI gen_ai.* telemetry; the framework's own CrestApps.Core spans and metrics do not depend on them.
EnableOpenTelemetry(off by default) addsgen_ai.*spans and metrics, under theAICompletionServicesource, to the OpenAI, Azure AI Inference and Ollama completion clients, the Azure OpenAI Responses path, the Claude orchestrator, and the embedding, image, speech and realtime clients fromIAIClientFactory. The Azure OpenAI chat-completions path uses the OpenAI SDK'sOpenAI.Experimental.EnableOpenTelemetryswitch instead.EnableSensitiveData(off by default) adds prompts, responses and tool arguments to those spans. It applies only whileEnableOpenTelemetryis on.
A site value applies only with its flag: GeneralAIOptions.OverrideEnableOpenTelemetry or OverrideEnableSensitiveData. See crestapps-core-observability.
Example Completion Service
using CrestApps.Core.AI.Completions;
using CrestApps.Core.AI.Deployments;
using CrestApps.Core.AI.Models;
using Microsoft.Extensions.AI;
public sealed class QuestionService
{
private readonly IAICompletionService _completionService;
private readonly IAIDeploymentManager _deploymentManager;
public QuestionService(
IAICompletionService completionService,
IAIDeploymentManager deploymentManager)
{
_completionService = completionService;
_deploymentManager = deploymentManager;
}
public async Task<string> AskAsync(string question, CancellationToken cancellationToken = default)
{
var deployment = await _deploymentManager.ResolveSlotAsync(
AIDeploymentSlotNames.Chat,
cancellationToken: cancellationToken)
?? throw new InvalidOperationException("No chat deployment is configured.");
var context = new AICompletionContext
{
ChatDeploymentName = deployment.Name,
SystemMessage = "You are a helpful assistant.",
DisableTools = true,
};
var response = await _completionService.CompleteAsync(
deployment,
[new ChatMessage(ChatRole.User, question)],
context,
cancellationToken);
return response.Text;
}
}
To reuse a profile's prompts, tools, persona and model parameters, build the context from the profile instead: await completionContextBuilder.BuildAsync(profile, cancellationToken: cancellationToken).
Direct Clients
var chatClient = await clientFactory.CreateChatClientAsync(deployment, builder => builder
.UseDefaultResilience()
.UseUsageLabels(contextType: "Leads", purpose: "LeadScoring"));
var embeddingDeployment = await deploymentManager.ResolveSlotOrThrowAsync(AIDeploymentSlotNames.Embedding);
var generator = await clientFactory.CreateEmbeddingGeneratorAsync(embeddingDeployment);
When you finish a builder yourself, call Build(serviceProvider), never Build().
Custom Providers
A provider that creates Microsoft.Extensions.AI clients implements IAIClientProvider (CanHandle(clientName) plus GetChatClientAsync, GetEmbeddingGeneratorAsync, GetRealtimeClientAsync and the other client getters for a connection), so IAIClientFactory, capability enforcement and metering apply to it with no extra code. A provider that owns the whole completion loop implements IAICompletionClient (ClientName, CompleteAsync, CompleteStreamingAsync) and registers it with AddCoreAICompletionClient<TClient>(clientName, configure). Deriving from AICompletionServiceBase gives ResolveDeploymentAsync(slotName, providerName, deploymentName) and ResolveRequestDeploymentAsync(context, providerName), which honors AICompletionContext.IsUtilityCompletion.
Troubleshooting
ResolveSlotAsyncreturnsnull: no deployment declares the slot's required feature, or the only candidate declares the excludedrealtimefeature forChatorUtility.- A configured deployment is missing: the entry declares no
Features, itsClientNameis unknown, or one of its values could not be read (each bad entry is now skipped on its own with an error naming it). - Tools are never called: the deployment declares metadata without
toolCalling, so capability enforcement removes them and logs a warning.
Upgrading Within 2.0 Previews
- The packages target
net10.0andnet11.0. Onnet11.0,Microsoft.Extensions.AIandMicrosoft.Extensions.AI.Abstractionsresolve to 11.0.0 (10.11.0 onnet10.0);Microsoft.Extensions.AI.OpenAIstays on 10.9.0 for both. - Conversation history now carries tool calls and results (stored as
ChatTurnToolActivityon the assistant message); a custom completion client should passChatRole.Toolmessages through to its provider.
ProviderAICompletionClient<TProvider>takes the scopedDefaultAIOptionsinstead ofIOptions<DefaultAIOptions>, so a site's telemetry settings reach the OpenAI, Azure AI Inference and Ollama clients. Code that constructs it directly passes aDefaultAIOptions. A client registered withAddCoreAICompletionClientneeds no change.
Upgrading from 1.x
AIDeploymentPurpose,AIDeploymentPurposeExtensions,AIDeploymentType,AIDeployment.Purpose,AIDeployment.Type,SupportsPurpose,SupportsTypeandCanServeTextCompletion()are removed.ResolveOrDefaultAsync(AIDeploymentPurpose.X, ...)becomesResolveSlotAsync(AIDeploymentSlotNames.X, ...).GetByPurposeAsync,GetAllByPurposeAsync,GetByTypeAsync,GetAllByTypeAsyncand the purpose overloads ofGetDefaultAsyncbecomeGetAllBySlotAsync.ResolveAsync(purpose, ...)becomesResolveSlotOrThrowAsync(slotName, ...).AIDeploymentConfigurationEntry.Purposeand.TypebecomeLegacyPurposes. Replace"Purpose"or"Type"keys in configuration withFeatures.- Stored and configured deployments that still carry
Purpose,CapabilityorTypeare projected onto capabilities when read, so no data migration is needed. The projection mapsChat/UtilitytotextGenerationonly, so declaretoolCallingandstreamingyourself or tools are stripped. AICompletionServiceBase.ResolveDeploymentAsynctakes a slot name instead of a purpose.ChatMode.RealtimeandAIProfile.RealtimeDeploymentNameare removed; a realtime model is a deployment that declaresrealtime.