CrewAI does not host its own language models — instead, it requires connecting external APIs like OpenAI, Anthropic, Groq, or local Ollama instances for the agents to execute tasks.
Publicidade
Why CrewAI Requires External API Keys
CrewAI is an orchestration framework that coordinates task division, collaboration, and tool usage among multiple AI agents. The framework does not host large language models locally — it acts as a manager that routes prompts to external APIs.
This decoupling allows developers to assign different models to different agents in the same project based on task complexity. A researcher agent can run on Claude Sonnet for deep analytical tasks, while a simple formatting agent uses Groq to minimize costs.
Setting Up OpenAI API Keys
OpenAI is the default provider for CrewAI. When no LLM is specified in an agent, the framework automatically searches for the OPENAI_API_KEY environment variable.
To obtain a key, log into platform.openai.com/api-keys and generate a new secret key starting with sk-. Create a .env file in the project's root folder and insert the following line without spaces or quotation marks:
OPENAI_API_KEY=sk-proj-your_key_here
Load these environment variables in Python before importing any CrewAI classes using the python-dotenv library. This prevents execution errors due to missing API keys:
from dotenv import load_dotenv
load_dotenv() # Always run before importing CrewAI modules
Configuring Anthropic (Claude) in CrewAI
Anthropic models like Claude 3.5 Sonnet are accessed using the Anthropic API.
Chaves are managed at console.anthropic.com and start with sk-ant-.
Add this variable to the .env file:
Publicidade
ANTHROPIC_API_KEY=sk-ant-your_key_here
To configure a specific agent to use Claude, specify the model string using the LiteLLM naming convention. The prefix anthropic/ indicates to the routing library that the Anthropic API should be used:
from crewai import Agent
analyst_agent = Agent(
role="Senior Analyst",
goal="Analyze data and write reports",
backstory="Data expert with years of research experience",
llm="anthropic/claude-3-5-sonnet-20241022",
verbose=True
)
Configuring Groq API Keys (Fast and Cost-Efficient Models)
Groq provides ultra-fast inference speed for open-source models like Llama 3, Mixtral, and Gemma. To generate a free developer key, visit console.groq.com.
Insert the key into the .env file:
GROQ_API_KEY=gsk_your_key_here
In your Python script, declare the model using the groq/ prefix. The Llama 3 70B model is highly recommended for general language tasks due to its speed and reasoning quality:
fast_writer = Agent(
role="Content Copywriter",
goal="Draft articles quickly",
backstory="Experienced writer specializing in fast copy drafting",
llm="groq/llama3-70b-8192"
)
Configuring Ollama for Free Local Execution
Ollama lets developers run language models locally, which eliminates external API costs and keeps all data completely private. After installing Ollama on your computer and running a model using ollama pull llama3, define the model in CrewAI:
local_agent = Agent(
role="Local Data Processor",
goal="Process documents offline with complete privacy",
backstory="Local agent specializing in sensitive data analysis",
llm="ollama/llama3"
)
Ollama does not require API keys. The CrewAI framework automatically routes prompts to http://localhost:11434, which is the default port for local Ollama instances.
Publicidade
Provider
Recommended Model
API Cost/1k tokens
Speed
Privacy Level
Best Application
OpenAI
GPT-4o
$0.005 (input)
Fast
Cloud Hosted
Complex logic and math
Anthropic
Claude Sonnet 3.5
$0.003 (input)
Fast
Cloud Hosted
Writing and summarization
Groq
Llama 3 70B
$0.00059 (input)
Very Fast
Cloud Hosted
Fast and cheap steps
Ollama
Llama 3 8B local
$0 (Free)
Hardware dependent
Full Local
Prototyping, sensitive data
🔒 Security Best Practice: Always add your .env file to the project's .gitignore file. This prevents committing private credentials to public version control systems. Maintain a .env.example file without secret values to document key requirements.
Using Python-Dotenv for Secure Keys Management
The python-dotenv library is the standard tool for loading configuration values in Python projects. Install the package using pip install python-dotenv and load the environment settings at the beginning of the application.
For complex setups, loading environment-specific configuration files is supported. Developers can pass file paths explicitly, like load_dotenv(".env.production"), to manage keys across staging and production servers.
Production Guidelines for API Credentials
For staging and production servers, avoid storing keys inside files on disk. Utilize cloud secret managers or environment configuration panels on hosting providers to inject values safely.
Generate unique keys for each application environment and monitor billing usage regularly. Restricting API keys to minimum required permissions prevents data exposure if a key is compromised.
No, CrewAI can run entirely on Groq, Anthropic, or local Ollama models. While it defaults to OpenAI when no model is explicitly set, declaring another LLM in the agent config eliminates OpenAI requirements.
How to verify if environment keys are loading correctly?
Run a test script print command: import os; print(os.getenv("OPENAI_API_KEY")). If the output returns None, the .env file is in the wrong directory or variables are misnamed.
Publicidade
Is Groq API free to use with CrewAI?
Groq offers a free tier with rate limits on a minute and daily basis. For development and testing, this free allowance is sufficient to run agents without incurring costs.
Is it possible to assign different API keys for different agents?
Yes, each agent can connect to a different API endpoint or provider. Simply declare the model name and provide the correct environment variable for each agent in the script.
Conclusion
Configuring API keys in CrewAI is simple when using a .env file, python-dotenv, and LiteLLM conventions to specify the preferred models. Combining different providers is a powerful way to balance performance and budget.
Using Groq or local Ollama models during development, and switching to Claude or GPT-4o for final production tasks, is the most cost-effective approach for agentic workflows in 2026.