Skip to content

Frameworks and agents · Published 2026-09-29 · Updated 2026-11-06 · AgentiSend

Send email from CrewAI

Give a CrewAI agent one tool that posts to POST /emails with its own budgeted key, and stop when the reply is agent_budget_exceeded.

To send email from CrewAI, give the crew one tool whose function posts to POST /emails with the agent's own key. The key is sending_access and carries a budget. The file below is that tool, called through run, which is the path a crew uses. It is run by the test suite against a local AgentiSend server: the first call sends, and the second message is refused with agent_budget_exceeded. The run does not call a hosted model.

Install

pip install crewai

The agentisend package is standard library only, so pip install agentisend adds no other dependency.

The tool and the loop

"""CrewAI — one tool that posts to POST /emails, and a scripted agent loop.

`tool.run` is the path a crew uses. The first call sends. The second is a
different message, so a key whose budget is one comes back as
agent_budget_exceeded. No model is called.

Install both packages with `pip install agentisend crewai`. The suite runs
this file against the SDK checkout; a fresh project uses the released package.

AGENTISEND_API_KEY is the agent's key, with sending_access and a budget.
MAIL_FROM is an address on a domain you have verified.
AGENTISEND_BASE_URL is optional.
"""

from __future__ import annotations

import json
import os

from crewai.tools import tool

from agentisend import AgentiSend, AgentiSendError, idempotency_key

agentisend = AgentiSend()
TO = "crewai@example.com"
UPDATE = {
    "to": TO,
    "subject": "Ticket 4182 — we are looking into it",
    "text": "Someone from support will reply within the hour.",
    "purpose": "ticket-4182-update",
}
CLOSED = {
    "to": TO,
    "subject": "Ticket 4182 — closed",
    "text": "The ticket is closed.",
    "purpose": "ticket-4182-closed",
}


@tool("send email")
def send_email(to: str, subject: str, text: str, purpose: str) -> str:
    """Send one email to a person who asked for it.

    Returns the message id, or a refusal with a code and a fix. When the fix
    says a person decides, stop and report it: calling again will not change
    the answer.
    """
    try:
        sent = agentisend.send_email(
            {"from": os.environ["MAIL_FROM"], "to": to, "subject": subject, "text": text},
            idempotency=idempotency_key(purpose, to),
        )
    except AgentiSendError as err:
        return json.dumps({"sent": False, "code": err.code, "fix": err.fix})
    return json.dumps({"sent": True, "id": sent["id"]})


def run() -> None:
    """Two tool calls, then stop when the second is refused."""
    for call in (UPDATE, CLOSED):
        raw = send_email.run(**call)
        result = json.loads(raw)
        print(json.dumps(result), flush=True)
        if result.get("sent") is False:
            return


if __name__ == "__main__":
    run()
examples/crewai/tool.py, run by the test suite against a local AgentiSend server

The function is wrapped with CrewAI's @tool("send email") decorator and returns a JSON string, because a CrewAI tool's result goes back to the agent as text. A refusal is caught as AgentiSendError and returned as {"sent": false, "code", "fix"} rather than raised, so the crew reads agent_budget_exceeded and its fix as the tool's answer. The loop in run() stops on that answer instead of calling the tool again.

The key

The crew's key is minted with sending_access and given its budget before the crew starts; An AI agent with a budget and a loop guard runs those calls. One key per crew keeps one crew's spend from eating another's budget.

Next