[BUG] Agent.copy() shares the LLM's additional_params and token usage

オープン 初心者向け
#9,587 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@HanYuanLu がすでに取り組んでいます。

2026年11月20日 から。

  • #9588 @HanYuanLu による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

まず Agent.copy() と issue に記載された LLM のコピー処理を特定し、既存の agent コピーのテストを読んでください。agent をコピーし、コピー側の additional_params と _token_usage を変更しても元の値が変わらず、HTTP クライアントが共有されたままであることを確認する回帰テストを追加してください。回帰テストが通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

bug
Description

Agent.copy() documents itself as a deep copy, but it copies the LLM with copy.copy. The two agents get different LLM objects that still share additional_params and _token_usage. Mutating either dict on the copy changes the original agent.

Steps to Reproduce
  1. Build an Agent with an LLM instance.
  2. Set llm.additional_params["bucket"] to an empty list.
  3. Call agent.copy().
  4. Append 1 to the copy's additional_params["bucket"] and add 5 to the copy's _token_usage["total_tokens"].
  5. Read those two values on the original agent.
Expected behavior

The original agent's bucket stays [] and its total_tokens stays 0. The copy has its own additional_params and _token_usage dicts.

Screenshots/Code snippets

from crewai import Agent, LLM

llm = LLM(model="gpt-4o-mini")
llm.additional_params["bucket"] = []
original = Agent(
role="test role",
goal="test goal",
backstory="test backstory",
llm=llm,
)
copied = original.copy()

copied.llm.additional_params["bucket"].append(1)
copied.llm._token_usage["total_tokens"] += 5

assert copied.llm is not original.llm
assert original.llm.additional_params["bucket"] == []
assert original.llm._token_usage["total_tokens"] == 0

Operating System

Windows 11

Python Version

3.12

crewAI Version

1.15.44

crewAI Tools Version

N/A

Virtual Environment

Venv

Evidence

On current main the script fails after printing:

llm identical False
params identical True
usage identical True
original bucket [1]
original tokens 5

copy.copy creates a new LLM object but does not copy its mutable dicts. additional_params and _token_usage stay shared with the original. The HTTP client is intentionally left shared, because it holds a connection pool and locks that deepcopy cannot clone after the first request.

Possible Solution

After the shallow copy, deepcopy additional_params and _token_usage onto the copied LLM. Leave the client in place.

Additional context

A regression test is test_agent_copy_isolates_llm_mutable_state.

主要言語
Python
スター
60.9k
フォーク
9.2k
平均マージ
1日 9時間
マージ済み PR(30日)
226

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

crewAIInc/crewAI のほかの issue

crewAIInc/crewAI の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。