惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
F
Full Disclosure
Cloudbric
Cloudbric
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
N
News and Events Feed by Topic
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
B
Blog
GbyAI
GbyAI
C
Check Point Blog
B
Blog RSS Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
N
News and Events Feed by Topic
T
The Blog of Author Tim Ferriss
O
OpenAI News
V
V2EX
人人都是产品经理
人人都是产品经理
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Security @ Cisco Blogs
C
Cisco Blogs
Security Latest
Security Latest
S
Security Affairs
V
Visual Studio Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
Last Week in AI
Last Week in AI
AWS News Blog
AWS News Blog
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
博客园_首页
U
Unit 42
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
N
Netflix TechBlog - Medium
L
LINUX DO - 热门话题
H
Hacker News: Front Page

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Keep your AI agent's email replies in the right thread
Qasim · 2026-06-23 · via DEV Community

An AI agent sends an email, a reply lands three hours later, and the agent has to answer two questions before it can do anything useful: which conversation is this, and what did I last say? Get the first one wrong and the agent's reply shows up in the recipient's inbox as a brand-new message instead of slotting into the existing thread. To the person on the other end, that looks broken — like the agent forgot the conversation it started.

Threading is the part of agent email that's easy to get almost right and quietly wrong. The fix lives in a few email headers most developers never touch, and in the Threads API that groups messages into conversations for you. This post walks through both, from two angles: the HTTP API for your backend, and the Nylas CLI for the terminal. I work on the CLI, so the terminal commands below are the ones I reach for when I'm testing a reply loop.

The three headers that make threading work

Threading runs on three email headers, not on subject lines. Every message carries a Message-ID — a globally unique identifier the sending server stamps on it. When someone replies, their mail client adds In-Reply-To (the Message-ID of the message being answered) and References (the full chain of Message-ID values, oldest to newest). Those two headers are how every mail client decides which messages belong together.

Here's what the chain looks like across one exchange. The agent's first message gets a Message-ID; the reply points back at it; the agent's follow-up references both:

# The agent's outbound message
Message-ID: <abc123@agents.yourcompany.com>
Subject: Following up on your demo request

# The recipient's reply
Message-ID: <def456@gmail.com>
In-Reply-To: <abc123@agents.yourcompany.com>
References: <abc123@agents.yourcompany.com>
Subject: Re: Following up on your demo request

# The agent's follow-up
Message-ID: <ghi789@agents.yourcompany.com>
In-Reply-To: <def456@gmail.com>
References: <abc123@agents.yourcompany.com> <def456@gmail.com>

The References header grows with every message. By the time a thread is five messages deep, it carries five Message-ID values in order — a complete audit trail of the conversation that Gmail, Outlook, Apple Mail, and Thunderbird all read the same way.

Why subject-line matching breaks

Matching replies by subject line is the trap most agent implementations fall into: if a subject starts with Re: and contains the original text, treat it as a reply. It works in testing and fails in production, for three concrete reasons. Subject matching has no way to tell two conversations apart when they share a subject, and no way to follow one when the subject changes.

  • Recipients edit subjects. A reply to "Q3 budget review" comes back as "Re: Q3 budget review — updated numbers attached", and a naive contains-match still works, until the edit drops the original words entirely.
  • Multiple threads share a subject. Two prospects both get "Following up on your demo request". A reply to either one matches both, and the agent can't tell which prospect answered.
  • Forwards reuse the subject. Someone forwards the thread to a colleague who replies. The subject is unchanged but the conversation context is completely different.

The header approach has none of these failure modes because In-Reply-To and References point at specific Message-ID values, not human-readable text. Match on headers first and fall back to subject only when headers are missing, which is rare enough to treat as a broken-client edge case.

How Nylas preserves threading

Nylas keeps the threading chain intact however a message moves through the mailbox, which means your agent never has to generate a Message-ID or hand-assemble a References header. Threading holds on both outbound paths and on inbound mail alike.

  • API sends (POST /v3/grants/{grant_id}/messages/send): pass reply_to_message_id and Nylas fetches the original's Message-ID, then sets In-Reply-To and References on the outbound message automatically. Sending an existing draft behaves the same way, since a draft send runs through the same path.
  • SMTP submission (port 465 or 587): if a human replies from a mail client connected over SMTP, Nylas preserves the Message-ID, In-Reply-To, and References the client set.
  • Inbound messages: when a reply arrives, Nylas stores the full headers. Read them with fields=include_headers for the complete set, or fields=include_basic_headers to skip the full header payload — which is often larger than the message body itself — and get just Message-ID, In-Reply-To, and References.

That consistency is the reason an agent can send via the API and have a human follow up via IMAP without the thread splitting apart. Both paths write to the same mailbox and share the same header chain.

List and fetch threads with the Threads API

Rather than parse In-Reply-To and References yourself, ask the Threads API for the grouped conversation — its message IDs, participants, and metadata — in one call. A GET against the grant's threads collection returns each thread already grouped, so the agent reads the conversation as a unit instead of stitching messages together by hand.

curl --request GET \
  --url "https://api.us.nylas.com/v3/grants/<GRANT_ID>/threads?limit=10" \
  --header "Authorization: Bearer <NYLAS_API_KEY>"

Each thread object carries the fields an agent needs to decide what to do: message_ids (every message in order), participants (everyone in the conversation), latest_message_received_date and latest_message_sent_date, a snippet of the most recent message, plus subject, unread, starred, and folders. When a reply fires the message.created webhook, the payload includes a thread_id you look up here to pull the full history before responding.

There's one more reason to fetch the thread rather than rely on the webhook payload alone. When a message body exceeds about 1 MB, the trigger name becomes message.created.truncated and the body is omitted to keep the payload small. In that case the agent has the thread_id and message_id but not the text, so a follow-up GET /messages/{message_id} returns the full body it needs to reply. Fetching the thread gives you the conversation's message summaries and IDs; the full body of any single message comes from the messages endpoint.

From an SDK, fetching the thread and its messages is a couple of calls. This is the pattern I use after a webhook fires — get the thread, then reconstruct what was said:

// After receiving a message.created webhook:
const thread = await nylas.threads.find({
  identifier: AGENT_GRANT_ID,
  threadId: message.thread_id,
});

// thread.data.messageIds has the full conversation chain.
const messages = await Promise.all(
  thread.data.messageIds.map((id) =>
    nylas.messages.find({ identifier: AGENT_GRANT_ID, messageId: id }),
  ),
);

Reply in-thread from the API and CLI

A reply that threads correctly is a normal send with one extra field: reply_to_message_id, set to the message you're answering. Nylas reads that ID, pulls the original's Message-ID, and stamps In-Reply-To and References on the outbound message so it lands in the right thread in every recipient's client — and in the agent's own mailbox. From the API:

curl --request POST \
  --url "https://api.us.nylas.com/v3/grants/<GRANT_ID>/messages/send" \
  --header "Authorization: Bearer <NYLAS_API_KEY>" \
  --header "Content-Type: application/json" \
  --data '{
    "reply_to_message_id": "<MESSAGE_ID>",
    "to": [{ "email": "alice@example.com" }],
    "subject": "Re: Following up on your demo request",
    "body": "Thanks for getting back to me, Alice. Here are the next steps..."
  }'

The CLI does the same with one flag. nylas email send takes a --reply-to option that sets reply_to_message_id for you, so Nylas stamps the threading headers and the message lands in the right thread. You still supply the recipient and subject explicitly — it's a normal send that happens to thread, which makes it a quick way to confirm your agent's replies thread before you wire the same call into code:

# Reply in-thread to a specific message
nylas email send \
  --to alice@example.com \
  --reply-to <message-id> \
  --subject "Re: Following up on your demo request" \
  --body "Sounds good, thanks!"

There's no separate reply-all on either surface: to copy everyone on the conversation, list them in --to and --cc on the CLI, or the to and cc arrays in the API body. To inspect what you're replying to first, nylas email read <message-id> prints the message and nylas email list shows the mailbox. The reply you send with either surface appears in the same thread you'd see from GET /threads.

Map threads to your agent's state

The Threads API tells the agent which messages belong together, but not what the agent was doing when the conversation started — which task, which workflow step, which session. That mapping lives in your application, keyed on the thread_id. The reliable pattern has two halves: store the mapping when the agent sends, and look it up when a reply arrives.

// thread_id -> { sessionId, taskId, step, ... }
const threadState = new Map();

// After sending:
threadState.set(sentMessage.threadId, {
  sessionId: currentSession.id,
  taskId: currentTask.id,
  step: "awaiting_reply",
});

// On webhook:
const context = threadState.get(inboundMessage.threadId);
if (context) {
  // A reply to something the agent sent — restore context and continue.
  await resumeTask(context.taskId, inboundMessage);
} else {
  // New conversation — classify and route.
  await triageNewMessage(inboundMessage);
}

In production this map belongs in a database or durable store, not in memory. Email conversations span hours and days, and an in-memory map doesn't survive a process restart — which is exactly when a three-hour-old reply tends to arrive. The thread_id is the right key because Nylas assigns it and it covers the whole conversation, where a single Message-ID covers only one message.

Don't react to your agent's own messages

The message.created webhook fires for every new message in the mailbox, and that includes the ones the agent sends. An agent that replies on every message.created without checking direction will answer its own outbound mail and loop. Before the agent acts, confirm the message came from someone else: check that the sender isn't the Agent Account's own address, and skip anything the agent just sent.

Deduplication on the inbound side matters too. A recipient might reply twice within seconds, firing two message.created events on the same thread, and Nylas redelivers a webhook on a retry if your endpoint is slow to acknowledge. Track the message_id values the agent has already answered and treat a reply as handled once you've sent a response for it. Keying that record on the inbound message_id keeps the agent from sending two replies to the same message.

Things to watch in an agent reply loop

A few threading behaviors trip up agents specifically, because agents react automatically where a human would use judgment. None are hard to handle once you know they exist, but each one causes a visible bug if you don't.

  • thread_id is your primary key, not Message-ID. It's more stable for application logic because Nylas assigns it and it spans the full conversation. Reach for raw headers only when you need them.
  • Don't assume one reply per outbound. A prospect might reply twice, or two people on a thread might both respond. Handle multiple inbound messages on one thread without firing duplicate replies — a dedup check on the inbound message_id is enough.
  • Threads go dormant and come back. Someone replies to a three-week-old thread. If your state mapping has a TTL, decide what the agent does when context has expired: re-read the thread history, escalate to a human, or start fresh.
  • Raw headers are one parameter away. Pass fields=include_basic_headers to any message GET for just Message-ID, In-Reply-To, and References when you need to debug threading directly, instead of the full header set.
  • Fetch the thread before responding. An agent that drafts replies off the single-message webhook payload alone will repeat itself or contradict an earlier message. Pull the thread first.

Wrapping up

Threading for an agent comes down to two habits: always pass reply_to_message_id so replies thread, and key your agent's state on thread_id so it knows what each reply belongs to. Nylas handles the header mechanics on every send path, so the work that's left is in your application — mapping conversations to tasks and deciding what to do when a thread goes quiet, then comes back to life.

Where to go next: