ag2-network-quickstart
Skill12 starsChanged 8 days agoApache-2.0View on GitHub
Build a multi-agent AG2 network — load whenever two or more `Agent`s (or an agent and a human/UI) need to interact; the network is the standard multi-agent pattern in `ag2`. Covers `Hub.open`, `LocalLink`, `HubClient.register` / `register_human`, `AgentClient`, `HumanClient`, `Passport`, `Resume`, channel lifecycle (PENDING → ACTIVE → CLOSING → CLOSED), the 2-party adapters `consulting` (strict 1Q1R) and `conversation` (free-form), `channel.send` / audience, `wait_for_channel_event`, reading the transcript from `hub.read_wal` (`EV_TEXT` text, `EV_PACKET` body), `hub.name_for`, close routes (`channel.close`, `ChannelInject` tool, TTL). Entry point — routes to `ag2-network-discussion` (N-party round-robin), `ag2-network-workflow` (`TransitionGraph` orchestration / GroupChat migration), `ag2-network-governance` (rules, expectations, audit), `ag2-network-tools-and-views` (custom handlers, views, peer discovery, `say`). One agent fanning out its own sub-tasks is `ag2-subagent-delegation` instead.
In this repository27 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ag2-a2aSkill
- ag2-acpSkill
- ag2-add-custom-toolSkill
- ag2-ag-uiSkill
- ag2-eval-comparisonSkill
- ag2-evaluationSkill
- ag2-hitlSkill
- ag2-knowledge-and-memorySkill
- ag2-liveSkill
- ag2-mcpSkill
- ag2-middlewareSkill
- ag2-multimodal-inputSkill
- ag2-network-discussionSkill
- ag2-network-governanceSkill
- ag2-network-tools-and-viewsSkill
- ag2-network-workflowSkill
- ag2-observers-and-alertsSkill
- ag2-overviewSkill
- ag2-quickstartSkill
- ag2-shell-toolSkill
- ag2-structured-outputSkill
- ag2-subagent-delegationSkill
- ag2-telemetrySkill
- ag2-testingSkill
- ag2-use-builtin-toolsSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 134
Use alice.agent_id for routing (audience, graphs).
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
AG2 Network — Quickstart
A Hub holds authoritative state (registry, audit log, per-channel write-ahead log). Each Agent is registered through a HubClient and gets back an AgentClient. Agents talk in channels; every send goes through the hub, so every exchange is replayable. The network is opt-in — bare Agent still works standalone.
When to use
"Have two agents talk", "multi-agent system / agent network", "agents that call each other", "replace GroupChat", "human in a multi-agent chat", "audit trail for my agents". Not for one agent spawning its own sub-tasks → ag2-subagent-delegation.
| Concept | Purpose |
Hub | Registry, audit log, channel table, WAL, sweepers (one per network) |
HubClient | Per-process broker; register(...) / register_human(...) |
AgentClient | Network handle for a registered Agent (.agent_id, .open, .wait_for_channel_event) |
HumanClient | Non-LLM participant (UI, bridge, scripted seeder) |
Passport / Resume | Identity (name, hub-stamped agent_id, kind) / capability claims |
Envelope | Hub-stamped record of one event in one channel |
Smallest network (executed against ag2 1.3.0)
import asyncio
from ag2 import Agent
from ag2.config import AnthropicConfig
from ag2.knowledge import MemoryKnowledgeStore
from ag2.network import EV_CHANNEL_CLOSED, EV_TEXT, Hub, HubClient, LocalLink, Passport, Resume
async def main() -> None:
config = AnthropicConfig(model="claude-sonnet-5") # any provider config works, e.g. OpenAIResponsesConfig(model="gpt-4.1-mini") — see ag2-quickstart
hub = await Hub.open(MemoryKnowledgeStore(), ttl_sweep_interval=0)
link = LocalLink(hub) # in-process transport
alice_hc, bob_hc = HubClient(link, hub=hub), HubClient(link, hub=hub)
alice = await alice_hc.register(
Agent("alice", prompt="Ask one focused question and stop.", config=config),
Passport(name="alice"), Resume(), attach_plugin=False,
)
bob = await bob_hc.register(
Agent("bob", prompt="Answer in one short sentence.", config=config),
Passport(name="bob"), Resume(), attach_plugin=False,
)
try:
# consulting = strict 1Q1R; bob's default handler auto-acks, answers, channel auto-closes.
channel = await alice.open(type="consulting", target="bob")
await channel.send(
"What's the single most important property of a distributed system?",
audience=[bob.agent_id],
)
close_env = await alice.wait_for_channel_event(
channel_id=channel.channel_id,
predicate=lambda e: e.event_type == EV_CHANNEL_CLOSED,
timeout=60.0,
)
print("closed:", close_env.event_data["reason"]) # 'consulting_complete'
# Transcript, in order, from the hub's WAL.
for env in await hub.read_wal(channel.channel_id):
if env.event_type == EV_TEXT:
print(f"{hub.name_for(env.sender_id)}: {env.event_data['text']}")
finally:
await alice_hc.close()
await bob_hc.close()
await hub.close()
asyncio.run(main())Registered agents answer automatically: the default notify handler runs Agent.ask on each inbound envelope and posts the reply. attach_plugin=False skips the NetworkPlugin tools (peers / channels / tasks / context / delegate); keep the default if agents need them.
Pick a channel adapter
type= | Participants | Turn order | Ends | Skill |
consulting | 2 | initiator → respondent, once | auto ("consulting_complete") | this one |
conversation | 2 | free-form | channel.close() / TTL / empty reply | this one |
discussion | 2+ | fixed round-robin | channel.close() / TTL | ag2-network-discussion |
workflow | 2+ | TransitionGraph | graph TerminateTarget / Finish / max_turns | ag2-network-workflow |
conversation: both sides run Agent.ask on every inbound EV_TEXT, so it auto-drives — cap it in your code (count EV_TEXT in the WAL, then channel.close(reason=...)) or give agents a close tool.
Reading what was said
hub.read_wal(channel_id)→ every envelope in commit order (invites, acks, text, close).EV_TEXT→event_data["text"](consulting / conversation / discussion turns).EV_PACKET→event_data["body"](every agent turn on aworkflowchannel;routingsays how it handed off).hub.name_for(env.sender_id)→ passport name. Close reason:EV_CHANNEL_CLOSED.event_data["reason"]or(await hub.get_channel(cid)).close_reason.
A human participant
user = await user_hc.register_human(Passport(name="user", kind="human")) # register() rejects kind="human"
channel = await user.open(type="consulting", target="bob")
await channel.send("Capital of France?", audience=[bob.agent_id])
reply = await user.next_envelope(
predicate=lambda e: e.event_type == EV_TEXT and e.sender_id == bob.agent_id, timeout=60
) # raises TimeoutError
print(reply.event_data["text"])HumanClient has no LLM; it auto-acks invites. Consume inbound via on_envelope(cb) (push) or next_envelope(...) / envelopes() (pull). It is the standard kickoff seeder for workflow channels.
Agent decides to stop
from ag2.network import ChannelInject
async def end_conversation(reason: str, channel: ChannelInject) -> str:
"""Close the active channel when the topic is done."""
if channel is None: # outside a network turn
return "no active channel"
await channel.close(reason=f"agent_close:{reason}")
return f"closed: {reason}"
alice_agent.tool(end_conversation) # attach before register()Expected noise: after the tool closes the channel, the default handler's round-end reply is rejected and logged (post_envelope rejected ... is closed, notify handler raised). It is harmless.
Gotchas
- Use
alice.agent_idfor routing (audience, graphs);target=inopenaccepts a name. wait_for_channel_event/next_enveloperaise builtinTimeoutError— catch that.- Pair
Hub.openwithhub.close()and everyHubClient.close()infinally. - An invitee that doesn't ack within
invite_ack_timeout(30s) makesopenraiseProtocolError("invite_timeout"). - Names are unique per hub and must not look like an agent id (32 lowercase hex chars) — both raise
ProtocolError. - Cross-process:
serve_wsrefuses a non-loopback bind on a hub that still acceptsNoAuth— seeag2-network-governance.
Reference
references/channels-and-lifecycle.md— consulting/conversation internals and expectations, plugin vs adaptersaytool, lifecycle diagram, five close routes and adapter matrix,Passport/Resume/PassportKind,HumanClientAPI, envelopes, imports.- Next:
ag2-network-discussion,ag2-network-workflow,ag2-network-governance,ag2-network-tools-and-views. - Upstream:
website/docs/user-guide/network/(quick_start.mdx,consulting.mdx,conversation.mdx,human_client.mdx,termination.mdx).
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

