.NET: AddAIAgent - Rename or add overload

Author: luisquintanillaCreated Sep 5, 2025Updated Sep 18, 2026
Labels.NETagents

AddAIAgent extension methods in the Hosting package implicitly register an AIAgent as a KeyedSingleton

https://github.com/microsoft/agent-framework/blob/518fd447fd259581ae58e17aae66f8be50e9cbe1/dotnet/src/Microsoft.Extensions.AI.Agents.Hosting/HostApplicationBuilderAgentExtensions.cs#L100-L120

When resolving the agent using DI, it's not obvious you need to use FromKeyedServices to get the agent. Using FromServices throws.

i.e.

Works

csharp
app.MapGet("/chat", async ([FromKeyedServices("user-agent")] AIAgent agent, string question) =>
{
    AgentRunResponse agentResponse = await agent.RunAsync(question);

    return Results.Ok(agentResponse.Text);
});

Error

csharp
app.MapGet("/chat", async ([FromServices] AIAgent agent, string question) =>
{
    AgentRunResponse agentResponse = await agent.RunAsync(question);

    return Results.Ok(agentResponse.Text);
});

Suggestions:

  1. Rename existing AddAIAgent to AddKeyedAIAgent
  2. Add overloads for both AddAIAgent and AddKeyedAIAgent

I'd be in favor of option 2.

Source: microsoft/agent-framework