Overview
A2A Delegation is the runtime routing layer that decides what happens when a user @mentions an agent in an Allternit surface such as chat or cowork. Instead of every agent running the request itself,useA2ADelegation inspects the target agent’s identity and dispatches the task to the right execution path: a swarm run, a direct agent-to-agent (A2A) message, or a normal single-agent run.
The hook lives in surfaces/ai.allternit.com/src/lib/agents/use-a2a-delegation.ts and is the bridge between the social convention of @mentioning an agent and the platform’s multi-agent execution primitives.
When to use A2A Delegation
Use A2A delegation whenever an agent needs to hand work to another runtime identity:- Swarm execution — route a task to a group of agents configured with a strategy such as hierarchical, democratic, or specialist.
- Sub-agent routing — ask a delegate-capable parent agent to spawn or coordinate its sub-agents.
- Peer agent messaging — send a typed A2A message to another agent for direct collaboration.
- Fallback single-agent runs — when the target is a regular agent, the caller can continue without extra routing.
Key concepts
Agent Card
A public metadata block on every agent that declares capabilities, trust tier, example prompts, and whether the agent can delegate to others.
canDelegate
A boolean flag in
agent.agentCard.canDelegate. When true, @mentions are routed as A2A messages so the agent can hand work to sub-agents or peers.Swarm-as-Agent Bridge
swarmToAgent() converts an AgentSwarm into an Agent with a swarm_-prefixed ID. Swarms appear in agent lists and can be @mentioned like any other agent.Loop Guard
The agent communication tool tracks hop counters per
correlationId and escalates to a human after a configurable number of hops to prevent infinite delegation loops.Delegation flow
When a surface detects an @mention, it callsdelegate(fromAgentId, toAgentId, task). The hook resolves the target agent and chooses one of three paths:
1. Swarm execution
If the target ID starts withswarm_, the hook extracts the original swarm ID and calls startSwarmRun(swarmId, task). This begins a collaborative run inside the configured swarm.
2. A2A message for delegate agents
If the target agent’s card hascanDelegate: true, the hook sends a direct A2A message through the agent communication store. The message carries the task content, sender/recipient metadata, a correlation ID, and a type of direct.
3. Regular agent fallback
If the target is a normal agent without delegation capabilities, the hook returns success withdelegatedTo set to the agent ID and no side effects. The calling surface can start a standard single-agent run.
Configuring an agent for delegation
Delegation behavior is declared on the agent record. TheagentCard block is the source of truth.
Making a swarm mentionable
Swarms are not agents by default. UseswarmToAgent() from swarm-as-agent.ts to expose a swarm as an @mentionable identity.
Return shape and error handling
delegate() returns a promise that resolves to:
Common outcomes:
Integration with the rest of the platform
A2A Delegation sits between the chat surface and the agent execution backends:- Agent Store (
useAgentStore) supplies the agent registry used to resolve mentions. - Advanced Agent Store (
useAdvancedAgentStore) providesstartSwarmRunfor swarm execution. - Agent Communication Store (
useAgentCommunicationStore) delivers the typed message bus and loop guard used for peer-to-peer A2A messages. - Swarm-as-Agent Bridge (
swarm-as-agent.ts) exposes swarms as first-class agent identities. - CommRails can carry A2A envelopes across local peers when a workspace spans multiple sessions or machines.
Best practices
- Keep
canDelegateoff for leaf agents that should never forward work. - Use descriptive
agentCard.examplesso users know when to @mention the agent. - Always set a
correlationIdwhen forwarding A2A messages so the loop guard and audit trail stay intact. - Handle
success: falsein the UI with clear feedback rather than silently retrying. - For swarms, prefer the
swarm_prefix when persisting agent lists so the hook can identify them without extra metadata.