Skip to content

Memory leak: Agent references not released between turns #2217

@ruskaruma

Description

@ruskaruma

Describe the bug

Agent objects accumulate in memory during long-running workflows because RunItem objects hold strong references that are never released.

Debug information

  • Agents SDK version: v0.6.4
  • Python version: Python 3.12

Repro steps

Run an agent multiple times in a loop. Agent objects accumulate in memory instead of being garbage collected.

In src/agents/run.py (line 774), lists are cleared without releasing agent references:

turn_result.pre_step_items.clear()
turn_result.new_step_items.clear()

The SDK has a release_agent() method for this, but it's never called.

Expected behavior

Memory should stay constant across multiple runs, not grow indefinitely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-more-infoWaiting for a reply/more info from the authorquestionQuestion about using the SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions