# 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

**URL:** <https://community.crewai.com/t/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/2197>\
**Category:** CrewAI Community Support\
**Created:** [December 22, 2024, 9:27am UTC](https://community.crewai.com/t/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/2197 "2024-12-22T09:27:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/manas/32/1585_2.png) [@Manas](https://community.crewai.com/u/Manas)\
**Post date:** [December 22, 2024, 9:27am UTC](https://community.crewai.com/t/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/2197/1 "2024-12-22T09:27:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vkrishna](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/vkrishna/32/1268_2.png) [@vkrishna](https://community.crewai.com/u/vkrishna)\
**Post date:** [December 22, 2024, 9:47am UTC](https://community.crewai.com/t/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/2197/2 "2024-12-22T09:47:49Z")

</div>

> [@Manas](#):
>
> OPENAI\_API\_KEY

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

---

<div class="post-metadata">

**Author:** ![Manas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/manas/32/1585_2.png) [@Manas](https://community.crewai.com/u/Manas)\
**Post date:** [December 22, 2024, 10:23am UTC](https://community.crewai.com/t/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/2197/3 "2024-12-22T10:23:38Z")

</div>

Idk i ran it again and it worked, thanks btw

---

<div class="post-metadata">

**Author:** ![NilOleart](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/niloleart/32/2040_2.png) [@NilOleart](https://community.crewai.com/u/NilOleart)\
**Post date:** [January 15, 2025, 2:58pm UTC](https://community.crewai.com/t/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/2197/4 "2025-01-15T14:58:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mhrsh-dev](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@mhrsh-dev](https://community.crewai.com/u/mhrsh-dev)\
**Post date:** [March 15, 2025, 1:33pm UTC](https://community.crewai.com/t/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/2197/5 "2025-03-15T13:33:51Z")

</div>

I encountered the same problem.  
It disappeared when removing “planning = True,” argument in “Crew(…)”.
