Skip to content

ModelProviderHealthCache

Namespace AgentPrism · Assembly AgentPrism.Core.dll

Caches the health-check results of the registered model providers.

public sealed class ModelProviderHealthCache

objectModelProviderHealthCache

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

The /api/models/health endpoint does not go to the provider on every call; the result is returned from the cache for AgentPrismHealthOptions.CacheTtl. refresh: true bypasses the cache and goes to the provider again.

This class is not tied to IModelProviderRegistry — it reads IModelProvider singletons directly through IEnumerable<IModelProvider>. This lets it check whether each provider implements IModelProviderHealthCheck without adding a member to the IModelProvider interface; a provider that does not implement it has a status of ModelProviderHealthStatus.Unknown.

A provider whose circuit breaker is open is reported as ModelProviderHealthStatus.Unhealthy regardless of the raw cached check result — if real chat calls are failing, that is a more reliable signal than a raw connectivity probe. This layer is read FRESH on every call and is not written to the cache.

ModelProviderHealthCache(IEnumerable<IModelProvider>, IOptionsMonitor<AgentPrismOptions>, ModelProviderCircuitBreaker?, TimeProvider?)

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

Creates a new health cache.

public ModelProviderHealthCache(IEnumerable<IModelProvider> providers, IOptionsMonitor<AgentPrismOptions> optionsMonitor, ModelProviderCircuitBreaker? circuitBreaker = null, TimeProvider? timeProvider = null)

providers IEnumerable<IModelProvider>

The registered model providers.

optionsMonitor IOptionsMonitor<AgentPrismOptions>

Settings used to read the cache TTL.

circuitBreaker ModelProviderCircuitBreaker?

The circuit breaker used to reflect circuit state into the result.

timeProvider TimeProvider?

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

ArgumentNullException

providers or optionsMonitor is null.

Returns the health status of every registered provider (sorted by name).

public ValueTask<IReadOnlyList<ModelProviderHealth>> GetAllAsync(bool refresh, CancellationToken cancellationToken = default)

refresh bool

Whether to bypass the cache and go to the provider again.

cancellationToken CancellationToken

The cancellation token.

ValueTask<IReadOnlyList<ModelProviderHealth>>

One record per provider.

GetAsync(string, bool, CancellationToken)

Section titled “ GetAsync(string, bool, CancellationToken)”

Returns the health status of a single provider.

public ValueTask<ModelProviderHealth?> GetAsync(string providerName, bool refresh, CancellationToken cancellationToken = default)

providerName string

The provider name.

refresh bool

Whether to bypass the cache and go to the provider again.

cancellationToken CancellationToken

The cancellation token.

ValueTask<ModelProviderHealth?>

The status if the provider is registered; otherwise null.

Returns the last known status from the cache without making a network call. Intended for fast endpoints such as /api/models to fill in a status field.

public bool TryPeek(string providerName, out ModelProviderHealth health)

providerName string

The provider name.

health ModelProviderHealth

The health record, if found.

bool

true if a record exists in the cache.