An automated email processing system using OpenAI Agents SDK with Ollama (qwen3:8b).
New email arrives to <INSERT EMAIL>
↓
Agent 1: "Triage Agent"
- Analyzes email importance/urgency
- Decides: respond, ignore, or flag for human
↓
Agent 2: "Draft Agent" (if respond)
- Generates reply draft
- Passes to Agent 3
↓
Agent 3: "Quality Checker"
- Reviews draft for tone, accuracy
- Flags if needs human review (sends email with title "FLAGGED EMAIL")
- Python 3.9+
- Ollama installed and running
- Gmail API credentials
# Install Ollama from https://ollama.com
# Then pull qwen3:8b
ollama pull qwen3:8bcd email-agent-workflow
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt- Go to Google Cloud Console
- Create a new project (or select existing)
- Enable the Gmail API:
- Go to "APIs & Services" → "Enable APIs and Services"
- Search for "Gmail API" and enable it
- Create OAuth 2.0 credentials:
- Go to "APIs & Services" → "Credentials"
- Click "Create Credentials" → "OAuth client ID"
- Choose "Desktop app"
- Download the credentials
- Rename the downloaded file to
credentials.jsonand place it in this directory
cp .env.example .env
# Edit .env if needed (defaults should work)python main.pyOn first run, a browser window will open for Gmail authorization. Grant the requested permissions.
# Ensure Ollama is running
ollama serve
# Run the workflow
python main.py- Analyzes each unread email
- Classifies by category (business, personal, spam, etc.)
- Rates urgency (high, medium, low)
- Decides action:
- respond: Pass to Draft Agent
- ignore: Mark as read, skip
- flag_for_human: Send notification, skip auto-reply
- Creates a contextually appropriate reply
- Matches tone to original email
- Addresses all questions/points raised
- Reviews draft for:
- Appropriate tone
- Accuracy
- Completeness
- If approved: sends the reply
- If issues found: saves draft for review
main.py- Workflow orchestrationagents_workflow.py- Agent definitions and toolsgmail_tools.py- Gmail API functionsconfig.py- Configuration (Ollama, email settings)
Edit .env:
OLLAMA_MODEL=llama3.2 # or any other Ollama model
Modify the instructions in agents_workflow.py for each agent.
"Ollama connection refused"
- Ensure Ollama is running:
ollama serve
"Missing credentials.json"
- Complete Gmail API setup (Step 3 above)
"Model not found"
- Pull the model:
ollama pull qwen3:8b