Skip to content

Python: [Feature]: Declarative context/thread reset support for iterative agent invocations #3691

@droideronline

Description

@droideronline

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:

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:

    - kind: InvokeAzureAgent
      agent: summarizer
      resetThread: true
  2. Explicit action - A new ResetAgentContext action kind:

    - 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

# 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions