.NET: Is Singleton the intended lifetime for agents, tools and AIContextProviders, or just the default? #8021
Unanswered
Paulo Pozeti (ppozeti)
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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