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

Đang mở Phù hợp với người mới
#9,587 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@HanYuanLu đang làm issue này rồi.

Từ ngày 20/11/2026.

  • #9588 của @HanYuanLu — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu bằng cách tìm Agent.copy() và logic sao chép LLM được mô tả trong issue, sau đó đọc các bài kiểm thử hiện có về sao chép agent. Thêm một bài kiểm thử hồi quy sao chép một agent, thay đổi additional_params và _token_usage của bản sao, rồi kiểm tra rằng các giá trị ban đầu không đổi trong khi HTTP client vẫn được dùng chung. Hoàn thành khi bài kiểm thử hồi quy chạy thành công.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
60.9k
Fork
9.2k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
226

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của crewAIInc/crewAI

Tất cả issue của crewAIInc/crewAI

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.