Threads, Runs, and Messages
Foundry Agent Service uses persistent threads, runs, and messages to manage conversation states and agent execution. Understanding these components is essential for building effective agents.Core Components
Agent
A configurable orchestration component that:- Uses AI models with instructions and tools
- Processes messages in threads
- Maintains conversation context
- Enforces safety and governance controls
Thread
A conversation session between an agent and a user:- Stores messages (up to 100,000 per thread)
- Automatically handles context truncation
- Persists until explicitly deleted
- Maintains conversation history
Message
Individual communication within a thread:- Created by agents or users
- Can include text, images, and files
- Stored in ordered list format
- Supports attachments
Run
An invocation of an agent on a thread:- Processes all messages in the thread
- May append new messages (agent responses)
- Calls models and tools as needed
- Tracks execution status
Agent Workflow
Run Status Values
| Status | Description |
|---|---|
queued | Run is waiting to be processed |
in_progress | Agent is actively processing |
requires_action | Agent needs function call results |
completed | Run finished successfully |
failed | Run encountered an error |
cancelled | Run was cancelled by user |
expired | Run exceeded time limits (10 min) |
Code Examples
Basic Agent Execution
Using create_and_poll
Thread Management
When to Create New Threads
Create a new thread when:- Starting a fresh topic or conversation
- User explicitly wants to “start over”
- Different users (each user should have their own thread)
- Thread becomes too large (impacts performance)
- Continuing an ongoing conversation
- Maintaining conversation context
- Building on previous interactions
Thread Lifecycle
Threads persist until explicitly deleted:- Threads with many messages consume storage
- Plan retention strategy based on:
- Storage costs
- Compliance requirements
- Business needs
Thread Limits
- Maximum 100,000 messages per thread
- Automatic context truncation when needed
- Performance may degrade with thousands of messages
- Consider creating new threads for long conversations
Best Practices
Clean Up Resources
Clean Up Resources
Delete threads and agents when no longer needed:
Handle Errors Gracefully
Handle Errors Gracefully
Always check run status and implement retry logic:
Use Appropriate Polling Intervals
Use Appropriate Polling Intervals
Start with short intervals, increase for longer operations:
Limit Message Size
Limit Message Size
Keep conversations concise for optimal performance:
- Avoid extremely long messages
- Summarize when threads get large
- Create new threads for new topics
- Monitor thread message count
Next Steps
Agent Overview
Learn about Foundry Agent Service
Environment Setup
Deploy agent infrastructure
Agent Tools
Extend agent capabilities
Quickstart
Create your first agent