Skip to content

CompiledAgentCache

Namespace AgentPrism · Assembly AgentPrism.Core.dll

Caches compiled agents keyed by (tenant, name, version, skill fingerprint, culture).

public sealed class CompiledAgentCache

objectCompiledAgentCache

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

When a definition is updated, its version increases and the cache naturally becomes stale. There is therefore no explicit invalidation logic; the old version’s entry is cleared with CompiledAgentCache.Evict.

The tenant component of the key is REQUIRED. Agent names are unique only within a tenant (see SqlAgentDefinitionStore) - it is common for two different tenants to have a definition with the same name (e.g. "support"), the same version (the first record is always 1), and the same dependency fingerprint (an empty string for both if they use no skills/callable agents). If the tenant is not included in the key, resolution for the second tenant returns the first tenant’s compiled agent (instructions, tool bindings - including the tenant identity bound into e.g. search_knowledge): tenant isolation broken outright.

public CompiledAgentCache()

Number of entries in the cache.

public int Count { get; }

int

Empties the cache entirely.

public void Clear()

Combines multiple dependency fingerprints into a single key component.

public static string CombineFingerprints(string first, string second)

first string

First fingerprint.

second string

Second fingerprint.

string

The combined fingerprint.

A separator is required: fingerprints may not have a fixed length, and a direct concatenation would let two different pairs produce the same string.

Removes all versions of an agent from the cache.

public void Evict(string name)

name string

Agent name.

GetOrAdd(string, string, int, Func<AIAgent>)

Section titled “ GetOrAdd(string, string, int, Func<AIAgent>)”

Retrieves the agent from the cache; if absent, produces it with factory and adds it.

public AIAgent GetOrAdd(string tenantId, string name, int version, Func<AIAgent> factory)

tenantId string

Identifier of the tenant requesting resolution.

name string

Agent name.

version int

Definition version.

factory Func<AIAgent>

Producer called when absent from the cache.

AIAgent

The compiled agent.

ArgumentNullException

One of the parameters is null.

GetOrAdd(string, string, int, string, Func<AIAgent>)

Section titled “ GetOrAdd(string, string, int, string, Func<AIAgent>)”

Retrieves the agent from the cache together with its dependency fingerprint; if absent, produces it with factory and adds it.

public AIAgent GetOrAdd(string tenantId, string name, int version, string dependencyFingerprint, Func<AIAgent> factory)

tenantId string

Identifier of the tenant requesting resolution.

name string

Agent name.

version int

Definition version.

dependencyFingerprint string

Current fingerprint of the definition’s external dependencies: skills and callable sub-agents. These can change without incrementing the definition’s own version, so they need a separate key component.

factory Func<AIAgent>

Producer called when absent from the cache.

AIAgent

The compiled agent.

GetOrAdd(string, string, int, string, string, Func<AIAgent>)

Section titled “ GetOrAdd(string, string, int, string, string, Func<AIAgent>)”

Retrieves the agent from the cache together with its dependency fingerprint and requested culture; if absent, produces it with factory and adds it.

public AIAgent GetOrAdd(string tenantId, string name, int version, string dependencyFingerprint, string culture, Func<AIAgent> factory)

tenantId string

Identifier of the tenant requesting resolution.

name string

Agent name.

version int

Definition version.

dependencyFingerprint string

See CompiledAgentCache.GetOrAdd.

culture string

The culture the definition was compiled with (see InstructionCultureResolver). An empty string when the run requested no culture. Part of the key: two runs of the same definition in different cultures must not share a compiled agent - the requested instructions text is baked into AI.ChatOptions at compile time.

factory Func<AIAgent>

Producer called when absent from the cache.

AIAgent

The compiled agent.

GetOrAddAsync(string, string, int, string, Func<ValueTask<AIAgent>>)

Section titled “ GetOrAddAsync(string, string, int, string, Func<ValueTask<AIAgent>>)”

Retrieves the agent from the cache together with its dependency fingerprint; if absent, produces it asynchronously with factory and adds it.

public ValueTask<AIAgent> GetOrAddAsync(string tenantId, string name, int version, string dependencyFingerprint, Func<ValueTask<AIAgent>> factory)

tenantId string

Identifier of the tenant requesting resolution.

name string

Agent name.

version int

Definition version.

dependencyFingerprint string

See CompiledAgentCache.GetOrAdd.

factory Func<ValueTask<AIAgent>>

Producer called when absent from the cache.

ValueTask<AIAgent>

The compiled agent.

Compiling now needs an async credential lookup on a cache miss. Concurrent.ConcurrentDictionary has no native async GetOrAdd; the same “the factory may run more than once, production is side-effect free, an extra instance is discarded” guarantee as the sync overload applies here too.

GetOrAddAsync(string, string, int, string, string, Func<ValueTask<AIAgent>>)

Section titled “ GetOrAddAsync(string, string, int, string, string, Func<ValueTask<AIAgent>>)”

Retrieves the agent from the cache together with its dependency fingerprint and requested culture; if absent, produces it asynchronously with factory and adds it.

public ValueTask<AIAgent> GetOrAddAsync(string tenantId, string name, int version, string dependencyFingerprint, string culture, Func<ValueTask<AIAgent>> factory)

tenantId string

Identifier of the tenant requesting resolution.

name string

Agent name.

version int

Definition version.

dependencyFingerprint string

See CompiledAgentCache.GetOrAdd.

culture string

See CompiledAgentCache.GetOrAdd.

factory Func<ValueTask<AIAgent>>

Producer called when absent from the cache.

ValueTask<AIAgent>

The compiled agent.