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

Open Beginner friendly
#9,587 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@HanYuanLu is already working on this.

Since Nov 20, 2026.

  • #9588 by @HanYuanLu — open
  • #9650 by @Mansi2221 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

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

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.

Dominant language
Python
Stars
61.2k
Forks
9.2k
Avg merge
1d 12h
Merged PRs (30d)
228

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from crewAIInc/crewAI

All issues in crewAIInc/crewAI

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.