Communication Between Agents › Handling Communication Failures
Michael BrenndoerferJuly 27, 20252 min read
AI Agent HandbookAll chapters Part 10 of 15Multi-Agent Systems - Teamwork Among AIs
Communication Between Agents › Handling Communication FailuresOne section · Sections in this chapter6 / 11 ↓
Sections in this chapter11
Communication between agents isn't always perfect. Networks fail, messages get lost, agents crash. Robust multi-agent systems need to handle these failures gracefully.
Timeouts
If Agent A sends a request to Agent B and doesn't get a response within a reasonable time, it should timeout and either retry or report an error:
In[10]:
Codeimport time
def send_with_timeout(sender_func, timeout_seconds=5):
"""
Send a message and wait for response with timeout.
"""
start_time = time.time()
try:
# In a real system, this would be async
response = sender_func()
elapsed = time.time() - start_time
if elapsed > timeout_seconds:
return {
"status": "timeout",
"message": f"No response after {timeout_seconds} seconds"
}
return {
"status": "success",
"response": response
}
except Exception as e:
return {
"status": "error",
"message": str(e)
}Retries
For transient failures, retrying can help:
In[11]:
Codedef send_with_retry(sender_func, max_retries=3):
"""
Send a message with automatic retries on failure.
"""
for attempt in range(max_retries):
try:
response = sender_func()
return {
"status": "success",
"response": response,
"attempts": attempt + 1
}
except Exception as e:
if attempt == max_retries - 1:
# Last attempt failed
return {
"status": "failed",
"error": str(e),
"attempts": max_retries
}
# Wait before retrying (exponential backoff)
time.sleep(2 ** attempt)Acknowledgments
For critical messages, require acknowledgments:
In[12]:
Codeclass ReliableMessaging:
"""
Ensures messages are received by requiring acknowledgments.
"""
def __init__(self):
self.pending_acks = {}
def send_reliable(self, message_id, sender, recipient, content):
"""
Send a message that requires acknowledgment.
"""
self.pending_acks[message_id] = {
"sender": sender,
"recipient": recipient,
"content": content,
"sent_at": datetime.utcnow().isoformat(),
"acknowledged": False
}
return AgentMessage(
sender=sender,
recipient=recipient,
message_type="reliable_message",
data={
"message_id": message_id,
"content": content,
"requires_ack": True
}
)
def acknowledge(self, message_id, recipient):
"""
Recipient acknowledges receiving the message.
"""
if message_id not in self.pending_acks:
raise ValueError(f"Unknown message: {message_id}")
self.pending_acks[message_id]["acknowledged"] = True
self.pending_acks[message_id]["acked_at"] = datetime.utcnow().isoformat()
msg = self.pending_acks[message_id]
return AgentMessage(
sender=recipient,
recipient=msg["sender"],
message_type="acknowledgment",
data={"message_id": message_id}
)
def check_pending(self):
"""
Check for messages that haven't been acknowledged.
"""
return {
msg_id: msg
for msg_id, msg in self.pending_acks.items()
if not msg["acknowledged"]
}