Skip to content

Blog / All posts

Keep email threading (In-Reply-To / References) when an AI agent replies by API

Set In-Reply-To to the parent's Message-ID and References to the chain, or use an API that sets both from a message id: Bavimail, AgentMail, Nylas, Resend Inboxes, Mails.ai. Who does what, checked Nov 30, 2026.

Originally published

To keep an AI agent's reply in the same email thread, the reply needs two headers: In-Reply-To set to the Message-ID of the message it answers, and References set to that message's References (or its In-Reply-To, if it has no References) followed by its Message-ID. Keep the original subject with a Re: prefix and send from the address the message was sent to.

You can build those headers yourself, or use an email API that builds them from a message or thread id. As of November 30, 2026, Bavimail, AgentMail, Nylas, Resend Inboxes and Mails.ai set them for you; Amazon SES, Postmark, Mailgun, SendGrid, the Gmail API and Resend's plain send endpoint make you pass them as custom headers. We build Bavimail; every other row below is from the vendor's own documentation.

Which email APIs set the threading headers for you?

APISets In-Reply-To and References?HowChain limit
BavimailYesthread_id and/or in_reply_to on POST /outbound-emailsFirst Message-ID plus the last 9
AgentMailYesPOST /v0/inboxes/{inbox_id}/messages/{message_id}/reply (and reply-all)Not documented
NylasYesreply_to_message_id on POST /v3/grants/{grant_id}/messages/sendNot documented
Resend InboxesYes; you cannot set them yourselfPOST /inboxes/{inbox_id}/threads/{thread_id}/emails/{email_id}/replyNot documented
Mails.aiYesPOST /v1/messages/{id}/reply, or in_reply_to_message_id and references by handNot documented
Resend (Receiving + emails.send)NoPass In-Reply-To and References in headersNot documented
PostmarkNoPass them in the Headers arrayNot documented
MailgunNoh:In-Reply-To and h:References custom headersAll h:, o:, v:, t: options together are capped at 16 KB
SendGridNoThe headers objectNot documented
Amazon SESNoContent.Simple.Headers, or a raw messageSimple sends: 15 headers, values up to 995 characters; longer chains need Content.Raw
Gmail APINoSet them in the raw RFC 2822 message, plus threadId and a matching subjectNot documented

How do you build the headers yourself?

When the API leaves it to you, read three headers from the message you are answering (the parent) and build the reply from them:

  1. In-Reply-To: the parent's Message-ID, with its angle brackets: <[email protected]>.
  2. References: the parent's References, then the parent's Message-ID, separated by spaces. If the parent has no References, use its In-Reply-To (if any) followed by its Message-ID.
  3. Subject: the parent's subject, with Re: added once.
  4. From: the address the parent was sent to, so the next reply comes back to the same agent.

Store Message-ID, In-Reply-To and References for every inbound message when it arrives, so the agent can reply later without re-fetching the original. Don't build threads from the subject line: senders change it, and unrelated messages share it.

How does Bavimail do it?

Every inbound and outbound message gets a thread_id. An arriving message joins an existing thread when its In-Reply-To, or failing that one of its References, names a message already in your account; the subject is never used.

To reply, pass the inbound message's thread_id and its message_id as in_reply_to on POST /outbound-emails. Bavimail builds both headers:

  • In-Reply-To is the in_reply_to you pass. If you pass only thread_id, it is the newest message in that thread.
  • References lists the Message-IDs of the thread's messages, inbound and outbound, in order. Past 10 ids it keeps the first one and the last 9.
  • If you pass only in_reply_to, Bavimail finds the thread that message belongs to and builds the same chain. Missing angle brackets are added.
  • Setting In-Reply-To or References in custom headers is refused with a 400, because they are built from the thread.

Bavimail doesn't change the subject, so add Re: yourself, and send from the inbox that received the message (inbox_id).

import { Bavimail } from 'bavimail'

const client = new Bavimail({ apiKey: process.env.BAVIMAIL_API_KEY! })

export async function replyToInbound(emailId: string, replyHtml: string) {
  const inbound = await client.inboundEmails.get(emailId)
  const subject = /^re:/i.test(inbound.subject) ? inbound.subject : `Re: ${inbound.subject}`

  return client.emails.send({
    inboxId: inbound.inboxId,
    threadId: inbound.threadId,
    inReplyTo: inbound.messageId,
    toEmails: [inbound.replyTo ?? inbound.fromAddress],
    subject,
    body: replyHtml,
  })
}
import os

from bavimail import Bavimail

client = Bavimail(api_key=os.environ["BAVIMAIL_API_KEY"])


def reply_to_inbound(email_id: str, reply_html: str):
    inbound = client.inbound_emails.get(email_id)
    subject = inbound.subject if inbound.subject.lower().startswith("re:") else f"Re: {inbound.subject}"

    return client.emails.send(
        inbox_id=inbound.inbox_id,
        to_emails=[inbound.reply_to or inbound.from_address],
        subject=subject,
        body=reply_html,
        thread_id=inbound.thread_id,
        in_reply_to=inbound.message_id,
    )

An agent on the hosted MCP server (https://mcp.bavimail.com/) does the same with bavimail_get_inbound_email and bavimail_send_outbound_email, passing thread_id and in_reply_to.

Why do replies still break out of the thread?

  • The reply went out without In-Reply-To: common when an agent calls a plain send endpoint. Check the sent message's raw headers.
  • References was rebuilt from scratch: a chain with only the parent's Message-ID threads in most clients, but drops the history some clients use for long conversations. Carry the whole chain forward, or the first id and the most recent ones.
  • The Message-ID lost its angle brackets: [email protected] is not the same header value as <[email protected]>.
  • The reply came from a different address: the thread may hold in the recipient's client, but the next reply goes to the wrong inbox.
  • The subject changed: keep it, with Re:.

For the inbound side (webhooks, payloads, signature checks), see the inbound email webhook tutorial and inbound docs.

← All posts