Add email sending, reading, and management to BabyAGI's autonomous task loops — with oversight modes that prevent unreviewed email delivery.
BabyAGI is one of the earliest autonomous agent frameworks, demonstrating how LLMs can create, prioritize, and execute tasks in a self-directing loop. When these autonomous loops include email operations, human oversight becomes critical — a BabyAGI agent can generate email-sending tasks unpredictably as it pursues its objectives.
MultiMail provides the email infrastructure layer that makes BabyAGI email tasks safe. The default gated_send mode ensures that every email composed by an autonomous task loop requires human approval before delivery, preventing runaway automation from sending unreviewed messages.
Integration is straightforward: define email functions that call the MultiMail REST API, then register them as available tools in BabyAGI's task execution environment.
BabyAGI's task loops run autonomously and can generate email tasks without explicit user intent. MultiMail's oversight modes gate every outbound email, ensuring a human reviews messages before they leave — essential when the agent is self-directing.
Start with gated_all mode where every email action (including reads) requires approval. As the BabyAGI loop proves reliable, progress to gated_send (reads are free, sends need approval) and eventually monitored or autonomous.
Every email action through MultiMail is logged with context. You can trace which BabyAGI task triggered an email send, helping debug unexpected behavior in the autonomous loop.
BabyAGI may create follow-up email tasks across multiple loop iterations. MultiMail's thread tracking ensures replies stay within the correct conversation thread regardless of when the agent generates the follow-up task.
import requests
MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}
def send_email(to: str, subject: str, body: str, mailbox_id: str) -> dict:
"""Send an email. In gated_send mode, queues for human approval."""
resp = requests.post(f"{MULTIMAIL_API}/send", headers=HEADERS, json={
"mailbox_id": mailbox_id,
"to": to,
"subject": subject,
"body": body
})
return resp.json()
def check_inbox(mailbox_id: str, limit: int = 10) -> dict:
"""Check inbox for new messages."""
resp = requests.get(
f"{MULTIMAIL_API}/mailboxes/{mailbox_id}/inbox",
headers=HEADERS,
params={"limit": limit}
)
return resp.json()
def reply_email(message_id: str, body: str) -> dict:
"""Reply to an email within its thread."""
resp = requests.post(f"{MULTIMAIL_API}/reply", headers=HEADERS, json={
"message_id": message_id,
"body": body
})
return resp.json()Create email functions that BabyAGI can invoke as part of its task execution.
from babyagi import BabyAGI
"cm"># Define available tools for the execution agent
email_tools = {
"send_email": send_email,
"check_inbox": check_inbox,
"reply_email": reply_email
}
"cm"># Create the BabyAGI instance with email capabilities
agi = BabyAGI(
objective="Monitor the support inbox and draft responses to "
"customer inquiries. All emails use gated_send mode "
"so a human must approve before delivery.",
first_task="Check the support inbox for unread messages",
tools=email_tools
)
"cm"># Run the autonomous loop
agi.run(max_iterations=10)Wire the email functions into BabyAGI's task execution agent so it can invoke them during autonomous loops.
import requests
import time
MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}
MAILBOX_ID = "your_mailbox_id"
def autonomous_email_loop(max_iterations: int = 5):
"""BabyAGI-style loop that processes emails with oversight."""
for i in range(max_iterations):
"cm"># Task: Check inbox
inbox = requests.get(
f"{MULTIMAIL_API}/mailboxes/{MAILBOX_ID}/inbox",
headers=HEADERS,
params={"limit": 5}
).json()
if not inbox.get("messages"):
print(f"Iteration {i+1}: No new messages")
break
for msg in inbox["messages"]:
"cm"># Task: Read and process each email
email = requests.get(
f"{MULTIMAIL_API}/emails/{msg[&"cm">#039;id']}",
headers=HEADERS
).json()
# Task: Draft a reply (sent to gated_send queue)
reply = requests.post(
f"{MULTIMAIL_API}/reply",
headers=HEADERS,
json={
"message_id": msg["id"],
"body": f"Thank you for your message. "
f"We are reviewing your inquiry."
}
).json()
print(f"Draft reply queued for approval: {reply}")
time.sleep(2) "cm"># Pause between iterations
autonomous_email_loop()Implement a controlled BabyAGI loop that processes inbox emails with oversight checks.
Sign up at multimail.dev, create a mailbox, and generate an API key from your dashboard. Your key will start with mm_live_.
Install BabyAGI and the HTTP library for calling the MultiMail API.
pip install babyagi requestsCreate Python functions that wrap MultiMail API endpoints for send_email, check_inbox, and reply_email. These become available tools in BabyAGI's execution environment.
Set up BabyAGI with an objective that includes email tasks. Include the oversight mode in the system prompt so the agent understands emails will be queued for review.
Monitor the MultiMail dashboard for pending emails generated by the autonomous loop. Review and approve or reject each message before it is delivered.
Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.