← All writing

Using Apps Script and Gemini to Triage Gmail

I used Google Apps Script and Gemini to classify incoming Gmail messages and make a crowded inbox easier to scan.

My inbox was full of newsletters, receipts, and vague subject lines. I wanted to know what deserved attention before opening every message.

The email problem

Subscriptions, work updates, and personal messages had turned my inbox into a long list of unknowns. The volume was inconvenient, but the bigger problem was uncertainty.

Many subject lines did not tell me whether a message was important or promotional. I was spending time opening messages that did not need a reply and skimming messages that deserved more attention.

The approach

Because I already used Gmail, Google Apps Script was a practical place to build the automation. Gemini would classify each message using its sender, subject, and a short content sample.

The script applies Gmail labels such as Financial, Newsletter, and Promotional Spam. The labels turn a crowded inbox into something I can scan quickly.

Designing the classifier

The prompt gives Gemini a fixed set of categories and rules for choosing among them. Sender domains provide a useful signal, while subject lines and content help resolve ambiguous cases.

For example, an email from a known newsletter source may become Newsletter. A message containing an invoice or transaction reference may become Financial.

The current categories are Career, Entertainment, Financial, Newsletter, Purchase/Receipt, System, Travels, and Promotional Spam. I used an LLM to help refine the wording as I tested the prompt.

Gmail labels created by the classification workflow

How the script works

The script runs on a schedule and processes unread messages within a selected date range.

  • Configuration: It defines the categories and creates them in Gmail when needed.
  • Extraction: It reads the sender, subject, and relevant message content.
  • Classification: It sends the prompt to Gemini and receives one category.
  • Labeling: It applies the category and an LLM-Categorized marker.
  • Failure handling: An unknown result receives only the marker, so the message is not silently assigned the wrong label.

The script processes messages in batches and pauses between requests. It also watches the execution time because Google Apps Script has a limit on how long one run can continue.

What worked and what did not

The system handles many messages in one run and removes a lot of manual sorting. It is not perfect: ambiguous emails are sometimes misclassified, so I still review the labels.

That limitation is important. The automation is a triage layer, not a replacement for reading important messages. It helps me decide where to look first.

For a personal inbox, Apps Script plus an LLM was enough to produce a useful result without building a separate service.