Scouty runs on Mermail

Explore
Back to blog
Email6 min read

Why Your Agent's Email Threads Break (and How to Keep Them Intact)

An email thread is not stored on a server. It is rebuilt from three headers. Here is what breaks them and how to keep your agent's conversations whole.

By Toan Nhu

Mermail field guide cover reading Keep the thread intact., with a chain of Message-ID, In-Reply-To and References header cards

Picture a customer replying to your support agent with "Yes, go ahead." Three words, no context. The agent treats it as a new conversation, has nothing to go on, and asks the customer what they mean. The customer was answering a question the same agent asked an hour earlier.

The message was fine. The thread was broken. For an email agent, that is the same as losing its memory mid-sentence.

A thread is not stored anywhere

It is tempting to think of an email thread as a folder on a server. It is not. Every message travels on its own, and the conversation is rebuilt by the reader from a few headers each message carries. If those headers survive, your agent sees one conversation. If they do not, it sees strangers.

Three headers do the work, all defined in RFC 5322:

  • Message-ID is a unique label for one message, written by the sender's system.
  • In-Reply-To holds the Message-ID of the message being answered.
  • References holds the chain of Message-IDs back to the start of the conversation.

Here is what a third message in a conversation looks like:

text
Message-ID: <c3@mail.yourapp.example>
In-Reply-To: <b2@customer.example>
References: <a1@mail.yourapp.example> <b2@customer.example>
Subject: Re: Your refund request

A mail client reads References, finds the earlier messages it already has, and stacks them together. In-Reply-To tells it which one this message answers directly.

Why agents lose threads more than people do

People rarely notice broken threads because they remember the context. Agents only know what the headers and the stored history tell them. The usual culprits:

  • Sending a reply as a fresh message. If your code calls a plain send function and forgets to copy the headers, the reply goes out as a new conversation. This is the most common bug in homemade agents.
  • Tools in the middle that rewrite headers. Some ticketing systems, forwarding rules and marketing relays replace the Message-ID or drop References.
  • Forwarding instead of replying. A forward starts a new chain by design. If a colleague forwards a customer email to your agent, the customer's thread does not come along.
  • Truncated References. Long conversations can get their References header trimmed, so the oldest messages fall out of the chain.
  • Replies from a different device or client. Usually fine, but older or unusual clients sometimes send only In-Reply-To, or neither header.

Why subject matching is a trap

The obvious fallback is to group messages by subject: strip the "Re:" and match. It feels clever and it fails in both directions.

  • Different conversations share subjects all the time. "Invoice", "Quick question" and "Following up" will collide across customers.
  • The same conversation changes subjects. Someone edits the line to "Re: Refund request (updated address)" and the match breaks.
  • Prefixes vary by language and client, so "Re:", "RE:", "AW:" and "SV:" all need handling.

Use subject only as a weak hint after the headers have failed, and never as the reason to merge two conversations on its own.

How Mermail keeps threads intact

Mermail gives each agent its own mailbox, so threading starts from a clean inbox instead of a shared personal one. On top of that:

  • Replies thread themselves. The reply endpoint injects in_reply_to, references and thread_id from the original message. Your agent answers a message and the headers are right without any copying.
  • Every message carries a `thread_id`. You can filter the inbox by it and pass it when you need to.
  • Whole-thread reads. The thread endpoint returns the conversation oldest to newest, so the agent sees the question it asked an hour ago before it reads "Yes, go ahead."
  • Safe context. The context endpoint returns the selected message plus a sanitized, paginated page of its thread, ready to place in a prompt as untrusted data.
  • The raw links are still visible. Each email exposes message_id, in_reply_to and email_references when present, for debugging and secondary correlation. Use the Mermail id for API paths, not the Message-ID.

The same operations are available through Mermail MCP, the CLI and the REST API.

A recovery order for orphaned replies

Even with clean outbound headers, some inbound replies will arrive with a broken chain. Give your agent a fixed order to try, and stop at the first confident match:

  1. Trust the thread the mailbox assigned. If the message has a thread_id with earlier messages, load that thread and move on.
  2. Check In-Reply-To and References against messages you sent. A match to one of your own Message-IDs is strong evidence.
  3. Search the mailbox for the sender. Look for recent conversations with the same address. If there is exactly one open thread, it is a likely match.
  4. Use the quoted text. Many replies include the previous message below the new one. A distinctive sentence from your own earlier email is a good clue.
  5. Ask, do not guess. If nothing matches confidently, draft a short reply asking for context and let a person review it.

Steps 3 and 4 are where an agent with search earns its keep. Give Your AI Agent a Memory of Its Inbox walks through that pattern.

Habits that prevent broken threads

  • Always answer with the reply operation, never a fresh send, when you are responding to a message.
  • Keep the subject line when you reply. Add to it only if you must.
  • Do not let middleware rewrite Message-ID on your agent's outbound mail.
  • Store the Mermail message id and thread_id in your own records, not the subject.
  • Watch for loops. Auto-replies can bounce back and forth inside one thread; this guide to autoresponders and mail loops shows how to stop them.
  • Start with drafts for replies to orphaned messages, where the agent is least sure of the context.

New to agent mailboxes? Start with What Is an Agent Inbox?, then create one in the Mermail console.

FAQ

Which email headers control threading?

Message-ID, In-Reply-To and References. The reader's mail client uses them to rebuild the conversation. Subject is only a weak hint.

Why did my agent's reply start a new thread?

Usually because it sent a new message instead of a reply, so the threading headers were missing. In Mermail, use the reply endpoint and the headers are set from the original message.

Can I group emails by subject line?

Only as a last resort. Different conversations share subjects and the same conversation can change its subject, so subject matching both merges and splits threads by mistake.

What should an agent do with a reply that has no thread?

Try the assigned thread, then the headers, then a search by sender and quoted text. If nothing matches confidently, draft a question for a person to review instead of guessing.

Should I use Message-ID as the key in my database?

Use Mermail's message id and thread_id for API calls and records. Message-ID is useful for debugging and correlation, but it is written by the sender and is not guaranteed to be clean.

References

Recent articles