# Issue with Invalid API Key in CrewAI Agent for GEMINI API

**URL:** <https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010>\
**Category:** CrewAI Community Support\
**Created:** [February 19, 2025, 11:50pm UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010 "2025-02-19T23:50:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerin\_Kumar](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/jerin_kumar/32/2678_2.png) [@Jerin\_Kumar](https://community.crewai.com/u/Jerin_Kumar)\
**Post date:** [February 19, 2025, 11:50pm UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/1 "2025-02-19T23:50:37Z")

</div>

I’m encountering an issue when using GEMINI through the CrewAI agent. Despite using a valid API key, I receive the following error:  
Error during LLM call: litellm.AuthenticationError: geminiException - {  
“error”: {  
“code”: 400,  
“message”: “API key not valid. Please pass a valid API key.”,  
“status”: “INVALID\_ARGUMENT”,

```
}

```

I have confirmed that my API key is valid and properly configured in CrewAI, but the **“API\_KEY\_INVALID”** error persists.

---

<div class="post-metadata">

**Author:** ![gfdc](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/gfdc/32/2018_2.png) [@gfdc](https://community.crewai.com/u/gfdc)\
**Post date:** [February 20, 2025, 9:57pm UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/2 "2025-02-20T21:57:32Z")

</div>

I have the same error, I don’t know how to fix this 😭

---

<div class="post-metadata">

**Author:** ![Nikhil\_J](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/nikhil_j/32/2701_2.png) [@Nikhil\_J](https://community.crewai.com/u/Nikhil_J)\
**Post date:** [February 21, 2025, 4:08am UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/3 "2025-02-21T04:08:47Z")

</div>

Same issue here.  
Even if i am creating new key and using it its still showing the invalid key error.

---

<div class="post-metadata">

**Author:** ![William](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/william/32/2821_2.png) [@William](https://community.crewai.com/u/William)\
**Post date:** [February 28, 2025, 1:37pm UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/4 "2025-02-28T13:37:17Z")

</div>

I encountered this error and in my instance after a little digging through code, it was resolved by setting _GEMINI_\_API\_KEY with my API key value. I previously was trying to use GOOGLE\_API\_KEY.

Let me know if this helps or if you’re still stuck.

---

<div class="post-metadata">

**Author:** ![Thom\_Web\_Com](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/thom_web_com/32/2832_2.png) [@Thom\_Web\_Com](https://community.crewai.com/u/Thom_Web_Com)\
**Post date:** [March 2, 2025, 4:30pm UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/5 "2025-03-02T16:30:58Z")

</div>

Hi Team,

I’m also facing this error when trying to boostrap a little project from one single .py file. It works when creating the project using the crewai create though.

Here is the code in case it can help debug:

```
import warnings
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
import os
import litellm  

os.environ["LITELLM_MODEL"] = "gemini/gemini-1.5-flash"
os.environ["GEMINI_API_KEY"] = "my-actual-keys"

warnings.filterwarnings('ignore')

website_scraping_tool = ScrapeWebsiteTool(website_url='https://w3champions.com/player/{player_id}/statistics')
search_tool = SerperDevTool()

researcher = Agent(
    role="Player Statistics Researcher",
    goal="Find and extract the statistics of the following Warcraft III player: {player_id}",
    backstory="A dedicated researcher with years of experience in gaming and Warcraft III.",
    tools=[search_tool, website_scraping_tool]
)

data_analyst = Agent(
    role="Data Analyst specialized in Warcraft III",
    goal="Analyze {player_id}'s statistics and share useful and actionable insights about their play",
    backstory="A dedicated data analyst with years of experience in gaming and Warcraft III. You love finding insights on other players.",
)

research_task = Task(
    description="Find and extract the statistics of {player_id}. These stats are all stored on the following URL: https://w3champions.com/player/{player_id}/statistics. Use the tools you have to find and extract them.",
    expected_output="detailed report on a the player's statistics including: win rate vs. each race (orc, human, undead, elf, random), Hero usage & Hero win rate",
    agent=researcher
)

data_analysis_task = Task(
    description="Analyze {player_id}'s statistics and share useful insights for their opponent, who play the race {opponent_race}.",
    expected_output="Give 3 maps the opponent should not play against {player_id}, and which hero they are likely to play against them given their race.",
    agent=data_analyst
)

crew = Crew(
    agents=[researcher, data_analyst],
    tasks=[research_task, data_analysis_task],
    process=Process.sequential,
    default_llm={"model": "gemini/gemini-1.5-flash", "api_key": os.getenv("GEMINI_API_KEY"), "provider": "google"}
)

# player_id = str(input("What Player are you going to play against?"))
# opponent_race = str(input("what race do you play?"))

# results = crew.kickoff(inputs={"player_id": player_id, "opponent_race": opponent_race})
results = crew.kickoff(inputs={"player_id": "ThoMus%2321493", "opponent_race": "orc"})
print(results)

```

---

<div class="post-metadata">

**Author:** ![William](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/william/32/2821_2.png) [@William](https://community.crewai.com/u/William)\
**Post date:** [March 3, 2025, 2:07am UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/6 "2025-03-03T02:07:10Z")

</div>

Hey @Thom_Web_Com,

I’m still getting to grips with CrewAI and building a linter to help myself and others. But a few observations:

- I don’t believe there is a default\_llm property on the Crew - available attributes are here: [Crews - CrewAI](https://docs.crewai.com/concepts/crews) or in code: [crewAI/src/crewai/crew.py at main · crewAIInc/crewAI · GitHub](https://github.com/crewAIInc/crewAI/blob/main/src/crewai/crew.py#L73)
- General: Do not **ever** put API keys in code, though you substituted it out here - always set them via .env. I cannot repeat this enough: Never ever secrets/API keys in code. This goes beyond CrewAI and into general software engineering practices to avoid potential pain in the future.
- You can set the llm on the Agents or by setting environment variables.

**Setting the LLM on the agents via code**  
If you wish to, check here: [LLMs - CrewAI](https://docs.crewai.com/concepts/llms)

**Setting via environment variables**  
Put the following into your .env file:

```
MODEL=gemini/gemini-1.5-flash
GEMINI_API_KEY=your-actual-key

```

Source: [crewAI/src/crewai/utilities/llm\_utils.py at main · crewAIInc/crewAI · GitHub](https://github.com/crewAIInc/crewAI/blob/main/src/crewai/utilities/llm_utils.py#L80)

Note that you could use OPENAI\_MODEL\_NAME in lieu of MODEL, but it would be somewhat misleading. Make sure OPENAI\_MODEL\_NAME environment variable is not set, as it will take precedence over MODEL.

Can I ask from where you got the default\_llm property/attribute? I’m assuming an AI hallucination which is one of the reasons I’m building out a linter.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![rokbenko](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/rokbenko/32/492_2.png) [@rokbenko](https://community.crewai.com/u/rokbenko)\
**Post date:** [March 3, 2025, 2:38pm UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/7 "2025-03-03T14:38:20Z")

</div>

@William @Thom_Web_Com @Nikhil_J Guys, just use the LLM class. CrewAI’s LLM class leverages [LiteLLM](https://docs.litellm.ai/) in the background, which supports a wide range of [LLM providers](https://docs.litellm.ai/docs/providers). Among these is [Gemini via Google AI Studio](https://docs.litellm.ai/docs/providers/gemini), or [Gemini via Vertex AI](https://docs.litellm.ai/docs/providers/vertex).

Here’s a code example:

```auto
from crewai import Agent, LLM

my_llm = LLM(
    api_key=os.getenv("GEMINI_API_KEY"),
    model="gemini/gemini-1.5-pro-latest",
)

my_agent = Agent(
    ...,
    llm=my_llm,
)

```

---

<div class="post-metadata">

**Author:** ![Thom\_Web\_Com](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/thom_web_com/32/2832_2.png) [@Thom\_Web\_Com](https://community.crewai.com/u/Thom_Web_Com)\
**Post date:** [March 3, 2025, 3:21pm UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/8 "2025-03-03T15:21:44Z")

</div>

Hi Team,

@rokbenko & @Willian thanks a lot for your recommendations guys.

I tried again using the LLM class as suggested by both of you. I confirm it works.

I had to set the llm=my\_llm property to each Agent, though. I only have 2 agents so it’s fine, it’d be easier with a general llm property in the crew class, though 😃

You were right @Willian, this attribute does not exist and comes from the CrewAI GPT Assistant suggestion (= hallucination)

Funnily enough, gemini-2.0-flash-exp throws this error while gemini-1.5-flash works perfectly:  
Received None or empty response from LLM call.  
An unknown error occurred. Please check the details below.

---

<div class="post-metadata">

**Author:** ![Muhammad\_Hilal](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/muhammad_hilal/32/2955_2.png) [@Muhammad\_Hilal](https://community.crewai.com/u/Muhammad_Hilal)\
**Post date:** [March 9, 2025, 10:19am UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/9 "2025-03-09T10:19:26Z")

</div>

> [@William](#):
>
> _GEMINI_\_API\_KEY

I was configuring my `.env` file like this:  
GEMINI\_API\_KEY=AIza  
MODEL\_NAME=gemini/gemini-1.5-flash

But it was showing an error. I aligned it the way you suggested, and it worked:  
MODEL=gemini/gemini-1.5-flash  
GEMINI\_API\_KEY=your-actual-key

I hope this rigidity will be improved in the future.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Saber\_You](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/saber_you/32/4230_2.png) [@Saber\_You](https://community.crewai.com/u/Saber_You)\
**Post date:** [August 5, 2025, 4:05pm UTC](https://community.crewai.com/t/issue-with-invalid-api-key-in-crewai-agent-for-gemini-api/4010/10 "2025-08-05T16:05:54Z")

</div>

Thanks so much for this solution!  
When I first encountered an errorA, I changed the `GEMINI_API_KEY` to `GOOGLE_API_KEY`, and it didn’t work.😅 I forgot to switch it back, which then led to the ‘Invalid API key’ issue. I tried so many different fixes until I found this post. Your advice to change the API key back was exactly what I needed 👏
