Why Your AI Agent Loses Context After Three Email Replies
Your AI email agent handles the first two replies flawlessly. It reads the original message, drafts a thoughtful response, and sends it on time. Then reply three arrives and something quietly goes wrong. The agent responds to the wrong topic. It asks a question the customer already answered. It loses the thread entirely.
This is not a reasoning failure. The underlying language model did not forget anything. The problem is infrastructure: the email headers that stitch a conversation together are either missing, truncated, or misread by the receiving client. By the time a thread hits three or more messages deep, the structural glue that email relies on has often already snapped.
How Email Threads Are Actually Built
Every email message carries a unique identifier called a Message-ID. When you reply to that message, your client adds two headers: In-Reply-To, which holds the parent message's Message-ID, and References, which holds the entire ancestry chain.
As defined by RFC 2822 (IETF), the References header must equal the parent message's own References field plus the parent's Message-ID appended at the end. Each new reply extends this chain. So by message four, the References header contains three Message-ID values, each pointing one step further back.
This chain is how email clients group messages into a visible thread. Without it, each reply appears as a disconnected new conversation. Different email clients handle threading differently: Gmail uses References together with In-Reply-To and subject matching, Outlook leans primarily on References, and Apple Mail follows RFC 2822 most closely. Omitting the References header outright breaks threads after three or more messages in every major client.
Why the Break Happens at Reply Three (Not Reply One or Two)
The first reply only needs In-Reply-To. Many email clients and APIs get that right automatically. The second reply needs both In-Reply-To and a short References chain. Most still manage this correctly. But the third reply requires your sending code to correctly read and forward the entire References chain from the previous message, append the previous message's Message-ID, and then set the new In-Reply-To to point at that same previous message.
If any step in that chain is wrong, not just the most recent one, clients diverge. According to Alibaba LifeTips research on Outlook threading, Outlook splits 31.4% of threads that Gmail preserves intact. Outlook's threading is folder-bound and more sensitive to subject-line changes, while Gmail treats subject-matching as a fallback rather than a primary signal. A thread that looks continuous in Gmail may already be fragmented in Outlook before your agent sees it.
This is the compound problem. Your agent does not receive a clean, continuous conversation object. It receives fragments: some messages grouped, some orphaned, some duplicated across quoted footers. As one developer put it in a discussion on r/AI_Agents, roughly 80% of the tokens in a real email thread are duplicate quotes and footers, not new information. The same thread "evolves into a substantial infrastructure project" once you try to use it reliably in production.
The Token Waste and Context Collapse Problem
Before the headers even matter, there is a tokenization problem. When your agent fetches a thread naively, it is likely reading the same content four or five times because each reply quotes the previous one in full. The new information in message five may be two sentences. The payload you are feeding your agent may be four thousand tokens of redundant history.
This is a practical, measurable problem. The developer thread on r/AI_Agents describes direct measurements: roughly 80% of tokens in a real email thread are duplicate quotes, signatures, and footers. Your agent is spending most of its context window processing information it has already seen.
Then, when the References chain is broken, your agent cannot reliably determine which message is the true root of the conversation, which messages have already been handled, or what the correct In-Reply-To value for its outgoing reply should be. At that point, even a perfectly capable reasoning engine will produce incoherent replies because the input it received was incoherent. The agent did not fail. The input pipeline failed.
According to the Composio 2025 AI Agent Report, integration failure rather than language model failure is the primary cause of AI agent production failures. Email threading is a textbook example of this pattern. The model is fine. The plumbing is broken.
The Wrong Way: Manual Header Construction
Many developers, when building email reply logic, reach for client.messages.send() with manually constructed headers. It looks like this:
import { MailbotClient } from '@yopiesuryadi/mailbot-sdk';
const client = new MailbotClient({ apiKey: 'mb_test_xxx' });
// WRONG: manually constructing threading headers
const response = await client.messages.send({
inboxId: 'inbox_abc123',
to: [{ email: 'customer@example.com' }],
subject: 'Re: Your support request',
bodyText: 'Thank you for your message...',
headers: {
'In-Reply-To': '<original-message-id@mail.example.com>',
'References': '<original-message-id@mail.example.com>',
// You are now responsible for reading and appending the full References chain
// Get this wrong once and Outlook splits the thread. Forever.
},
});
The problem with this approach is not that it is hard to write. It is that it requires you to correctly read the full References chain from the previous message, append the new Message-ID, and keep this logic accurate across every environment that may format or truncate headers differently. Miss it once on reply three, and your agent is now operating on a broken thread for every reply that follows.
The Right Way: client.messages.reply()
The client.messages.reply() method exists specifically to handle this. It reads the correct Message-ID from the parent message, builds the full References chain by reading the parent's own References header, and sets both In-Reply-To and References correctly before sending. You do not touch headers at all.
import { MailbotClient } from '@yopiesuryadi/mailbot-sdk';
const client = new MailbotClient({ apiKey: 'mb_test_xxx' });
// RIGHT: let the SDK handle threading headers automatically
const reply = await client.messages.reply({
inboxId: 'inbox_abc123',
messageId: 'msg_xyz789', // the specific message you are replying to
bodyText: 'Thank you for following up. Here is what we found...',
});
That single call handles RFC 2822 compliance, correct References chain construction, and In-Reply-To assignment. No manual header management, no risk of chain truncation.
Fetching Full Thread Context Before You Reply
The other half of the problem is making sure your agent actually reads the full thread before composing a reply. The correct pattern is to call client.threads.get() first, which returns the complete message history for the thread as a structured object, then pass that context to your agent, and only then call client.messages.reply().
import { MailbotClient } from '@yopiesuryadi/mailbot-sdk';
const client = new MailbotClient({ apiKey: 'mb_test_xxx' });
// Step 1: Get the full thread so your agent has complete context
const thread = await client.threads.get(inboxId, threadId);
// Step 2: Extract message bodies in order (skip duplicate quoted sections)
const messages = thread.messages.map((msg) => ({
from: msg.from,
date: msg.date,
body: msg.bodyText,
}));
// Step 3: Feed structured thread to your agent, get a reply draft
const replyDraft = await yourAgentLogic(messages);
// Step 4: Reply using the SDK so headers are handled correctly
const sent = await client.messages.reply({
inboxId,
messageId: thread.messages.at(-1)?.id, // reply to the most recent message
bodyText: replyDraft,
});
This pattern gives your agent structured, deduplicated context rather than a raw chain of quoted bodies. It also ensures the reply is attached to the correct message in the thread, which is what determines whether Gmail, Outlook, and Apple Mail all show it in the right place. The deduplication step in particular matters: by passing only the unique message bodies in chronological order instead of four thousand tokens of repeated quoted text, your reasoning engine operates on clean input. Your agent processes less and produces more accurate output.
What Consistent Threading Actually Unlocks
When your agent maintains thread continuity reliably, several things improve at once. The conversation history your agent reads is accurate instead of fragmented. Reply rates from customers tend to rise because responses feel contextually aware rather than generic. Thread-level event history through client.events.list(threadId) becomes useful for auditing what the agent did and when.
More importantly, you stop debugging ghost threads in Outlook and wondering why a customer says "I already answered that." This much is clear: the clients that handle threading well do so because they construct headers by the spec. The ones that break threads do so because they take shortcuts. Your agent should not take shortcuts either.
Email is still the dominant communication channel for business workflows. An AI agent that loses thread context after three replies is not a production agent. It is a prototype that creates cleanup work for your team. Getting the infrastructure right is not optional, and as the r/AI_Agents community noted, most teams learn this the hard way after shipping.
You do not have to.
Start building thread-aware email agents at getmail.bot/docs/getting-started.