Skip to main content

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

1

Create Agent

Define agent with model, instructions, and tools
2

Create Thread

Create conversation session (reuse for ongoing conversations)
3

Send Messages

Add user messages to the thread
4

Run Agent

Execute agent to process messages
5

Monitor Status

Poll run status until completion
6

Get Response

Retrieve agent’s messages from thread

Run Status Values

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)
Reuse existing thread when:
  • Continuing an ongoing conversation
  • Maintaining conversation context
  • Building on previous interactions

Thread Lifecycle

Threads persist until explicitly deleted:
Storage considerations:
  • 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

Delete threads and agents when no longer needed:
Always check run status and implement retry logic:
Start with short intervals, increase for longer operations:
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