.NET: Is Singleton the intended lifetime for agents, tools and AIContextProviders, or just the default? #8021
Replies: 1 comment
|
I think the current public contract supports both readings, but at different levels:
That gives this compatibility table:
So my reading is: session-specific mutable state belongs in The docs would benefit from stating those two rules separately: (a) the concurrency/session-state contract for agent and context-provider implementations, and (b) the DI lifetime compatibility rule for hosted registrations. That would avoid turning a default into an apparent requirement. |
Uh oh!
There was an error while loading. Please reload this page.
AddAIAgentdefaults toServiceLifetime.Singleton,WithAITool(AITool)always registers a keyed singleton, and theWithAIToolfactory overload throws if a tool is shorter-lived than its agent. I cannot find anywhere in the docs ordocs/decisionsthat explains whether this is the intended lifetime model or only the historical default.Searching for related discussions/issues I found:
AIAgentandAIContextProviderremarks say instances are shared across many conversations and must keep per-session state inAgentSession. That suggests a stateless-definition model where Singleton is the natural choice.ServiceLifetimeparameter because "we cannot assume that all agent dependencies (e.g. function tools) will always be singletons".To me is clear that agents/tools being singleton isn't part of the design at all, and it's up for us, the consumers, to handle our scopes however we like. But some people might look at everything using Singleton and think that this is a framework design pattern, not a choice.
The question is: "agents, tools and context providers are stateless and shared, state lives in the session" the design intent, or is Singleton a convenience default carried over from
AddChatClient? Can we make it more explicit in the docs?All reactions