Allow user-defined selection logic when multiple existing meta-agents are eligible
Summary
Add optional meta_agent_selector parameter to create_meta_agent to let users control which meta-agent receives new agents when multiple candidates of the same class exist.
Current Behavior
When multiple meta-agents of the same class already exist, create_meta_agent selects one deterministically by lowest unique_id (lines 268-275):
# TODO: Add way for user to specify how agents join meta-agent
# instead of random choice if there are multiple meta-agents of the same class
meta_agent = (
sorted(existing_meta_agents, key=lambda x: x.unique_id)[0]
if len(existing_meta_agents) > 1
else existing_meta_agents[0]
)
Proposed Solution
Add an optional callback parameter to create_meta_agent:
meta_agent_selector: Callable[[list[MetaAgent], list[Agent]], MetaAgent] | None = None
If provided, it receives the candidate meta-agents and agents being added, and returns which meta-agent should receive them.
Implementation
Replace the current selection logic with:
if meta_agent_selector:
meta_agent = meta_agent_selector(existing_meta_agents, agents)
elif len(existing_meta_agents) > 1:
meta_agent = sorted(existing_meta_agents, key=lambda x: x.unique_id)[0]
else:
meta_agent = existing_meta_agents[0]
Example Usage
# Select meta-agent with most members
def select_largest(candidates, new_agents):
return max(candidates, key=len)
meta = create_meta_agent(
model, "Team", [agent1, agent2], Agent,
meta_agent_selector=select_largest
)
# Select based on aggregate power
def select_strongest(candidates, new_agents):
return max(candidates, key=lambda ma: sum(a.power for a in ma.agents))
meta = create_meta_agent(
model, "Alliance", [agent3, agent4], Agent,
meta_agent_selector=select_strongest
)
Benefits
- Fully backward compatible (default behavior unchanged)
- Clean extension point for model-specific selection logic
- No additional policy baked into Mesa
Would this be a useful addition to meta_agents?
Hey @souro26,
This would be a really helpful improvement to Mesa’s meta-agent system. At the moment, if there are multiple meta-agents of the same class, create_meta_agent just picks the one with the lowest unique_id, which works but feels a bit arbitrary. Adding an optional meta_agent_selector would let users decide more naturally which meta-agent new agents should join, based on what makes sense for their model (like the biggest group or the strongest alliance), while still keeping the current behavior as the default.
I completely understand why this feature would be valuable, and I’d be genuinely happy to work on implementing it since it makes the system much more flexible without adding unnecessary rules into Mesa.
Hey @quaquel , this seems like a useful improvement to the meta-agent system. I’d love to hear your thoughts on the proposed approach. If you’re okay with it, I’d be happy to work on implementing this change.
Thanks for the interest @Krishsharma179. I'm glad the use case resonates.
Since this touches core meta-agent selection semantics, I was planning to flesh out the edge cases and API constraints before implementation. Specifically around:
- Selector failure modes
- Whether selector can/should mutate agents
- When the hook applies
- Deterministic fallback behavior
Let me follow up with a concrete design proposal addressing these, then we can determine the cleanest implementation path.
@quaquel I would appreciate your input on the overall direction before diving into implementation details. My goal is to keep the default behavior fully unchanged unless the selector is explicitly provided.
Hey @souro26 , thanks for the response I really appreciate you highlighting those design concerns.
Just to make sure I understand correctly, could you please clarify what you specifically mean by the following points in the context of meta_agent_selector?
Selector failure modes Whether the selector can/should mutate agents When exactly the hook should apply Deterministic fallback behavior
A bit more detail on how you envision these semantics would really help me stay aligned before moving forward.
I’d also be very happy to help with fleshing out the design and edge cases once things are clearer, so we can ensure the implementation fits well into Mesa’s meta-agent system.
Thanks again!
Here's what i meant by the following points :
-
Selector failure modes : By this I meant what should happen if the selector behaves unexpectedly, like if it raises an exception, returns
None, or returns something that isn’t one of the candidate meta-agents. This is mostly about being explicit on whether Mesa should raise an error immediately or attempt a fallback. -
Whether the selector can / should mutate agents : Here I was thinking about the intended action of the selector. Conceptually it’s a decision function (choosing among existing meta-agents), not a mutation hook. Clarifying this helps set expectations for users.
-
When exactly the hook applies : This is about whether the selector is only called when there are multiple eligible meta-agents, versus being invoked in all cases. The distinction matters for preserving current behavior and avoiding unnecessary complexity.
-
Deterministic fallback behavior : This is simply about what happens when no selector is provided. Currently the choice is deterministic (lowest
unique_id), and my intent was to make sure that behavior remains unchanged unless the user explicitly opts into custom logic.
Hope this explains what i meant. These were some semantic questions i was thinking about.
Based on the above, here's a concrete direction I'm leaning toward that keeps the current behavior fully intact while adding an explicit extension point.
Proposed semantics:
- Add an optional
meta_agent_selectorcallable tocreate_meta_agent - The selector is invoked only when multiple eligible meta-agents of the same class exist
- It receives the list of candidate meta-agents and the agents being added, and returns exactly one of the candidates
- If no selector is provided, the existing deterministic behavior (lowest
unique_id) remains unchanged
Signature:
meta_agent_selector: Callable[[list[MetaAgent], Iterable[Agent]], MetaAgent] | None = None
Error handling:
- If the selector raises or returns an invalid result (not one of the candidates), Mesa should raise
ValueErrorrather than silently falling back, to avoid masking logic bugs. This makes selector failures explicit and can be debugged.
Backward compatibility:
- Default is
None, so the current behavior is preserved - Fully opt-in extension point
- No existing code affected
This keeps the default path untouched and makes custom logic completely optional. I'll put together a PR along these lines so the discussion can be grounded in code rather than hypotheticals. I’ll open it as a draft to make the tradeoffs explicit in code.
Hey @souro26 , thanks I was thinking along the same lines. This proposed semantics looks really solid and keeps the default behavior fully intact.
Once the draft PR is up, I’d be very happy to help by testing it and adding any useful edge-case coverage to make sure everything behaves as expected.
Thanks again for driving this forward