-
Notifications
You must be signed in to change notification settings - Fork 439
Open
Labels
Description
In the knights and knaves sample, multiple agents are instantiated and composed directly inside the lambda passed to AddAIAgent
.
While it works fine for small demos, this approach becomes difficult to maintain and reason about when dealing with more complex setups.
Example from the documentation:
builder.AddAIAgent("knights-and-knaves", (sp, key) =>
{
var chatClient = sp.GetRequiredKeyedService<IChatClient>("chat-model");
ChatClientAgent knight = new(chatClient, "...", "Alice");
ChatClientAgent knave = new(chatClient, "...", "Bob");
ChatClientAgent narrator = new(chatClient, "...", "Narrator");
// TODO: How to avoid sync-over-async here?
return AgentWorkflowBuilder
.BuildConcurrent([knight, knave, narrator])
.AsAgentAsync(name: key)
.AsTask()
.GetAwaiter()
.GetResult();
});
Problem
- The lambda becomes cluttered as the number of agents grows.
- It mixes dependency resolution, agent construction, and workflow composition logic.
- It’s hard to reuse or organize agent definitions in larger projects.
- There’s already a TODO comment in the sample about avoiding sync-over-async — which suggests this pattern isn’t ideal for production use.