How do I use labels to track email state?
Use labels to manage agent workflow state on emails and threads.
Labels are string-based tags you attach to messages and threads. They are the primary way agents track state, classify emails, and filter conversations in AgentMail.
Adding labels when sending
You can attach labels directly when sending a message:
Python
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:
Python
Filtering by label
List messages or threads that match specific labels. This is where labels become powerful for building agent workflows:
Python
Common label patterns for agents
Labels are free-form strings. 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:
Python
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,campaign-q4are 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.
