Building a Simple Email Assistant Agent
End-to-end: read inbox → summarize → draft reply → await approval.
Email Assistant Agent Architecture
An email assistant agent follows a standard pipeline: Fetch → Classify → Decide → Draft → Approve → Send. The agent reads unread emails, uses an LLM to classify and draft responses, then waits for human approval before sending. This human-in-the-loop design prevents costly mistakes from fully autonomous email sending.
# Email Assistant Pipeline:
#
# 1. FETCH: Pull unread emails from Gmail API
# 2. CLASSIFY: LLM labels each email
# - 'action_needed': requires a reply
# - 'fyi': informational, no reply needed
# - 'spam': should be archived
# 3. DRAFT: LLM generates reply for 'action_needed' emails
# 4. APPROVE: Human reviews drafts in Gmail UI
# 5. SEND: Agent sends approved drafts
#
# Tools: Gmail API, Anthropic/OpenAI API, json, base64
print('Email assistant pipeline: fetch -> classify -> draft -> approve -> send')Defining Agent Tools
Structure the agent with clear tool definitions. Each tool is a Python function with a specific responsibility. This modularity makes the agent testable, debuggable, and easy to extend with new capabilities.
class EmailAssistantTools:
def __init__(self, gmail_service, llm_client):
self.gmail = gmail_service
self.llm = llm_client
def fetch_unread(self, max_emails=10):
'''Fetch unread emails from inbox.'''
pass
def classify_email(self, subject, body, sender):
'''Ask LLM to classify: action_needed / fyi / spam.'''
pass
def draft_reply(self, subject, body, sender, context):
'''Ask LLM to draft a reply to an email.'''
pass
def create_draft(self, message_id, reply_text):
'''Save draft reply in Gmail for human review.'''
pass
def send_approved_drafts(self):
'''Send all drafts marked as approved.'''
pass
print('Tool-based architecture enables testing each step independently')All lessons in this course
- Connecting to Gmail via the API
- Reading and Sending Emails Programmatically
- Calendar Event Creation and Querying
- Building a Simple Email Assistant Agent