DEV Community

Qasim Muhammad
Qasim Muhammad

Posted on • Originally published at cli.nylas.com

Email as Memory for AI Agents — Search, Retrieve, Reason

Every AI agent framework has the same problem: memory. Agents forget what happened between sessions. They don't know what you discussed last Tuesday, what decisions were made over email, or who promised to deliver what by Friday.

Your inbox already stores all of this. Years of conversations, decisions, commitments, and context — timestamped, threaded, and searchable. Email is the most complete memory store most people have, and it's sitting there unused by AI agents.

Nylas CLI gives agents programmatic access to this memory. Search across years of email history, retrieve full threads, and reason over conversation context — all from a subprocess call or MCP tool.

The three types of email memory

1. Semantic memory — facts and knowledge

Who works at which company. What the project timeline is. What was agreed in the contract. These facts are scattered across email threads.

# Search for facts about a specific topic
nylas email search "project timeline Q2" --json --limit 20

# Find the latest message from a specific person about a topic
nylas email search "from:alice@partner.com contract terms" --json --limit 5
Enter fullscreen mode Exit fullscreen mode

2. Episodic memory — events and interactions

What happened at the meeting last week. Who replied to the proposal. When the last deployment went out. These are events anchored in time.

# Get emails from a specific date range
nylas email list --after "2026-03-01" --before "2026-03-15" --json

# Find a specific interaction thread
nylas email search "subject:Sprint review" --json | jq '.[0].thread_id'

# Get the full thread
nylas email thread thr_abc123 --json
Enter fullscreen mode Exit fullscreen mode

3. Procedural memory — how things are done

The deployment checklist someone emailed last month. The onboarding steps HR sent. The incident runbook shared after the last outage.

# Find process documentation
nylas email search "subject:runbook OR subject:checklist OR subject:how to" --json --limit 10
Enter fullscreen mode Exit fullscreen mode

Building a memory-augmented agent

Here's a Python agent that uses email as its long-term memory:

import subprocess
import json
from openai import OpenAI

client = OpenAI()

def search_email_memory(query, limit=10):
    """Search email for relevant context."""
    result = subprocess.run(
        ["nylas", "email", "search", query, "--json", "--limit", str(limit)],
        capture_output=True, text=True
    )
    if result.returncode != 0:
        return []
    return json.loads(result.stdout)

def ask_with_memory(question):
    """Answer a question using email as context."""
    # Search email for relevant messages
    memories = search_email_memory(question, limit=5)

    # Build context from email results
    context = ""
    for email in memories:
        context += f"From: {email['from'][0]['email']}\n"
        context += f"Subject: {email['subject']}\n"
        context += f"Date: {email['date']}\n"
        context += f"Snippet: {email['snippet']}\n\n"

    # Ask the LLM with email context
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{
            "role": "system",
            "content": f"Answer based on the user's email history:\n\n{context}"
        }, {
            "role": "user",
            "content": question
        }]
    )
    return response.choices[0].message.content

# Example usage
print(ask_with_memory("What did Alice say about the Q2 deadline?"))
print(ask_with_memory("Who was the last person to email me about the API redesign?"))
Enter fullscreen mode Exit fullscreen mode

For a complete agent framework with classification and auto-replies, see Build an AI Email Triage Agent.

MCP: give agents direct memory access

Instead of subprocess calls, connect your agent to email via MCP. Claude Code, Cursor, and Codex can then search your inbox directly as a tool:

nylas mcp install --assistant claude-code
Enter fullscreen mode Exit fullscreen mode

Now your agent can answer questions like:

  • "What did the client say about pricing in their last email?"
  • "Find the deployment runbook someone shared last month"
  • "Who was CC'd on the contract discussion?"

Full MCP setup: Give Your AI Agent an Email Address

For details on MCP configuration: Give AI Agents Email Access via MCP

Combine with calendar memory

Email + calendar together give agents a complete picture of your professional context:

# What meetings do I have with Alice this week?
nylas calendar list --from "monday" --to "friday" --json | \
  jq '.[] | select(.participants[]?.email == "alice@company.com")'

# What did we discuss in our last email thread?
nylas email search "from:alice@company.com" --json --limit 5
Enter fullscreen mode Exit fullscreen mode

Calendar guides:

Contact relationship memory

Your inbox reveals relationships: who emails whom, how often, response times, CC patterns. This is organizational memory that no CRM captures automatically.

# Who emails you most?
nylas email list --json --limit 200 | \
  jq -r '.[].from[0].email' | sort | uniq -c | sort -rn | head -20

# Map email relationships
nylas email list --json --limit 500 | \
  jq -r '.[] | .from[0].email + " -> " + (.to[0].email // "unknown")'
Enter fullscreen mode Exit fullscreen mode

For building org charts and relationship maps from email data:

Privacy: keep email memory local

Use a local LLM so email content never leaves your machine:

client = OpenAI(
    base_url="http://localhost:11434/v1",
    api_key="ollama"
)
Enter fullscreen mode Exit fullscreen mode

For audit trail requirements when agents access email: AI Agent Audit Logs

For credential security in automated workflows: Secure Email Handling for CLI

Email memory vs vector databases

Feature Vector DB (Pinecone, etc.) Email via Nylas CLI
Setup Ingest pipeline required Zero setup
Data freshness Stale until re-indexed Always current
Storage cost Paid per vector Already stored
Semantic search Yes (embeddings) Keyword + date
Thread context Lost Preserved
Sender/recipient Metadata only Full context
Attachments Separate handling Included

Email won't replace vector databases for embedding-heavy RAG, but for conversational context and organizational memory, it's already there and always up to date.


Full guide with RAG patterns, thread summarization, and multi-account memory: Email as Memory for AI Agents

Related guides:

All guides: cli.nylas.com/guides

Top comments (0)