Run an AG2 swarm with context variables for shared state and conditional handoffs

domain: docs.ag2.ai · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Install ag2: pip install ag2
  2. Create ConversableAgent instances for each specialist, providing name, system_message, and llm_config
  3. Define a context_variables dict with initial shared state (e.g. {'order_status': 'pending'})
  4. Set handoff conditions using OnContextCondition, pointing to a target agent when a context variable matches a value
  5. Register tool functions on agents using register_for_execution() and register_for_llm(); tool functions receive and mutate context_variables to drive handoff routing
  6. Initiate the swarm with initiate_swarm_chat(initial_agent, agents, messages, context_variables=context_variables)

Known gotchas

Related routes

Automate unit access-state transitions (reserve, grant access, overlock, deallocate) via the Stora API
docs.stora.co · 6 steps · unrated
Build a multi-agent handoff workflow with the OpenAI Agents SDK
openai.github.io/openai-agents-python · 6 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans