litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

I am using crew ai for the first time, i followed the quickstart doc, im using open ai api and gpt 4o mini model. I followed all the steps still got the same error as everyone getting with using other llm models.

Running the Crew

warning: VIRTUAL_ENV=C:\Users\manas\Workspce\AiAgents\CrewAi_Course\my_env does not match the project environment path .venv and will be ignored

Agent: AI LLMs Senior Data Researcher

Task: Conduct a thorough research about AI LLMs Make sure you find any interesting and relevant information given the current year is 2024.

LiteLLM.Info: If you need to debug this error, use `litellm.set_verbose=True’.

ERROR:root:LiteLLM call failed: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

Agent: AI LLMs Senior Data Researcher

Task: Conduct a thorough research about AI LLMs Make sure you find any interesting and relevant information given the current year is 2024.

LiteLLM.Info: If you need to debug this error, use `litellm.set_verbose=True’.

ERROR:root:LiteLLM call failed: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

Agent: AI LLMs Senior Data Researcher

Task: Conduct a thorough research about AI LLMs Make sure you find any interesting and relevant information given the current year is 2024.

LiteLLM.Info: If you need to debug this error, use `litellm.set_verbose=True’.

ERROR:root:LiteLLM call failed: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
Traceback (most recent call last):
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 639, in completion
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 542, in completion
openai_client: OpenAI = self._get_openai_client( # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 317, in _get_openai_client
_new_client = OpenAI(
^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\openai_client.py”, line 110, in init
raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\main.py”, line 1608, in completion
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\main.py”, line 1581, in completion
response = openai_chat_completions.completion(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 649, in completion
raise OpenAIError(
litellm.llms.openai.common_utils.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agent.py”, line 345, in execute_task
result = self.agent_executor.invoke(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agents\crew_agent_executor.py”, line 102, in invoke
formatted_answer = self._invoke_loop()
^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agents\crew_agent_executor.py”, line 193, in _invoke_loop
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agents\crew_agent_executor.py”, line 115, in _invoke_loop
answer = self.llm.call(
^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\llm.py”, line 164, in call
response = litellm.completion(**params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\utils.py”, line 1005, in wrapper
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\utils.py”, line 886, in wrapper
result = original_function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\main.py”, line 2938, in completion
raise exception_type(
^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\litellm_core_utils\exception_mapping_utils.py”, line 2146, in exception_type
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\litellm_core_utils\exception_mapping_utils.py”, line 312, in exception_type
raise AuthenticationError(
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 639, in completion
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 542, in completion
openai_client: OpenAI = self._get_openai_client( # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 317, in _get_openai_client
_new_client = OpenAI(
^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\openai_client.py”, line 110, in init
raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\main.py”, line 1608, in completion
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\main.py”, line 1581, in completion
response = openai_chat_completions.completion(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 649, in completion
raise OpenAIError(
litellm.llms.openai.common_utils.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agent.py”, line 345, in execute_task
result = self.agent_executor.invoke(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agents\crew_agent_executor.py”, line 102, in invoke
formatted_answer = self._invoke_loop()
^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agents\crew_agent_executor.py”, line 193, in _invoke_loop
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agents\crew_agent_executor.py”, line 115, in _invoke_loop
answer = self.llm.call(
^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\llm.py”, line 164, in call
response = litellm.completion(**params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\utils.py”, line 1005, in wrapper
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\utils.py”, line 886, in wrapper
result = original_function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\main.py”, line 2938, in completion
raise exception_type(
^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\litellm_core_utils\exception_mapping_utils.py”, line 2146, in exception_type
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\litellm_core_utils\exception_mapping_utils.py”, line 312, in exception_type
raise AuthenticationError(
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 639, in completion
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 542, in completion
openai_client: OpenAI = self._get_openai_client( # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 317, in _get_openai_client
_new_client = OpenAI(
^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\openai_client.py”, line 110, in init
raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\main.py”, line 1608, in completion
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\main.py”, line 1581, in completion
response = openai_chat_completions.completion(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\llms\openai\openai.py”, line 649, in completion
raise OpenAIError(
litellm.llms.openai.common_utils.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “”, line 198, in _run_module_as_main
File “”, line 88, in run_code
File "C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Scripts\run_crew.exe_main
.py", line 8, in
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project\src\first_project\main.py”, line 21, in run
FirstProject().crew().kickoff(inputs=inputs)
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\crew.py”, line 553, in kickoff
result = self._run_sequential_process()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\crew.py”, line 660, in _run_sequential_process
return self._execute_tasks(self.tasks)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\crew.py”, line 758, in _execute_tasks
task_output = task.execute_sync(
^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\task.py”, line 192, in execute_sync
return self._execute_core(agent, context, tools)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\task.py”, line 250, in _execute_core
result = agent.execute_task(
^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agent.py”, line 357, in execute_task
result = self.execute_task(task, context, tools)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agent.py”, line 357, in execute_task
result = self.execute_task(task, context, tools)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agent.py”, line 356, in execute_task
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agent.py”, line 345, in execute_task
result = self.agent_executor.invoke(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agents\crew_agent_executor.py”, line 102, in invoke
formatted_answer = self._invoke_loop()
^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agents\crew_agent_executor.py”, line 193, in _invoke_loop
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\agents\crew_agent_executor.py”, line 115, in _invoke_loop
answer = self.llm.call(
^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\crewai\llm.py”, line 164, in call
response = litellm.completion(**params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\utils.py”, line 1005, in wrapper
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\utils.py”, line 886, in wrapper
result = original_function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\main.py”, line 2938, in completion
raise exception_type(
^^^^^^^^^^^^^^^
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\litellm_core_utils\exception_mapping_utils.py”, line 2146, in exception_type
raise e
File “C:\Users\manas\Workspce\AiAgents\CrewAi_Course\first_project.venv\Lib\site-packages\litellm\litellm_core_utils\exception_mapping_utils.py”, line 312, in exception_type
raise AuthenticationError(
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
An error occurred while running the crew: Command ‘[‘uv’, ‘run’, ‘run_crew’]’ returned non-zero exit status 1.

Did you set the api_key during initialization or export a environment variable to set the api key that you got from ChatGPTs’ website?

Idk i ran it again and it worked, thanks btw

Same problem here. It fails although sometimes it manages to run.
I cannot find any explanation.