crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] Independent Flows share nested initial_state and charge another department’s purchases

Open master1018 opened this issue 3 weeks ago • 2 comments

Description

An expense recorded by one Flow appears in a separately created Flow before that second Flow runs. I was creating two small purchasing agents from the same initial budget settings, one for Marketing and one for Engineering. Marketing spends 40 USD; Engineering should start with its own 100 USD budget and spend 10 USD. Instead, Engineering inherits Marketing’s expense and reports 50 USD remaining.

Steps to Reproduce

  1. Run the following code:

import os
from crewai import Agent, LLM
from crewai.flow import Flow, start
from crewai.tools import tool

llm = LLM(model="openai/deepseek-flash", custom_openai=True,
          api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com/v1",
          extra_body={"thinking": {"type": "disabled"}})

class DepartmentOrder(Flow):
    @start()
    def purchase(self):
        @tool
        def buy_notebooks(quantity: int) -> str:
            """Buy notebooks at 5 USD each and return this department's remaining budget.

            Args:
                quantity: Number of notebooks to buy.
            """
            self.state["wallet"]["expenses"].append(quantity * 5)
            return f"Remaining budget: {100 - sum(self.state['wallet']['expenses'])} USD"

        buyer = Agent(role="Office buyer", goal="Buy requested notebooks and report the budget",
                      backstory="Each department starts with its own 100 USD budget.",
                      llm=llm, tools=[buy_notebooks], verbose=True)
        return buyer.kickoff(self.state["request"]).raw

settings = {"wallet": {"expenses": []}}
marketing = DepartmentOrder(initial_state=settings)
engineering = DepartmentOrder(initial_state=settings)
print(marketing.kickoff(inputs={"request": "Buy 8 notebooks and tell me my remaining budget."}))
print(engineering.kickoff(inputs={"request": "Buy 2 notebooks and tell me my remaining budget."}))
print(engineering.state["wallet"])
assert engineering.state["wallet"]["expenses"] == [10], "Engineering inherited Marketing's 40 USD expense"

Expected behavior

Each Flow should own its working state. Engineering should have expenses 10 and 90 USD remaining. Running Marketing should leave both the initial settings and Engineering’s not-yet-used expenses unchanged.

Screenshots/Code snippets

None

Operating System

Ubuntu 20.04

Python Version

3.11

crewAI Version

1.15.39

crewAI Tools Version

1.15.39

Virtual Environment

Conda

Evidence


[Finalize] todos_count=0, todos_with_results=0
╭─────────────────────────── ✅ Agent Final Answer ────────────────────────────╮
│                                                                              │
│  Agent: Office buyer                                                         │
│                                                                              │
│  Final Answer:                                                               │
│  Bought 2 notebooks (2 × $5 = $10). Your remaining budget is **$50 USD**.    │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯

╭─────────────────────────── ✅ LiteAgent Completed ───────────────────────────╮
│                                                                              │
│  LiteAgent Completed                                                         │
│  Role: Office buyer                                                          │
│  id: faab607a-70b8-4b9c-8d5a-262c54a3f390                                    │
│  role: Office buyer                                                          │
│  goal: Buy requested notebooks and report the budget                         │
│  backstory: Each department starts with its own 100 USD budget.              │
│  tools: buy_notebooks                                                        │
│  verbose: True                                                               │
│                                                                              │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯

╭────────────────────────── ✅ Flow Method Completed ──────────────────────────╮
│                                                                              │
│  Method: purchase                                                            │
│  Status: Completed                                                           │
│                                                                              │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯

╭───────────────────────────── ✅ Flow Completion ─────────────────────────────╮
│                                                                              │
│  Flow Execution Completed                                                    │
│  Name: DepartmentOrder                                                       │
│  ID: 6ad4407e-628f-41a7-9c00-7acf5459aa4a                                    │
│                                                                              │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯

Bought 2 notebooks (2 × $5 = $10). Your remaining budget is **$50 USD**.
{'expenses': [40, 10]}
Traceback (most recent call last):
  File "/tmp/unified-context-audit/crewai/investigation/initial-state-alias/main.py", line 34, in <module>
    assert engineering.state["wallet"]["expenses"] == [10], "Engineering inherited Marketing's 40 USD expense"
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Engineering inherited Marketing's 40 USD expense

Possible Solution

None

Additional context

None

master1018 avatar Nov 12 '26 02:11 master1018

You can keep the departments independent on released 1.15.39 by giving each constructor a fresh snapshot:

from copy import deepcopy
marketing = DepartmentOrder(initial_state=deepcopy(settings))
engineering = DepartmentOrder(initial_state=deepcopy(settings))

I ran the complete Flow → Agent.kickoff() → buy_notebooks → final-answer path. Shared settings and dict(settings) both leave the unstarted Engineering flow with [40] after Marketing; Engineering finishes at 50 USD. Fresh snapshots keep the original settings empty, Marketing at [40], Engineering at [10], and the final budgets at 60 / 90 USD. Each purchasing tool executes once.

Complete free native lab + source · 8.8KB ZIP · Recorded states and tool observations. The fixture uses a controlled BaseLLM on Python 3.12.3/Linux. The existing #9384/#9433 source proposals are credited in the README.

Built and run by Blucca, an AI-led studio using Codex. Does this constructor change give you 60/90 with your own DeepSeek run?

blucca avatar Nov 14 '26 22:11 blucca

Sounds #9498 have already fixed this bug, so I will close this issue.

master1018 avatar Nov 18 '26 06:11 master1018