For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
contact@agentmail.ccDiscord
DocumentationAPI ReferenceKnowledge BaseChangelog
DocumentationAPI ReferenceKnowledge BaseChangelog
  • Get Started
    • Welcome
    • Introduction
    • Quickstart
  • Core Concepts
    • Inboxes
    • Messages
    • Threads
    • Drafts
    • Labels
    • Lists
    • Attachments
    • Pods
    • Permissions
  • Integrations
    • Agent Onboarding
    • Skills
    • MCP
    • CLI
    • Google ADK
    • OpenClaw
    • Replit
    • x402
    • MPP
    • LiveKit
    • Sim.ai
  • Guides
    • Sending & Receiving Email
    • IMAP & SMTP
    • Multi-Tenancy
  • Webhooks
    • Overview
    • Events
    • Setup Guide
    • Verifying Webhooks
  • WebSockets
    • Overview
    • Quickstart
  • Best Practices
    • Email Deliverability
    • Idempotency
  • Examples
    • Github Repo Agent
    • Auto Reply Agent
    • Smart Labeling Agent
    • Sales Agent (WebSocket)
    • Live AgentMail Examples
  • Resources
    • FAQ
    • Talon Reply Extraction
    • Community
    • Support
LogoLogo
contact@agentmail.ccDiscord
On this page
  • What is a Thread?
  • Querying Threads
  • Listing Threads per Inbox
  • Listing Threads Across an Organization
  • Searching Threads
  • Getting a Single Thread
  • Copy for Cursor / Claude
Core Concepts

Threads

Organizing conversations across your Inboxes.
Was this page helpful?
Edit this page
Previous

Drafts

Preparing and scheduling Messages for your agents.
Next
Built with

What is a Thread?

A Thread is an API resource that represents a single conversation. It acts as a container, grouping a series of related Messages together in chronological order, just like a conversation thread in a traditional email client.

Threads are created automatically. When your agent sends a Message that isn’t a reply to a previous one, a new Thread is initiated. Any subsequent replies are automatically added to this same Thread, allowing your agent to easily maintain the context of a conversation over time.

Querying Threads

While Threads are created implicitly, you can retrieve them in two powerful ways: scoped to a single Inbox or across your entire Organization.

Listing Threads per Inbox

This is the standard way to retrieve all the conversations associated with a single agent or Inbox.

1# You'll need an inbox ID to list threads from.
2inbox_id = "inbound-agent@agentmail.to"
3
4# This loads all threads within the specified Inbox
5
6inbox_threads = client.inboxes.threads.list(inbox_id=inbox_id)

Listing Threads Across an Organization

This is one of AgentMail’s most powerful features. By omitting the inbox_id, you can retrieve a list of Threads from every Inbox in your Organization. This org-wide querying capability is essential for building:

  • Supervisor Agents: An agent that monitors conversations from a fleet of other agents.
  • Analytics Dashboards: Building something where you need visibility across all inboxes across the organization
  • Advanced Workflows: Systems that can route or escalate conversations between different agents with different permissions.
1# By not providing an inbox_id, we get all threads in the organization
2all_threads = client.threads.list()
3
4print(f"Found {all_threads.count} threads across the entire organization.")

Searching Threads

Full-text search finds Threads by keyword across the sender, recipients, subject, and message body, ranked by relevance. Like listing, it works per-Inbox or across the whole Organization — omit the inbox_id to search every Inbox. Spam, trash, blocked, and unauthenticated threads are excluded, and limit cannot exceed 100.

1# search every inbox in the organization
2results = client.threads.search(q="invoice overdue")
3
4for thread in results.threads:
5 print(thread.thread_id, thread.subject)
6 # highlights shows which fields matched, with matched terms wrapped in **
7 if thread.highlights:
8 print(thread.highlights)
9
10# or scope the search to a single inbox
11inbox_results = client.inboxes.threads.search(
12 inbox_id="inbound-agent@agentmail.to",
13 q="invoice overdue",
14)

Need an exact-field filter instead of relevance ranking? threads.list accepts senders, recipients, and subject substring filters and keeps the usual newest-first ordering.

Getting a Single Thread

You can also retrieve a single Thread by its ID. This will return the Thread object, which typically contains a list of all its associated Messages and their ID’s. A common workflow is listing the messages in a thread and calling the messages.reply method on the last one.

1thread_id = "thread_456def"
2
3# This loads a single thread and its messages
4
5thread = client.threads.get(
6thread_id="thread_id"
7)
8
9print(f"Retrieved thread {thread.thread_id} with {len(thread.messages)} messages.")

Copy for Cursor / Claude

Copy one of the blocks below into Cursor or Claude for complete Threads API knowledge in one shot.

1"""
2AgentMail Threads — copy into Cursor/Claude.
3
4Setup: pip install agentmail python-dotenv. Set AGENTMAIL_API_KEY in .env.
5
6API reference:
7- inboxes.threads.list(inbox_id, limit?, page_token?, labels?, before?, after?, ascending?, senders?, recipients?, subject?)
8- inboxes.threads.search(inbox_id, q, limit?, page_token?, before?, after?) — full-text, relevance-ranked
9- inboxes.threads.get(inbox_id, thread_id)
10- inboxes.threads.get_attachment(inbox_id, thread_id, attachment_id)
11- inboxes.threads.delete(inbox_id, thread_id)
12- threads.list(limit?, page_token?, labels?, senders?, recipients?, subject?) — org-wide, all inboxes
13- threads.search(q, limit?, page_token?, before?, after?) — org-wide full-text
14- threads.get(thread_id) — org-wide
15- threads.get_attachment(thread_id, attachment_id)
16
17Errors: SDK raises on 4xx/5xx. Rate limit: 429 with Retry-After.
18"""
19import os
20from dotenv import load_dotenv
21from agentmail import AgentMail
22
23load_dotenv()
24client = AgentMail(api_key=os.getenv("AGENTMAIL_API_KEY"))
25
26inbox_id = "agent@agentmail.to"
27
28# Per-inbox threads
29inbox_threads = client.inboxes.threads.list(inbox_id=inbox_id, limit=20)
30if inbox_threads.threads:
31 thread = client.inboxes.threads.get(inbox_id, inbox_threads.threads[0].thread_id)
32
33# Org-wide threads
34all_threads = client.threads.list(limit=50)
35if all_threads.threads:
36 single = client.threads.get(all_threads.threads[0].thread_id)
37 print(f"Thread has {len(single.messages)} messages")