Safe Email for Autonomous BabyAGI Agents

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.

Built for BabyAGI developers

Guardrails for Autonomous Loops

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.

Graduated Trust as Confidence Grows

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.

Task-Aware Audit Trail

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.

Thread Continuity Across Tasks

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.


Get started in minutes

Define Email Tools for BabyAGI
python
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.

Register Email Tools in Task Execution
python
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.

Safe Autonomous Email Loop
python
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.


Step by step

1

Create a MultiMail Account and API Key

Sign up at multimail.dev, create a mailbox, and generate an API key from your dashboard. Your key will start with mm_live_.

2

Install Dependencies

Install BabyAGI and the HTTP library for calling the MultiMail API.

bash
pip install babyagi requests
3

Define Email Tool Functions

Create Python functions that wrap MultiMail API endpoints for send_email, check_inbox, and reply_email. These become available tools in BabyAGI's execution environment.

4

Configure the Autonomous Loop

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.

5

Approve Pending Emails

Monitor the MultiMail dashboard for pending emails generated by the autonomous loop. Review and approve or reject each message before it is delivered.


Common questions

Is it safe to let BabyAGI send emails autonomously?
With MultiMail's gated_send mode (the default), yes. Every email the autonomous loop generates is queued for human approval rather than sent immediately. You maintain full control over what actually gets delivered, while the agent handles composition and prioritization.
How do I prevent BabyAGI from creating too many email tasks?
Set a max_iterations limit on the autonomous loop and include constraints in the objective prompt. MultiMail also enforces plan-level rate limits (200 emails/month on free tier), providing a hard backstop against runaway task generation.
Can BabyAGI manage email threads across loop iterations?
Yes. MultiMail tracks threads automatically using message IDs. When BabyAGI creates a follow-up email task in a later iteration, passing the original message_id to the reply endpoint maintains thread continuity. The agent can also use get_thread to retrieve full conversation history.
What oversight mode should I use with BabyAGI?
Start with gated_all for maximum safety, where even inbox reads require approval. Once you trust the agent's reading behavior, switch to gated_send so only outbound emails need approval. Only move to monitored or autonomous after extensive testing with predictable task patterns.

Explore more

The only agent email with a verifiable sender

Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.