How do I use labels to track email state?
Labels are string-based tags you attach to messages, threads, and drafts. They are the primary way agents track state, classify emails, and filter conversations in AgentMail.
Labels AgentMail applies
Some labels are set by AgentMail rather than by your agent:
- System labels such as
received,sent,delivered,bounced,complained,delayed,rejected, andopenedtrack a message’s delivery lifecycle. You can filter on them, but you cannot add or remove them: a request that tries is rejected with a400. unreadis added to every message AgentMail receives. Unlike the system labels, you can remove it, which is how an agent marks a message as handled.- Classification labels
spam,unauthenticated, andblockedmark inbound mail that AgentMail or your lists flagged. Messages and threads with these labels, or with thetrashlabel, are left out of list results unless you pass the matchinginclude_spam,include_unauthenticated,include_blocked, orinclude_trashparameter and your API key has the matching read permission.
Beyond these, labels are yours to define. Labels are stored in lowercase, so Needs-Review and needs-review are the same label.
Adding labels when sending
You can attach labels directly when sending a message:
Updating labels on existing messages
Add or remove labels on messages that have already been sent or received. This is how agents change the state of a conversation as they process it:
To change labels on many messages at once, batch_update applies one label change to up to 50 messages in a single request. The update is atomic: either all of the messages it finds are updated or none are (IDs that do not exist, or that your key cannot see, are skipped). To relabel a whole thread, threads.update applies the change to every message in the thread; it rejects threads with 100 or more messages with a 422.
Filtering by label
List messages or threads that match specific labels. When you pass several labels, results must carry all of them. This is where labels become powerful for building agent workflows:
List calls return one page at a time. Pass next_page_token as page_token to get the next page.
Common label patterns for agents
Apart from unread, every label in this table is one your agent applies; AgentMail does not add them for you. You do not need labels for message direction: AgentMail already applies sent and received. Labels are free-form strings, so use whatever naming convention makes sense for your agent’s workflow.
Example: agent workflow with labels
Here is a complete pattern for an agent that processes inbound emails, classifies them, and tracks state. It starts from the unread label AgentMail applies, and the reply carries an Idempotency-Key so a retry within 24 hours cannot send a second email:
Best practices
- Be consistent: Pick a naming convention (e.g.,
kebab-case) and stick with it across all your agents - Use prefixes for grouping: Labels like
status-pending,priority-high,customer-acmeare easier to manage at scale - Keep it concise: A message with too many labels becomes hard to query. Aim for a meaningful, focused set
- Combine with threads: Filter threads by label to find conversations in a specific state, then get the thread details to process them
For more details, see the Labels core concept documentation.
