Skip to content

ModelProviderCircuitBreaker

Namespace AgentPrism · Assembly AgentPrism.Core.dll

Circuit breaker that temporarily cuts off requests when a model provider fails consecutively. Holds state per provider name.

public sealed class ModelProviderCircuitBreaker

objectModelProviderCircuitBreaker

object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

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.

ModelProviderCircuitBreaker(IOptionsMonitor<AgentPrismOptions>, TimeProvider?)

Section titled “ ModelProviderCircuitBreaker(IOptionsMonitor<AgentPrismOptions>, TimeProvider?)”

Creates a new circuit breaker.

public ModelProviderCircuitBreaker(IOptionsMonitor<AgentPrismOptions> optionsMonitor, TimeProvider? timeProvider = null)

optionsMonitor IOptionsMonitor<AgentPrismOptions>

The runtime settings.

timeProvider TimeProvider?

The time source. If null, the system clock is used.

ArgumentNullException

optionsMonitor is null.

Whether the circuit breaker is currently enabled.

public bool IsEnabled { get; }

bool

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)

providerName string

The provider name.

credentialScope string?

The blast radius; see ModelProviderCircuitBreaker.Wrap.

AgentPrismProviderUnavailableException

The circuit is open.

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)

providerName string

The provider name.

retryAfter TimeSpan?

The remaining break duration, if open.

credentialScope string?

The blast radius; see ModelProviderCircuitBreaker.Wrap.

bool

true if the circuit is open and the break duration is still in effect.

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)

providerName string

The provider name.

credentialScope string?

The blast radius; see ModelProviderCircuitBreaker.Wrap.

Resets the consecutive-failure counter after a successful call.

public void RecordSuccess(string providerName, string? credentialScope = null)

providerName string

The provider name.

credentialScope string?

The blast radius; see ModelProviderCircuitBreaker.Wrap.

Wraps the given chat client with the circuit breaker for this provider.

public IChatClient Wrap(string providerName, IChatClient inner, string? credentialScope = null)

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.

IChatClient

The client wrapped with the circuit breaker.

ArgumentException

providerName is empty.

ArgumentNullException

inner is null.