microsoft / microsoft/agent-framework
.NET Workflows - Declarative context/thread reset support for iterative agent invocations
- Dominant language
- Python
- Stars
- 13.6k
- Forks
- 2.3k
- Avg merge
- 2d 45m
- Merged PRs (30d)
- 358
Description
## Description
### Problem
In declarative workflows, when using multiple agents and iterating over a list of inputs (e.g., creating summaries for each item), there's currently no way to reset the agent's context/thread state between iterations.
**Use Case Example:**
```yaml
actions:
- kind: Foreach
items: =workflow.input.documents
actions:
- kind: InvokeAzureAgent
agent: summarizer
input:
messages:
- =loop.item.content
output:
path: turn.summaries
append: true
```
In this scenario, when the summarizer agent processes each document, the conversation history from previous iterations accumulates. This causes:
- Context pollution between unrelated documents
- Increased token usage as history grows
- Potential confusion for the agent model
### Expected Behavior
The ability to reset an agent's thread/context state within declarative workflows:
1. **Per-invocation option** - A `resetThread` flag on `InvokeAzureAgent`:
```yaml
- kind: InvokeAzureAgent
agent: summarizer
resetThread: true
```
2. **Explicit action** - A new `ResetAgentContext` action kind:
```yaml
- kind: ResetAgentContext
agent: summarizer
```
3. **Via BaseAgent** - Expose thread reset methods that declarative workflows can invoke (similar to how `MagenticAgentExecutor` handles `MagenticResetSignal`)
### Alternatives Considered
- Creating new agent instances per iteration (inefficient)
- Manual thread management in code (not available in declarative/YAML workflows)
### Related Code
The framework already has reset patterns:
- `MagenticAgentExecutor.handle_magentic_reset()` → `self._agent_thread = self._agent.get_new_thread()`
- `AgentExecutor.reset()` clears internal cache
- `BaseGroupChatOrchestrator._clear_conversation()`
- `AgentEntity.reset()` in durable task entities
## Code Sample
```yaml
# Option 1: Per-invocation reset
- kind: Foreach
items: =workflow.input.documents
actions:
- kind: InvokeAzureAgent
agent: summarizer
resetThread: true
input:
messages:
- =loop.item.content
# Option 2: Explicit reset action
- kind: Foreach
items: =workflow.input.documents
actions:
- kind: ResetAgentContext
agent: summarizer
- kind: InvokeAzureAgent
agent: summarizer
input:
messages:
- =loop.item.content
```
## Language/SDK
Python
Contributor guide
Research direction
Start by tracing declarative workflow handling for InvokeAzureAgent, then compare the reset patterns in MagenticAgentExecutor.handle_magentic_reset(), AgentExecutor.reset(), BaseGroupChatOrchestrator._clear_conversation(), and AgentEntity.reset(). Done means declarative workflows can reset agent thread or context between iterations using a clearly defined mechanism, with the documented examples behaving as expected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- ai-infra-agents
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100