Files
agent-framework/dotnet/samples/02-agents/Harness/Harness_Step02_Research_WithBackgroundAgents
96d242fa7f .NET: Remove required token params from HarnessAgent, make compaction opt-in (#6409)
* Move token params from HarnessAgent constructor to options

Remove the required maxContextWindowTokens and maxOutputTokens
constructor parameters from HarnessAgent and AsHarnessAgent, replacing
them with optional MaxContextWindowTokens and MaxOutputTokens properties
on HarnessAgentOptions.

When both values are provided, compaction is enabled as before (in-loop
CompactionProvider and chat reducer on the default InMemoryChatHistory
Provider). When either is null, compaction is disabled entirely, making
it opt-in.

New constructor: HarnessAgent(IChatClient, HarnessAgentOptions?,
ILoggerFactory?, IServiceProvider?)

Closes #6333

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Improving comments.

* feat: Add custom CompactionStrategy and DisableCompaction to HarnessAgentOptions

Allow users to provide their own CompactionStrategy via options, with
a clear priority system:
1. DisableCompaction=true: no compaction regardless of other settings
2. Custom CompactionStrategy provided: use it (token params ignored)
3. Both MaxContextWindowTokens and MaxOutputTokens set: default strategy
4. Otherwise: no compaction

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: Address PR review comments on compaction opt-in

- Update chatClient param XML doc to reflect compaction is opt-in
- Strengthen compaction tests to assert ChatReducer is null/not-null
  rather than just asserting construction succeeds

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-09 13:06:00 +00:00
..

Harness Step 02 — BackgroundAgents (Stock Price Research)

This sample demonstrates how to use the BackgroundAgentsProvider to delegate work from a parent agent to background agents. Both agents use HarnessAgent for pre-configured function invocation, per-service-call persistence, and context-window compaction.

What It Does

A parent agent receives a list of stock tickers and uses a web-search background agent to find the closing price for each ticker on December 31, 2025. The background tasks run concurrently, and results are presented in a summary table.

Architecture

┌────────────────────────────────────────┐
│     StockPriceResearcher               │
│         (Parent Agent)                 │
│                                        │
│  BackgroundAgentsProvider              │
│    ├─ BackgroundAgents_StartTask       │
│    ├─ BackgroundAgents_WaitFor...      │
│    ├─ BackgroundAgents_GetTaskResults  │
│    └─ ...                              │
└────────────┬───────────────────────────┘
             │  delegates to
             ▼
┌─────────────────────────────────┐
│       WebSearchAgent            │
│        (Sub-Agent)              │
│                                 │
│  Tools:                         │
│    └─ web_search (Foundry)      │
└─────────────────────────────────┘

Prerequisites

  • An Azure AI Foundry endpoint with an OpenAI model deployment
  • Set the following environment variables:
    • AZURE_FOUNDRY_OPENAI_ENDPOINT — Your Foundry OpenAI endpoint URL
    • AZURE_AI_MODEL_DEPLOYMENT_NAME — Model deployment name (defaults to gpt-5.4)

Running the Sample

cd dotnet/samples/02-agents/Harness/Harness_Step02_Research_WithBackgroundAgents
dotnet run

When prompted, enter a list of stock tickers such as:

BAC, MSFT, BA

The parent agent will delegate each ticker lookup to the web search background agent concurrently and present the results in a table.