[BUG] Agent.copy() shares the LLM's additional_params and token usage
Maintainers usually reply within 1 day
@HanYuanLu is already working on this.
Since Nov 20, 2026.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start by locating Agent.copy() and the LLM copy logic described in the issue, then read the existing agent copy tests. Add a regression test that copies an agent, mutates the copy's additional_params and _token_usage, and checks that the original values remain unchanged while the HTTP client stays shared. Done when the regression test passes.
Written by the indexing model from the issue text.
Description
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
- Build an Agent with an LLM instance.
- Set llm.additional_params["bucket"] to an empty list.
- Call agent.copy().
- Append 1 to the copy's additional_params["bucket"] and add 5 to the copy's _token_usage["total_tokens"].
- 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.
- Dominant language
- Python
- Stars
- 61.2k
- Forks
- 9.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 228
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from crewAIInc/crewAI
-
[BUG] crewai tool create/install/login exit 0 when they failPossibly taken @Kiko0301 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
[BUG] BraveSearchTool ignores the documented api_key argumentPossibly taken @Kiko0301 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
APIKeyAuth cookie mode overwrites existing Cookie headersPossibly taken @Kiko0301 claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
crewAIInc/crewAI#9706 · 1 comment ·
Maintainers usually reply within 1 day
-
[FEATURE] Document a single-agent workflow with skillsPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
All issues in crewAIInc/crewAI
Similar issues
-
MarkdownDocSerializer.post_process escapes '\<' to '\<', rendering literal '<' in CommonMarkOpen
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
docling-project/docling-core#1067 ·
Maintainers usually reply within 3 days
-
correction metadata
Difficulty 1/5 Under an hour Newbie friendliness 85/100
acl-org/acl-anthology#11101 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
vllm-project/compressed-tensors#1033 ·
Maintainers usually reply within 3 days
-
chore kaizen ui-rewrite
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
IBM/mcp-context-forge#7681 ·
Maintainers usually reply within 2 days
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
awesomedata/apd-core#1216 ·
Maintainers usually reply within 4 days