ModelProviderCircuitBreaker
AgentPrism.Core.dllCircuit breaker that temporarily cuts off requests when a model provider fails consecutively. Holds state per provider name.
public sealed class ModelProviderCircuitBreakerInheritance
Section titled “Inheritance”object ← ModelProviderCircuitBreaker
Inherited Members
Section titled “Inherited Members”object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()
Remarks
Section titled “Remarks”The classic three-state circuit breaker pattern: Closed (requests
pass through), Open (requests are rejected immediately with
AgentPrismProviderUnavailableException, no call reaches the
provider), and HalfOpen (a single trial window opened once
BreakDuration has elapsed).
State is held on a Concurrent.ConcurrentDictionary using
immutable (record) snapshots and compare-and-swap (CAS)
loops — no lock is used. The AI.IChatClient returned by
Wrap calls ModelProviderCircuitBreaker.EnsureRequestAllowed before every call,
ModelProviderCircuitBreaker.RecordSuccess after a success, and ModelProviderCircuitBreaker.RecordFailure
after a failure.
AgentPrismCircuitBreakerOptions.Enabled is read via IOptionsMonitor.CurrentValue on every call; if it is turned off at runtime, the circuit breaker becomes inactive from that moment on.
Constructors
Section titled “Constructors”ModelProviderCircuitBreaker(IOptionsMonitor<AgentPrismOptions>, TimeProvider?)
Section titled “ ModelProviderCircuitBreaker(IOptionsMonitor<AgentPrismOptions>, TimeProvider?)”Creates a new circuit breaker.
public ModelProviderCircuitBreaker(IOptionsMonitor<AgentPrismOptions> optionsMonitor, TimeProvider? timeProvider = null)Parameters
Section titled “Parameters”optionsMonitor IOptionsMonitor<AgentPrismOptions>
The runtime settings.
timeProvider TimeProvider?
The time source. If null, the system clock is used.
Exceptions
Section titled “Exceptions”optionsMonitor is null.
Properties
Section titled “Properties”IsEnabled
Section titled “ IsEnabled”Whether the circuit breaker is currently enabled.
public bool IsEnabled { get; }Property Value
Section titled “Property Value”Methods
Section titled “Methods”EnsureRequestAllowed(string, string?)
Section titled “ EnsureRequestAllowed(string, string?)”Throws AgentPrismProviderUnavailableException if the circuit
is Open and the break duration has not elapsed. If the break
duration has elapsed, moves the circuit to HalfOpen and lets the
caller have the single trial.
public void EnsureRequestAllowed(string providerName, string? credentialScope = null)Parameters
Section titled “Parameters”providerName string
The provider name.
credentialScope string?
The blast radius; see ModelProviderCircuitBreaker.Wrap.
Exceptions
Section titled “Exceptions”AgentPrismProviderUnavailableException
The circuit is open.
IsOpen(string, out TimeSpan?, string?)
Section titled “ IsOpen(string, out TimeSpan?, string?)”Whether the circuit is currently open (Open with the break
duration not yet elapsed). Intended for the health endpoint to report
circuit state; unlike ModelProviderCircuitBreaker.EnsureRequestAllowed it
does not change the state.
public bool IsOpen(string providerName, out TimeSpan? retryAfter, string? credentialScope = null)Parameters
Section titled “Parameters”providerName string
The provider name.
retryAfter TimeSpan?
The remaining break duration, if open.
credentialScope string?
The blast radius; see ModelProviderCircuitBreaker.Wrap.
Returns
Section titled “Returns”true if the circuit is open and the break duration is still in effect.
RecordFailure(string, string?)
Section titled “ RecordFailure(string, string?)”Increments the consecutive-failure counter after a failed call; opens the circuit if the threshold is exceeded (or if the half-open trial fails).
public void RecordFailure(string providerName, string? credentialScope = null)Parameters
Section titled “Parameters”providerName string
The provider name.
credentialScope string?
The blast radius; see ModelProviderCircuitBreaker.Wrap.
RecordSuccess(string, string?)
Section titled “ RecordSuccess(string, string?)”Resets the consecutive-failure counter after a successful call.
public void RecordSuccess(string providerName, string? credentialScope = null)Parameters
Section titled “Parameters”providerName string
The provider name.
credentialScope string?
The blast radius; see ModelProviderCircuitBreaker.Wrap.
Wrap(string, IChatClient, string?)
Section titled “ Wrap(string, IChatClient, string?)”Wraps the given chat client with the circuit breaker for this provider.
public IChatClient Wrap(string providerName, IChatClient inner, string? credentialScope = null)Parameters
Section titled “Parameters”providerName string
The provider name. Circuit state is held keyed by this name.
inner IChatClient
The client to wrap.
credentialScope string?
The blast radius of the circuit. Pass null when the call uses the setup-time global credential: every tenant shares that credential’s fate, so one broken provider backs all of them off. Pass the tenant identifier when the call uses a tenant-supplied credential, so that a credential only one tenant owns can only ever trip that tenant.
Returns
Section titled “Returns”IChatClient
The client wrapped with the circuit breaker.
Exceptions
Section titled “Exceptions”providerName is empty.
inner is null.