Skip to content

[FEATURE] Add reset() method to Agent class for clearing all agent state between runs #329

@austinmw

Description

@austinmw

Problem Statement

Currently, there is no way to reset an Agent's complete state (conversation history, tool execution history, metrics, traces, and any accumulated internal state) without creating an entirely new Agent instance. This makes it inefficient to reuse agents for multiple independent tasks, particularly in evaluation/benchmarking scenarios where thousands of queries need to be tested across multiple agent configurations.

The Agent class maintains state in messages, event_loop_metrics, tool execution history, trace spans, and potentially other internal attributes that accumulate over time. Users must recreate the entire agent to start fresh, which is wasteful of resources and adds unnecessary overhead.

Proposed Solution

Add a reset() method to the Agent class that clears ALL agent state while preserving configuration:

class Agent:
    def reset(self):
        """Reset all agent state to fresh initialization while preserving configuration."""
        self.messages = []
        self.event_loop_metrics = EventLoopMetrics()
        self.trace_span = None
        # Reset tool handler state
        # Reset any tool execution history
        # Reset conversation manager state
        # Clear any cached results or internal buffers
        # Essentially return agent to just-initialized state

Use Case

Evaluating multiple agents across thousands of test queries for benchmarking:

# Current approach
agent_configs = [config1, config2, config3]
test_queries = ["query1", "query2", ...]

for config in agent_configs:
    for query in test_queries:
        agent = Agent(**config)  # Recreating agent every time!
        result = agent(query)
        # Overhead of reinitializing model connections, tools, etc.
# Desired approach - efficient
agents = [Agent(**config) for config in agent_configs]  # Create once
test_queries = ["query1", "query2", ...]  # Thousands of queries

def run_evaluation(agents, test_queries):
    results = []
    for agent in agents:
        for query in test_queries:
            agent.reset()  # Simply reset all state
            result = agent(query)
            results.append((agent, query, result))
    return results

# Clean, reusable agents
evaluation_results = run_evaluation(agents, test_queries)

Alternatives Solutions

  1. Agent Factory Pattern - Create a factory that manages agent creation, but this still has the overhead of creating new instances and reinitializing connections
  2. Manual Reset - Users can manually do agent.messages = [], but this doesn't reset metrics, tool state, traces, or other internal state and requires deep knowledge of internals
  3. Context Manager - Could use a context manager pattern that creates/destroys agents, but this doesn't fit well with the agent's design and still has initialization overhead
  4. Deep Copy - Could deep copy a "template" agent, but this is memory intensive and may not properly reset all state

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-contextSession or context relatedarea-devxDeveloper experience improvementsenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions