How do I manage threaded conversations?
Maintain context across multi-turn email conversations with your agent.
Threads are how AgentMail organizes conversations. Every time your agent sends a new email, a thread is created. Replies are automatically grouped into the same thread, giving your agent full conversation context.
How threads work
- Your agent sends an email, and a new thread is created automatically
- The recipient replies, and the reply is added to the same thread
- Your agent replies back, and it is added to the same thread
- The full conversation history stays organized in one place
You never need to create threads manually. AgentMail handles threading automatically using standard email headers (Message-ID, In-Reply-To, References).
Listing threads
Per inbox
Across your entire organization
This org-wide query is useful for building supervisor agents that monitor conversations across a fleet of other agents, analytics dashboards, or routing systems that escalate conversations between agents.
Getting a full thread
Retrieve a thread by its ID to read the messages in the conversation. Get Thread can return a long thread in pages, so do not assume one response holds every message:
- The first page holds the newest messages. Within each page, messages are ordered oldest to newest.
- When
next_page_tokenis present, pass it aspage_tokento fetch the next, older page. It is absent once you have the whole thread. - Pass
limit(up to 100) to set the page size.
Replying in a thread
To continue a conversation, reply to the most recent message in the thread:
Always provide both text and html when sending replies. This ensures readability across all email clients and improves deliverability.
Handling quoted text in replies
When people reply to emails, their email client often includes the entire previous conversation as quoted text. AgentMail provides extracted_text and extracted_html fields on received messages, which contain only the new reply content without the quoted history.
This prevents your agent from re-processing the entire conversation history on every reply.
Using labels to track conversation state
Combine threads with labels to manage your agent’s workflow. AgentMail adds an unread label to every message it receives, and a thread carries the labels of its messages, so listing threads with labels=["unread"] finds conversations with mail your agent has not handled yet. Labels such as replied, escalated, or resolved are your own: AgentMail does not apply them, so your agent adds them as it works.
After replying, remove unread from the messages you handled so the thread drops out of the list. batch_update changes labels on up to 50 messages in one request. The Idempotency-Key on the reply means that an identical retry within 24 hours returns the original message_id and thread_id instead of sending a second email.
Idempotency keys may only contain A-Z a-z 0-9 - . _ ~ and be up to 256 characters. Message IDs contain characters such as <, @, and >, so hash the ID instead of using it directly. Include the inbox in the hash: keys are shared across your organization, and an email sent to two of your inboxes arrives in both with the same message ID.
A key only replays an identical request. If your agent writes different reply text on a retry, AgentMail returns 409 Conflict instead of sending, so treat a 409 as a sign that a reply may already exist rather than retrying with a new key.
Tips
- Use threads to maintain context in multi-turn conversations, so your agent can reference what was said earlier
- Query org-wide threads with
client.threads.list()to build dashboards or route conversations between agents - Filter on the
unreadlabel AgentMail applies to received mail, and add your own labels such asreplied,escalated, andresolvedto track conversation state - Follow
next_page_tokenwhen you need a thread’s full history, since Get Thread can return long threads in pages - Always reply to the latest message in a thread to keep email headers correct
