Configures retry and resilience middleware for Microsoft.Extensions.AI clients using CrestApps.Core.AI.Resilience. Covers default rate-limit retries, custom Polly pipelines, and builder-based application for chat, embedding, image, speech, and text-to-speech clients.
Ships in crestapps-core. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Builder-based retry middleware for Microsoft.Extensions.AI clients, from the standalone CrestApps.Core.AI.Resilience package.
CrestApps.Core.AI.Resilience is a standalone package. Reference it explicitly in the consuming project.
<PackageReference Include="CrestApps.Core.AI.Resilience" Version="*" />
It depends on Microsoft.Extensions.AI and Microsoft.Extensions.Resilience.
The extensions live in namespace CrestApps.Core.AI.Resilience and apply to the corresponding Microsoft.Extensions.AI builders: ChatClientBuilder, EmbeddingGeneratorBuilder<TInput, TEmbedding>, ImageGeneratorBuilder, SpeechToTextClientBuilder, and TextToSpeechClientBuilder.
Framework-owned completion and utility chat paths in CrestApps.Core (for example orchestration planning) already use the default retry policy internally. Use this package for host-created clients or when an application wants to opt in explicitly.
There is no resilience extension for RealtimeClientBuilder.
Two ways to apply resilience:
IAIClientFactory.CreateChatClientAsync(deployment, configurePipeline). The factory passes a ChatClientBuilder to the callback and owns the final build..AsBuilder(), apply UseDefaultResilience() or UseResilience(...), then finish with Build(serviceProvider).When you build manually, always pass the active IServiceProvider to Build(serviceProvider). Never call Build() or Build(null) — downstream middleware may need DI to resolve tools and runtime components.
UseDefaultResilience() is intentionally narrow. It retries provider rate-limit failures such as HTTP 429 Too Many Requests. Tune it with AIChatClientRetryOptions.
| Setting | Default |
|---|---|
MaxRateLimitRetries | 5 |
RateLimitRetryDelay | 1 second |
BackoffType | DelayBackoffType.Exponential |
UseJitter | true |
MaxRetryDelay | 32 seconds |
The default produces an approximate schedule of ~1-2s, ~2-4s, ~4-8s, ~8-16s, ~16-32s across five retries. Actual delays vary because jitter is on.
When the provider asks for a delay, UseDefaultResilience() waits as long as it asks instead of the backoff: it reads retry-after-ms, x-ms-retry-after-ms and Retry-After from the failed response of a ClientResultException (thrown by the OpenAI and Azure OpenAI SDKs), never longer than MaxRetryDelay. A failure without those headers, such as an HttpRequestException, uses the backoff. AIRetryAfterHelper reads the same headers for a pipeline of your own:
using CrestApps.Core.AI.Resilience;
using Polly;
using Polly.Retry;
var maxDelay = TimeSpan.FromSeconds(30);
var chatClient = await clientFactory.CreateChatClientAsync(deployment, builder => builder
.UseResilience(pipeline => pipeline.AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = 3,
DelayGenerator = args => ValueTask.FromResult<TimeSpan?>(
AIRetryAfterHelper.TryGetRetryAfter(args.Outcome.Exception, TimeProvider.System, out var retryAfter)
? AIRetryAfterHelper.Cap(retryAfter, maxDelay)
: null),
})));
TryGetRetryAfter(Func<string, string> getHeaderValue, TimeProvider, out TimeSpan) reads the headers from any response.
Through the factory (recommended when the factory creates the client):
var resilientClient = await aiClientFactory.CreateChatClientAsync(
deployment,
builder => builder.UseDefaultResilience());
From an existing raw IChatClient:
var resilientClient = chatClient
.AsBuilder()
.UseDefaultResilience()
.Build(serviceProvider);
Keep the built-in rate-limit handling but tune the retry shape:
var resilientClient = await aiClientFactory.CreateChatClientAsync(
deployment,
builder => builder.UseDefaultResilience(options =>
{
options.MaxRateLimitRetries = 3;
options.RateLimitRetryDelay = TimeSpan.FromSeconds(2);
options.BackoffType = DelayBackoffType.Exponential;
options.UseJitter = true;
options.MaxRetryDelay = TimeSpan.FromSeconds(20);
}));
For a fixed (non-exponential) schedule:
var resilientClient = chatClient
.AsBuilder()
.UseDefaultResilience(options =>
{
options.MaxRateLimitRetries = 4;
options.RateLimitRetryDelay = TimeSpan.FromSeconds(5);
options.BackoffType = DelayBackoffType.Constant;
options.UseJitter = false;
options.MaxRetryDelay = TimeSpan.FromSeconds(5);
})
.Build(serviceProvider);
Use UseResilience(...) for full control over the Polly pipeline:
var resilientClient = chatClient
.AsBuilder()
.UseResilience(pipeline => pipeline.AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = 2,
Delay = TimeSpan.FromSeconds(1),
BackoffType = DelayBackoffType.Exponential,
UseJitter = true,
ShouldHandle = args => ValueTask.FromResult(
args.Outcome.Exception is HttpRequestException ex &&
ex.StatusCode == HttpStatusCode.TooManyRequests),
}))
.Build(serviceProvider);
You can also pass a prebuilt ResiliencePipeline to share one pipeline across multiple clients.
The same UseDefaultResilience() / UseResilience(...) extensions are available on the other builders. Factory and raw-builder forms both work.
// Embeddings
var embeddings = embeddingGenerator.AsBuilder().UseDefaultResilience().Build(serviceProvider);
// Image generation
var images = imageGenerator.AsBuilder().UseDefaultResilience().Build(serviceProvider);
// Speech to text
var stt = speechToTextClient.AsBuilder().UseDefaultResilience().Build(serviceProvider);
// Text to speech
var tts = textToSpeechClient.AsBuilder().UseDefaultResilience().Build(serviceProvider);
ITextToSpeechClient streaming retries are supported when the failure happens before the first streamed update is yielded.ISpeechToTextClient non-streaming retries work for both seekable and non-seekable streams.ISpeechToTextClient streaming retries require a seekable input stream so the audio can be replayed safely across retry attempts.UseDefaultResilience() adds 1 to crestapps.core.ai.resilience.retries, tagged operation (Chat, Embedding, Image, SpeechToText or TextToSpeech), reason (rate_limit) and attempt (capped at 5).UseDefaultResilience() and UseResilience(...) on a chat builder count it on crestapps.core.ai.resilience.stream_failures (tag operation).client (Azure) instead of operation, with reason rate_limit or transient.UseResilience(...) adds nothing to the retries metric. Give it a Name and call ConfigureTelemetry(loggerFactory) so the Polly meter reports it.See crestapps-core-observability for collecting these metrics.
UseDefaultResilience() — a safe default for provider throttling, framework-style retries on your own clients, when you do not yet need a custom Polly pipeline.UseResilience(...) — custom retry predicates, additional strategies, or one shared prebuilt pipeline across multiple clients.Resilience composes with the other pipeline extensions. Usage metering is applied at the provider client below the whole pipeline, wherever these calls sit:
var client = await aiClientFactory.CreateChatClientAsync(deployment, builder => builder
.UseDefaultResilience()
.UseUsageLabels(contextType: "Reports", purpose: "Summary")
.UseModelParameters(parameterApplier, deployment, completionContext, AIDeploymentParameterScope.Utility));
AIProviderErrorHelper.IsRateLimitException(ex) (used by the default policy) and IsNotFoundException(ex) (a missing model or deployment, such as an Ollama model that was never pulled) help write custom ShouldHandle predicates and error messages.
UseDefaultResilience() and the Azure OpenAI SDK retry policy wait for the provider's Retry-After delay (capped by MaxRetryDelay) instead of retrying too early. New: AIRetryAfterHelper, and AzureClientOptions.NetworkTimeout (see crestapps-core-azure-openai).IAIClientFactory clients with UseDefaultResilience().ResolveSlotAsync); see crestapps-core-ai-runtime.What SKILL.md sets; muted values are Claude Code's defaults
/crestapps-core:crestapps-core-resiliencefrom crestapps-core
npx claudepluginhub crestapps/crestapps.agentskills --plugin crestapps-core51 skills in this plugin
/crestapps-core-a2aSkill for Agent-to-Agent (A2A) protocol support in CrestApps.Core on the A2A 1.0 SDK. Covers AddA2AClient and AddCoreAIA2AClient with Entity Framework Core or YesSql connection stores, A2AConnection and A2AConnectionMetadata authentication, AIProfileA2AMetadata, the agent card cache and A2AToolRegistryProvider, the listAvailableAgents, findAgentForTask and findToolsForTask tools, building an A2A host with IAgentHandler, A2AServer, ITaskStore and MapA2A, A2A 1.0 agent cards with supportedInterfaces published at /.well-known/agent-card.json, A2AHostOptions authentication and ExposeAgentsAsSkill, system agents exposed through the host, and upgrading from the 0.3 SDK./crestapps-core-ag-uiSkill for running the CrestApps.Core 2.0 AI chat as an AG-UI agent. Covers the CrestApps.Core.AI.AGUI package (AGUIRunEventWriter, AGUIRunRequest, AGUIJson), AIChatHubCore.RunAgent over SignalR and MapAGUIChat with Server-Sent Events, the RunAgentInput members and streamed events, tool approvals with RequireApproval or an approval tool kind, ApprovalRequiredAIFunction, interrupts and resume, approvals inside agents, page context with crestAppsAGUI.registerContextProvider, shared state with registerStateProvider and update_shared_state, frontend tools, stored tool calls (ChatTurnToolActivity), runs that continue on the next page, AGUIChatOptions.DisconnectedRunTimeout, the protocol and sendPageContext chat options, and the AICompletionContextKeys and ChatResponseHandlerContext members. Use it when showing tool calls in the chat, asking a person before a tool runs, sharing page state with the agent, connecting CopilotKit or another AG-UI client, or troubleshooting approvals and continued runs.Other plugins' skills for the same job