Autonomous AI AgentsModule 3

3.3Agent-to-Agent Communication Protocols

35 min 6 code blocks Practice Lab Homework Quiz (5Q)

Agent-to-Agent Communication Protocols: Orchestrating the Swarm

In a multi-agent swarm, the "Handshake" between agents is the most common point of failure. In this lesson, we learn how to implement Standardized Communication Protocols using JSON and XML tags to ensure zero data loss during inter-agent transfers.

🏗️ The Handshake Hierarchy

  1. Request: Agent A asks Agent B for a specific task.
  2. Payload: Agent A provides the necessary context (JSON).
  3. Validation: Agent B confirms receipt and schema validity.
  4. Handoff: Agent B returns the result or an error code.
Technical Snippet

Technical Snippet: The XML Handshake Pattern

Instruct your agents to communicate using this structure:

xml
<handoff>
  <source_agent>LeadScout</source_agent>
  <target_agent>LeadScorer</target_agent>
  <payload>
    {"email": "ceo@brand.com", "lcp": "3.4s", "crm": "None"}
  </payload>
  <instruction>Score this lead for High-Ticket retention potential.</instruction>
</handoff>
Key Insight

Nuance: Token-Efficient Messaging

Agents love to yap to each other. To save costs and reduce latency, implement a 'Minimalist Protocol': "When talking to other agents, use only JSON. No introductory text. If the payload is valid, start execution immediately."

Practice Lab

Practice Lab: The Agent Handover

  1. Setup: Create two AI threads (Agent A and Agent B).
  2. Command A: Ask Agent A to "Extract a technical fact and format it for Agent B in XML."
  3. Command B: Paste that XML into Agent B and command: "Process this payload and provide a 1-sentence fix."
  4. Verify: Note the 100% precision of the data transfer.

🇵🇰 Pakistan Capstone: The Full Agency Swarm

Put everything together. Design a 5-agent swarm for a Karachi digital agency:

Agent 1: LeadScout → Finds restaurants without websites in DHA/Clifton

xml
<handoff>
  <source_agent>LeadScout</source_agent>
  <target_agent>Auditor</target_agent>
  <payload>{"name": "Kolachi", "phone": "+923001234567", "google_rating": 4.2, "has_website": false}</payload>
</handoff>

Agent 2: Auditor → If has_website=true, runs PageSpeed. If false, notes "No website = opportunity"

Agent 3: Pitcher → Writes cold email using audit data + local Urdu warmth

Agent 4: QC → Reviews email, checks for spam triggers, ensures cultural appropriateness

Agent 5: Deployer → Sends via email engine, logs in CRM, schedules WhatsApp follow-up

The constraint: Agents MUST use JSON payloads only. No "Hi there, I found a lead!" — just data. This keeps costs low (fewer tokens) and reliability high (structured parsing).

This is the exact architecture behind the bots in this ecosystem. You're not learning theory — you're learning the production system.

📺 Recommended Videos & Resources

  • JSON Schema Validation — Ensuring type safety in agent payloads

    • Type: Documentation
    • Link description: Visit json-schema.org for validation patterns
  • Protocol Buffers for Agent Communication — Production-grade serialization

    • Type: Documentation
    • Link description: Visit Google's protocol buffers docs for serialization format
  • Structured Output in LLMs — Forcing AI models to return JSON/XML exactly

    • Type: Documentation
    • Link description: Search OpenAI/Anthropic docs for "JSON mode" or "structured output"
  • Building Reliable Multi-Agent Systems — Error handling in agent handoffs

    • Type: YouTube
    • Link description: Search YouTube for "multi-agent reliability patterns 2025"
  • Pakistani Business Data Standards — Designing schemas for PK context (phone formats, business types)

    • Type: YouTube
    • Link description: Search YouTube for "business data standards Pakistan"

🎯 Mini-Challenge

Design a 3-Agent Handshake (5 minutes)

Your mission: Design the exact XML/JSON schema for 3 agents passing data:

Agent 1 (Scout) → Finds a restaurant lead Agent 2 (Auditor) → Checks their website quality Agent 3 (Pitcher) → Writes cold email

Write out the XML/JSON payload for each handoff:

  1. Scout → Auditor: "Here's the restaurant I found"
  2. Auditor → Pitcher: "Here are the audit results"
  3. Pitcher → Output: "Here's the final cold email"

Include fields like:

  • restaurant_name, phone, city
  • website_speed, google_rating, mobile_friendly
  • email_subject, email_body, urgency_score

Output: Post your 3 payload schemas in XML or JSON format.

🖼️ Visual Reference

code
📊 Agent Communication Handshake Protocol

┌─────────────┐
│   SCOUT     │
│   AGENT     │
└──────┬──────┘
       │
       │ HANDOFF 1 (XML)
       │ ┌────────────────────┐
       │ │<handoff>           │
       │ │ <source>Scout      │
       │ │ <target>Auditor    │
       │ │ <payload>          │
       │ │  {lead data}       │
       │ │ </payload>         │
       │ └────────────────────┘
       ↓
┌─────────────┐
│   AUDITOR   │
│   AGENT     │
└──────┬──────┘
       │
       │ HANDOFF 2 (JSON)
       │ ┌────────────────────┐
       │ │{                   │
       │ │ "source": "Auditor"│
       │ │ "target": "Pitcher"│
       │ │ "payload": {       │
       │ │   "lcp": "2.1s",   │
       │ │   "rating": 4.5    │
       │ │ }                  │
       │ └────────────────────┘
       ↓
┌──────────────┐
│   PITCHER    │
│   AGENT      │
└──────┬───────┘
       │
       │ FINAL OUTPUT
       │ Email + metadata
       ↓
    [DEPLOY]

Rules:
✓ Always use source/target tags
✓ Always validate payload schema
✓ Always include error handling
✓ Always log handoffs for audit
Homework

Homework: The Swarm Handshake Schema

Design a communication schema for a 5-agent swarm targeting Pakistani businesses (Scout -> Auditor -> Pitcher -> QC -> Deployer). Define the exact XML tags and JSON payload each agent must use.

💡 Key Takeaways

  • Agent-to-agent communication requires structured schemas, not natural language. XML tags and JSON payloads eliminate ambiguity between agents operating at different reasoning speeds.
  • The "source/target" tag pattern ensures any agent in the swarm can read any message and know if it's meant for them — critical when agents operate asynchronously.
  • Schema validation at handoff points prevents cascading failures. An agent that receives malformed data should reject and log it, not try to process corrupted state.
  • Audit logs for every handoff are non-negotiable in production. When a swarm goes wrong, the handoff log is your debugging trail.

🇵🇰 Pakistan Case Study: The Agency Swarm That Scaled to 400 Leads/Day

Saad built a 5-agent lead generation swarm for his Islamabad IT consulting agency. Initial version: agents passed raw text between each other. Problem: 30% of handoffs failed silently because agents misinterpreted previous agent's output.

He rewrote every handoff using the XML tag + JSON schema pattern.

Before (broken):

code
Pitcher receives from Auditor:
"The website has issues with page speed and mobile responsiveness.
Contact is probably Ali Hassan, owner."

The Pitcher would sometimes confuse "owner" as the target client's name, sometimes ignore it, sometimes write to "Ali Hassan" when the target was the marketing manager.

After (structured):

xml
<source>AUDITOR_AGENT</source>
<target>PITCHER_AGENT</target>
<payload>
{
  "business_name": "TechSoft Solutions",
  "contact_name": "Ali Hassan",
  "contact_role": "Owner",
  "pain_points": ["slow website (LCP: 8.2s)", "no mobile layout"],
  "priority": "HIGH",
  "recommended_channel": "email",
  "city": "Islamabad",
  "audit_score": 3
}
</payload>

Result of the schema fix:

  • Silent handoff failures: 30% → 0.4%
  • Daily leads processed: 40 → 400 (10x)
  • Time to debug a failure: 45 minutes → 4 minutes (audit log)
  • Human intervention needed: 15%/day → 2%/day

Saad's key insight: "The schema feels like extra work upfront. But it's the difference between a system that works and one that constantly surprises you with wrong outputs."

📋 Complete Handshake Schema Template (5-Agent Swarm)

python
# schemas.py — Copy this for any multi-agent Pakistani business workflow

from dataclasses import dataclass, asdict
from typing import List, Optional
from enum import Enum

class Priority(str, Enum):
    HIGH = "HIGH"      # Score 1-4: immediate action
    MEDIUM = "MEDIUM"  # Score 5-7: queue for next batch
    LOW = "LOW"        # Score 8-10: skip this cycle

class Channel(str, Enum):
    WHATSAPP = "whatsapp"
    EMAIL = "email"
    SKIP = "skip"

@dataclass
class ScoutPayload:
    business_name: str
    city: str
    neighborhood: str
    contact_name: Optional[str]
    phone: Optional[str]
    email: Optional[str]
    website: Optional[str]
    google_rating: Optional[float]

@dataclass
class AuditPayload(ScoutPayload):
    audit_score: int          # 1-10
    priority: Priority
    pain_points: List[str]
    recommended_channel: Channel

@dataclass
class PitchPayload(AuditPayload):
    email_subject: str
    email_body: str
    whatsapp_message: Optional[str]

@dataclass
class QCPayload(PitchPayload):
    qc_status: str            # "APPROVED" / "REJECTED"
    qc_reason: Optional[str]  # If rejected: specific reason

# Usage in agent communication:
def create_handoff(source: str, target: str, payload: dict) -> str:
    import json
    return f"<source>{source}</source><target>{target}</target><payload>{json.dumps(payload)}</payload>"

Lesson Summary

Includes hands-on practice labHomework assignment included6 runnable code examples5-question knowledge check below

Quiz: Agent-to-Agent Communication Protocols

5 questions to test your understanding. Score 60% or higher to pass.