-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Fix LLM callback isolation without serializing requests #4252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VedantMadane
wants to merge
3
commits into
crewAIInc:main
Choose a base branch
from
VedantMadane:fix/llm-callbacks-no-global-mutation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−49
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import threading | ||
| import time | ||
| from unittest.mock import MagicMock, patch | ||
| import pytest | ||
| from crewai.llm import LLM | ||
| from crewai.utilities.token_counter_callback import TokenCalcHandler | ||
| from crewai.agents.agent_builder.utilities.base_token_process import TokenProcess | ||
|
|
||
| def test_concurrent_llm_calls_isolation(): | ||
| """ | ||
| Test that concurrent LLM calls with different callbacks do not interfere with each other. | ||
| """ | ||
|
|
||
| # We patch globally so it applies to all threads | ||
| # We use crewai.llm.litellm to be safe | ||
| with patch("crewai.llm.litellm.completion") as mock_completion: | ||
|
|
||
| # Setup mock to return a valid response structure | ||
| def side_effect(*args, **kwargs): | ||
| messages = kwargs.get("messages", []) | ||
| content = messages[0]["content"] if messages else "" | ||
| thread_id = content.split("thread ")[-1] | ||
|
|
||
| mock_message = MagicMock() | ||
| mock_message.content = f"Response for thread {thread_id}" | ||
| mock_choice = MagicMock() | ||
| mock_choice.message = mock_message | ||
| mock_response = MagicMock() | ||
| mock_response.choices = [mock_choice] | ||
|
|
||
| # Use unique usage stats based on thread ID | ||
| tid_int = int(thread_id) | ||
|
|
||
| # Create a usage object with attributes (not a dict) | ||
| usage_obj = MagicMock() | ||
| usage_obj.prompt_tokens = 10 + tid_int | ||
| usage_obj.completion_tokens = 5 + tid_int | ||
| usage_obj.total_tokens = 15 + 2 * tid_int | ||
| # Mock prompt_tokens_details to be None or have cached_tokens=0 | ||
| usage_obj.prompt_tokens_details = None | ||
|
|
||
| mock_response.usage = usage_obj | ||
|
|
||
| # Simulate slight delay | ||
| time.sleep(0.1) | ||
| return mock_response | ||
|
|
||
| mock_completion.side_effect = side_effect | ||
|
|
||
| # Define the workload | ||
| def run_llm_request(thread_id, result_container): | ||
| token_process = TokenProcess() | ||
| handler = TokenCalcHandler(token_cost_process=token_process) | ||
|
|
||
| # Store handler so we can verify it later | ||
| result_container[thread_id] = { | ||
| "handler": handler, | ||
| "summary": None | ||
| } | ||
|
|
||
| llm = LLM(model="gpt-4o-mini", is_litellm=True) | ||
|
|
||
| llm.call( | ||
| messages=[{"role": "user", "content": f"Hello from thread {thread_id}"}], | ||
| callbacks=[handler] | ||
| ) | ||
|
|
||
| result_container[thread_id]["summary"] = token_process.get_summary() | ||
|
|
||
| results = {} | ||
| threads = [] | ||
|
|
||
| # Start threads | ||
| for i in [1, 2]: | ||
| t = threading.Thread(target=run_llm_request, args=(i, results)) | ||
| threads.append(t) | ||
| t.start() | ||
|
|
||
| for t in threads: | ||
| t.join() | ||
|
|
||
| # Verification | ||
| assert mock_completion.call_count == 2 | ||
|
|
||
| # Check each call arguments | ||
| for call_args in mock_completion.call_args_list: | ||
| kwargs = call_args.kwargs | ||
| messages = kwargs.get("messages", []) | ||
| content = messages[0]["content"] | ||
| thread_id = int(content.split("thread ")[-1]) | ||
|
|
||
| expected_handler = results[thread_id]["handler"] | ||
|
|
||
| # CRITICAL CHECK: Verify ONLY the expected handler was passed | ||
| assert "callbacks" in kwargs | ||
| callbacks = kwargs["callbacks"] | ||
| assert len(callbacks) == 1 | ||
| assert callbacks[0] == expected_handler, f"Callback mismatch for thread {thread_id}" | ||
|
|
||
| # Verify token usage isolation | ||
| summary1 = results[1]["summary"] | ||
| assert summary1.prompt_tokens == 11 | ||
| assert summary1.completion_tokens == 6 | ||
|
|
||
| summary2 = results[2]["summary"] | ||
| assert summary2.prompt_tokens == 12 | ||
| assert summary2.completion_tokens == 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.