An intelligent email summarization tool that uses Scaledown for context compression and AI to generate concise email summaries.
This project demonstrates how to build an Email Triage Assistant that:
- Fetches emails from your inbox (Gmail, Outlook, etc.)
- Uses Scaledown to compress email content by up to 80% while preserving semantic meaning
- Generates AI-powered summaries to help you quickly triage your inbox
- Identifies priority emails and action items
Scaledown is an open-source Python package for prompt optimization and context engineering. It transforms bloated prompts into precision-engineered context, reducing tokens by 80% while preserving semantic integrity. This makes LLM calls faster and cheaper.
- 📧 Email Fetching: Connect to Gmail via IMAP
- 🔄 Context Compression: Uses Scaledown to reduce email content tokens
- 🤖 AI Summarization: Generates concise summaries with key points
- 🎯 Priority Detection: Identifies urgent/important emails
- 📊 Batch Processing: Process multiple emails efficiently
- 💰 Cost Efficient: Reduces API costs by 80% through compression
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ Email │─────▶│ Scaledown │─────▶│ LLM │
│ Inbox │ │ Compressor │ │ Summarizer │
└─────────────┘ └──────────────┘ └─────────────┘
│
▼
Token Reduction
(60-80%)
- Python 3.8 or higher
- Gmail account (or other email provider)
- Scaledown API key from https://scaledown.ai
- OpenAI API key (or Anthropic Claude API key)
git clone <your-repo-url>
cd email-triage-assistantpython -m venv venv
# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activatepip install scaledown
pip install openai anthropic # Choose your preferred LLM provider
pip install python-dotenvCreate a .env file in the project root:
# Scaledown API Configuration
SCALEDOWN_API_KEY=sk-your-scaledown-api-key
SCALEDOWN_API_URL=https://api.scaledown.xyz
# LLM Provider (choose one)
OPENAI_API_KEY=your-openai-api-key
# OR
ANTHROPIC_API_KEY=your-anthropic-api-key
# Email Configuration (for Gmail)
EMAIL_ADDRESS=your-email@gmail.com
EMAIL_PASSWORD=your-app-specific-password
IMAP_SERVER=imap.gmail.com
IMAP_PORT=993- Visit https://scaledown.ai
- Sign up for an account
- Generate your API key from the dashboard
- Add it to your
.envfile
For Gmail:
- Go to Google Account settings
- Enable 2-Factor Authentication
- Generate an App Password for "Mail"
- Use this password in your
.envfile
from email_triage import EmailTriageAssistant
# Initialize the assistant
assistant = EmailTriageAssistant()
# Process recent emails
summaries = assistant.process_recent_emails(count=10)
# View summaries
for summary in summaries:
print(f"From: {summary['from']}")
print(f"Subject: {summary['subject']}")
print(f"Priority: {summary['priority']}")
print(f"Summary: {summary['summary']}")
print(f"Action Items: {summary['action_items']}")
print("-" * 80)# Process emails with custom compression rate
assistant = EmailTriageAssistant(compression_rate=0.7)
# Filter by date range
from datetime import datetime, timedelta
since_date = datetime.now() - timedelta(days=7)
summaries = assistant.process_emails_since(since_date)
# Batch process with progress tracking
summaries = assistant.batch_process(
count=50,
show_progress=True,
save_to_file="email_summaries.json"
)email-triage-assistant/
├── README.md # This file
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── email_triage.py # Main application code
├── email_fetcher.py # Email fetching utilities
├── compressor.py # Scaledown compression wrapper
├── summarizer.py # LLM summarization logic
├── examples/
│ ├── basic_usage.py # Simple example
│ ├── advanced_usage.py # Advanced features demo
│ └── batch_processing.py # Batch processing example
└── tests/
├── test_compressor.py # Unit tests
├── test_summarizer.py
└── test_integration.py
The system connects to your email inbox via IMAP and retrieves unread or recent emails.
Before sending to the LLM, email content is compressed:
from scaledown import ScaleDownCompressor
compressor = ScaleDownCompressor(
target_model="gpt-4o",
rate="auto" # Automatically determines optimal compression
)
# Original email: 2000 tokens
result = compressor.compress(
context=email_body,
prompt="Summarize this email with key points and action items"
)
# Compressed email: ~400 tokens (80% reduction!)
print(f"Token reduction: {result.metrics.original_prompt_tokens} → "
f"{result.metrics.compressed_prompt_tokens}")The compressed context is sent to an LLM for summarization:
# Using OpenAI
import openai
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are an email triage assistant."},
{"role": "user", "content": result.compressed_prompt}
]
)The system analyzes summaries to detect:
- Urgency indicators (deadlines, "ASAP", "urgent")
- Importance markers (from VIPs, meeting requests)
- Action requirements (tasks, approvals needed)
Without Scaledown:
- 100 emails × 2000 tokens each = 200,000 tokens
- At $0.003/1K tokens = $0.60 per batch
With Scaledown (80% reduction):
- 100 emails × 400 tokens each = 40,000 tokens
- At $0.003/1K tokens = $0.12 per batch
- Savings: $0.48 per batch (80% cost reduction)
- Reduced tokens mean faster LLM responses
- Typical response time improvement: 50-70%
- Better for real-time applications
Scaledown preserves semantic meaning while removing redundancy:
- Email signatures and footers
- Quoted previous messages
- Verbose greetings
- Marketing fluff
# Automatic compression (recommended)
compressor = ScaleDownCompressor(rate="auto")
# Fixed compression rate
compressor = ScaleDownCompressor(rate=0.5) # 50% reduction
# Target token count
compressor = ScaleDownCompressor(
target_tokens=500,
target_model="gpt-4o"
)# Filter by sender
assistant.process_emails(sender_filter="boss@company.com")
# Filter by subject keywords
assistant.process_emails(subject_keywords=["invoice", "urgent"])
# Filter by date
from datetime import datetime, timedelta
since = datetime.now() - timedelta(days=3)
assistant.process_emails(since_date=since)class EmailTriageAssistant:
def __init__(
self,
compression_rate: str = "auto",
llm_provider: str = "openai",
llm_model: str = "gpt-4o-mini"
)
def process_recent_emails(self, count: int = 10) -> List[Dict]
def process_emails_since(self, since_date: datetime) -> List[Dict]
def batch_process(
self,
count: int,
show_progress: bool = True,
save_to_file: str = None
) -> List[Dict]Run the test suite:
# Install test dependencies
pip install pytest pytest-cov
# Run all tests
pytest
# Run with coverage
pytest --cov=. --cov-report=html-
Authentication Failed
- Ensure you're using an App Password for Gmail
- Check that 2FA is enabled on your Google account
-
Scaledown API Error
- Verify your API key is correct
- Check your account has available credits
- Ensure SCALEDOWN_API_URL is set correctly
-
Rate Limiting
- Add delays between batch processing
- Consider caching results
- Use compression to reduce token usage
- Optimize Compression Rate: Start with "auto" and adjust based on results
- Batch Processing: Process emails in batches of 10-20 for best performance
- Cache Results: Store summaries to avoid reprocessing
- Filter Aggressively: Only process emails you need to triage
From: john.doe@company.com
Subject: Q4 Budget Review Meeting - Action Required
Priority: ⚠️ HIGH
Summary:
John requests your review of the Q4 budget proposal before Friday's meeting.
The finance team identified a 15% variance in marketing spend that needs
explanation. Your input on resource allocation for Project Phoenix is needed.
Action Items:
- Review attached Q4 budget spreadsheet
- Prepare variance explanation for marketing department
- Submit Project Phoenix resource requirements by Thursday 5 PM
Token Savings: 1,847 → 312 tokens (83% reduction)
Processing Time: 1.2s
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Submit a pull request
- Scaledown Team for the excellent context compression library
- Built with OpenAI GPT and Anthropic Claude APIs
- Inspired by modern context engineering practices