Skip to content

fix(agents): isolate LLM params and token usage on Agent.copy() - #9588

Open
HanYuanLu wants to merge 3 commits into
crewAIInc:mainfrom
HanYuanLu:fix/agent-copy-llm-state
Open

HanYuanLu wants to merge 3 commits into
crewAIInc:mainfrom
HanYuanLu:fix/agent-copy-llm-state

Conversation

@HanYuanLu

Copy link
Copy Markdown
Contributor

Related issue

Fixes #9587

Summary

Agent.copy() copied the LLM with copy.copy, so additional_params and
_token_usage stayed shared with the original agent. Deep-copy those two
dicts after the shallow copy, and leave the HTTP client in place.

Verification

  • Tests added or updated for the changed behavior
  • Relevant tests and quality checks pass locally

uv run pytest lib/crewai/tests/agents/test_agent.py::test_agent_copy_isolates_llm_mutable_state -q --allowed-hosts=127.0.0.1

Additional context

Local mypy on Windows reports pre-existing errors in unrelated files
(fcntl, pwd, python_calamine). Those names exist on the Ubuntu CI runner.

@strix-security

strix-security Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Strix Security Review

Warning

This pull request has 37 commits after the last Strix review (b1e50bc). Strix has not reviewed these changes.
Automatic review on push is off for this repository. To review the latest changes, tag @strix-security in a comment, or turn on re-review on push.

✅ No open security findings.

PR summary: PR #9588 makes `Agent.copy()` isolate LLM state: after the shallow copy of the LLM it deep-copies `additional_params` and `_token_usage` so a copied agent no longer shares or mutates the original's provider parameters and token totals, while deliberately keeping the HTTP client shared. A regression test asserts the two dicts are independent after copying.

Review summary

The change is a seven-line state-isolation fix inside BaseAgent.copy(): after the LLM is shallow-copied, its additional_params and _token_usage dicts are now deep-copied so a copied agent cannot mutate the original's request parameters or token totals. Tracing the change showed it only narrows aliasing on an object the copy already shared — it adds no new input, no new sink and no new call path, and the dicts it duplicates are exactly the two the library mutates at runtime. Two suspected issues were raised and both were dismissed: the LLM attributes that remain shared (client_params, default_headers, api_key, base_url, the HTTP client) are populated only from the developer's own configuration and are never written in place by any in-repo path, so no attacker-controlled input reaches them; and the new deepcopy can only raise for a non-copyable object the developer themselves placed in additional_params, which fails that same caller's own run without crossing a user, tenant or service boundary. Nothing in the test-only change introduces a production pattern.

Ruled out (2)
  • Does Agent.copy() leave security-relevant LLM state shared, letting a copied agent's use affect another agent or redirect credentials?
    Every attribute still aliased (client_params, default_headers, api_key, base_url, the HTTP client) is populated only from developer-supplied configuration, is never mutated in place anywhere in the library, and scalar rebinding on a pydantic copy does not touch the original — so no attacker-control…
  • Can the new deepcopy calls in Agent.copy() be exploited to crash another user's run or execute attacker-chosen __deepcopy__ code?
    additional_params and _token_usage can only be filled from typed config, JSON/YAML (primitives only) or integer usage counters, so deepcopy can only fail on a non-copyable object the configuring developer themselves supplied, harming that same caller's own process.

Review 1 on this PR · b1e50bc · requested Nov 20 03:11 UTC when the PR was opened


Reviewed by Strix
Re-run review · Configure security review settings · Try a deeper review with @strix-security /deep

# copied agent actually mutates: provider params and token totals.
if getattr(existing_llm, "additional_params", None) is not None:
existing_llm.additional_params = deepcopy(existing_llm.additional_params)
if getattr(existing_llm, "_token_usage", None) is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The copied _token_usage keeps the original's counts. Resetting it to zero in base_agent.py:739 would keep kickoff_for_each usage totals right when the source LLM was already used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 33a489c. A copied agent's _token_usage is now a fresh zeroed dict, so kickoff_for_each no longer adds the source LLM's existing counts once per input. The test sets the original total to 10 and checks the copy starts at 0.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-pushed bda2b9e with no change to the fix. It only merges main so the pip-audit check is green.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants