Skip to main content
stack-tweaks intermediate 9 min read

Auto-categorize your Gmail inbox in 20 minutes with Vercel AI SDK

Summary. Gmail filters can route by sender or keyword but cannot reason about intent. A 50-line script using the Vercel AI SDK reads new mail, classifies it (Customer / Internal / Vendor / Newsletter / Spam-not-caught), and applies the right Gmail label. Three hours saved a week, indefinitely.

Tools needed: Node 20+, a Google Cloud project (free), Gmail API enabled, Vercel AI SDK, an OpenAI or Anthropic key, a cron runner (cron-job.org free, Vercel Cron, or your own Mac).

The problem

If you run sales or support, your inbox gets 100+ messages a day across 5 categories. Gmail's built-in tabs (Promotions, Updates) miss B2B nuance — a vendor invoice and a customer renewal both look like "Updates". Manual triage at 9am steals 25–40 min daily.

Step-by-step

  1. Create a Google Cloud project, enable the Gmail API, create OAuth credentials, and authorize your inbox once via the official quickstart.
  2. Create a Node project and install: npm i ai @ai-sdk/openai googleapis dotenv.
  3. Create labels in Gmail manually: AI/Customer, AI/Vendor, AI/Internal, AI/Newsletter, AI/Misc.
  4. Write a script that polls the last 20 unread messages with gmail.users.messages.list.
  5. For each message, send the subject + first 500 chars of body to generateObject from the Vercel AI SDK with a Zod schema: { category: enum, confidence: number }.
  6. If confidence > 0.7, call gmail.users.messages.modify to apply the right label.
  7. Schedule the script to run every 10 minutes via cron-job.org or as a Vercel Cron.

Expected outcome

By 9am the inbox is pre-sorted. You scan AI/Customer first (the only label that matters), archive AI/Newsletter in a single click, and triage AI/Vendor + AI/Internal when energy allows. ~3 hours/week reclaimed.

Gotchas

  • The Gmail API has a 1B-quota-per-day, but the OAuth refresh token expires every 7 days for Testing apps. Publish your app (no review needed for personal use) to get a stable token.
  • Use gpt-4o-mini at $0.15/1M input — running every 10 minutes for a year costs ~$2.
  • Newsletter detection is the easiest 80% — start there if you want fast wins before tackling Customer vs Internal.

Time to set up: 20 min. Estimated savings: ~3 hours/week.