AI Agent for email assistance using LangGraph

Share it with your senior IT friends and colleagues
Reading Time: 2 minutes

Managing emails efficiently is crucial for professionals in today’s fast-paced digital world.

AI Agents for email assistants can help by classifying emails, responding to critical messages, and even scheduling meetings.

Namaste and Welcome to Build It Yourself.

In this guide, we’ll walk through the process of building an AI email assistant using LangChain (LangGraph) and OpenAI’s models.

If you are a senior It professional and looking to learn AI + LLM in a simple language, check out the courses and other details – https://www.aimletc.com/online-instructor-led-ai-llm-coaching-for-it-technical-professionals/

Building an AI Agent for email assistance

The AI agent we will build can perform the following tasks:

  1. Classify incoming emails
    • Respond to important emails
    • Notify the user about informative emails
    • Ignore marketing or spam emails
  2. Draft responses for important emails
  3. Schedule meetings based on calendar availability

Workflow Breakdown – AI Agent for email assistance

The AI assistant follows a structured workflow:

  1. Triage Router: Determines whether to respond, notify, or ignore an email.
  2. Response Generation: If an email requires action, the assistant drafts an appropriate response.
  3. Meeting Scheduler: If the email requires scheduling a meeting, the assistant checks the user’s calendar availability.

Setting Up the Environment

Code Notebook – https://github.com/tayaln/email-assistant-ai-agent

Before coding, install the necessary libraries:

pip install langchain openai

We will be using OpenAI’s API, so ensure you have an API key:

import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"

Defining the User Profile and Triage Rules

We define the user profile and email classification rules:

user_profile = {
    "name": "Rahul Dravid",
    "background": "Senior Software Engineer leading a team of five developers"
}

triage_rules = {
    "ignore": ["marketing newsletter", "spam email", "mass company announcements"],
    "notify": ["team member out-of-office", "system notifications", "project status updates"],
    "respond": ["direct questions", "meeting requests", "critical bug reports"]
}

Implementing Email Classification

We use GPT-4o-mini to classify incoming emails:

from langchain.schema import BaseModel

class EmailTriage(BaseModel):
    def classify_email(self, email_content):
        if any(keyword in email_content for keyword in triage_rules["ignore"]):
            return "ignore"
        elif any(keyword in email_content for keyword in triage_rules["notify"]):
            return "notify"
        else:
            return "respond"

Implementing AI-Generated Responses

When an email requires a response, the AI drafts a reply:

from langchain.tools import Tool

def write_email(to, subject, content):
    return f"To: {to}\nSubject: {subject}\n\n{content}"

write_email_tool = Tool(
    name="Write Email",
    func=write_email,
    description="Writes and sends an email based on the given input."
)

Implementing Meeting Scheduling

To automate scheduling, we define a tool to check availability:

def check_calendar(day):
    available_times = {"Tuesday": ["9 AM", "2 PM", "4 PM"]}
    return available_times.get(day, "No availability")

calendar_tool = Tool(
    name="Check Calendar",
    func=check_calendar,
    description="Checks available times for scheduling meetings."
)

Putting It All Together

Finally, we integrate all components into the AI assistant:

email = "Hi Rahul, I was reviewing the API documentation and noticed missing endpoints. Can you confirm?"
triage = EmailTriage().classify_email(email)

if triage == "respond":
    response = write_email("anjali.singh@example.com", "Re: API Documentation", "I'll check with the development team and get back to you.")
    print("Response Sent:", response)
elif triage == "notify":
    print("Notification Sent to Rahul.")
elif triage == "ignore":
    print("Email Ignored.")

Conclusion

We have successfully built an AI-powered email assistant using LangChain and OpenAI.

This assistant can classify emails, draft responses, and schedule meetings. Future improvements can include integrating it with email services like Gmail and Outlook.

Stay tuned for more AI automation tutorials!

If you have any queries or suggestions, share them with me on LinkedIn – https://www.linkedin.com/in/nikhileshtayal/

Let’s learn to build a basic AI/ML model in 4 minutes (Part 1)

Happy learning!

Share it with your senior IT friends and colleagues
Nikhilesh Tayal
Nikhilesh Tayal
Articles: 84
💬 Send enquiry on WhatsApp