microsoft/autogen · error · Exception
Agent factory with type {type} already exists.
Error message
Agent factory with type {type} already exists. What it means
GrpcAgentTypesHost.RegisterAgentFactory throws when a factory for the same AgentType is already registered. The host keeps a Dictionary<AgentType, Func<...>> and refuses duplicate keys to prevent silently replacing agent implementations.
Source
Thrown at dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs:57
return this.agentInstances[agentId];
}
public async ValueTask<Contracts.AgentId> GetAgentAsync(Contracts.AgentId agentId, bool lazy = true)
{
if (!lazy)
{
await this.EnsureAgentAsync(agentId);
}
return agentId;
}
public AgentType RegisterAgentFactory(AgentType type, Func<Contracts.AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factoryFunc)
{
if (this.agentFactories.ContainsKey(type))
{
throw new Exception($"Agent factory with type {type} already exists.");
}
this.agentFactories.Add(type, factoryFunc);
return type;
}
public void AddSubscription(ISubscriptionDefinition subscription)
{
if (this.Subscriptions.ContainsKey(subscription.Id))
{
throw new Exception($"Subscription with id {subscription.Id} already exists.");
}
this.Subscriptions.Add(subscription.Id, subscription);
}
public bool RemoveSubscriptionAsync(string subscriptionId)
{View on GitHub (pinned to 027ecf0a37)
Solutions
- Remove the duplicate registration — search for all RegisterAgentFactory/AddAgent calls for that type and keep one.
- If using auto-registration attributes, delete the manual registration (or vice versa).
- Guard registration with a check of RegisteredAgentTypes before calling RegisterAgentFactory in shared/pluggable code.
Example fix
// before
host.RegisterAgentFactory(new AgentType("planner"), plannerFactory);
host.RegisterAgentFactory(new AgentType("planner"), plannerFactory); // throws: duplicate
// after
if (!host.RegisteredAgentTypes.Contains(new AgentType("planner")))
{
host.RegisterAgentFactory(new AgentType("planner"), plannerFactory);
} Defensive patterns
Strategy: validation
Validate before calling
if (!host.RegisteredAgentTypes.Contains(type))
{
host.RegisterAgentFactory(type, factoryFunc);
} Type guard
static bool IsFactoryRegistered(GrpcAgentTypesHost host, AgentType type) =>
host.RegisteredAgentTypes.Contains(type); Prevention
- Centralize agent registration in one startup path; avoid mixing auto-scan and manual registration.
- Make registration code idempotent with a RegisteredAgentTypes check when it runs in shared extensions.
When it happens
Trigger: Calling RegisterAgentFactory(type, factory) twice with the same AgentType — commonly from double execution of startup code, hosting the same agent in two hosting groups, or DI lifetimes that re-run registration (e.g. scoped resolution invoking a register method).
Common situations: Adding the same agent via two AddAgent/RegisterAgent calls (e.g. in a shared extension method called twice); hot-reload or retry logic in startup that re-executes registration; combining an auto-scan/registration assembly attribute with explicit manual registration; tests that build the host multiple times against a shared host instance.
Related errors
- Agent with name {agentId.Type} not found.
- Subscription with id {subscription.Id} already exists.
- INVALID_ARGUMENT
- Agent with type {type} already exists.
- Agent with id {agent_id} already exists.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/b1798a73466cef71.
Report an issue: GitHub.