AttributeError or RuntimeError: LLM not configured
crewai.agents.agent.Agent initialization error: LLM parameter missing or API key not found
Stack trace
AttributeError: 'NoneType' object has no attribute 'call'
File "/path/to/site-packages/crewai/agents/agent.py", line 247, in execute
response = self.llm.call(messages=formatted_messages)
AttributeError: 'NoneType' object has no attribute 'call'
OR
RuntimeError: Agent must have an LLM configured. Pass an llm parameter when creating the Agent or set OPENAI_API_KEY environment variable.
File "/path/to/site-packages/crewai/agents/agent.py", line 89, in __init__
raise RuntimeError("Agent must have an LLM configured...") Why it happens
CrewAI Agents require an LLM instance to execute tasks. When you create an Agent without explicitly passing an llm parameter, it attempts to use a default LLM initialized from the OPENAI_API_KEY environment variable. If this variable is not set, or if you pass llm=None, the Agent's llm attribute becomes None. When the Agent later tries to call self.llm.call() during task execution, it crashes because None has no call() method. This is a common initialization error when deploying to new environments or running locally without proper environment configuration.
Detection
Check for OPENAI_API_KEY in your environment before instantiating Agents: print(os.environ.get('OPENAI_API_KEY')). Add logging in your Agent initialization to confirm the llm parameter is not None: agent.llm will show the LLM instance or None. Use env linting tools like python-dotenv to validate .env files before running code.
Causes & fixes
OPENAI_API_KEY environment variable is not set when Agent is created without explicit llm parameter
Set OPENAI_API_KEY in your environment before running the code: export OPENAI_API_KEY='sk-...' (Linux/Mac) or set OPENAI_API_KEY=sk-... (Windows), or use python-dotenv: load_dotenv() in your main script
Explicitly passing llm=None or llm=False to the Agent constructor
Remove the llm=None parameter, or pass a valid LLM instance: from crewai import Agent; from langchain_openai import ChatOpenAI; llm = ChatOpenAI(model='gpt-4o-mini'); agent = Agent(role='...',goal='...',llm=llm)
Using a .env file with OPENAI_API_KEY but not calling load_dotenv() before Agent initialization
Add from dotenv import load_dotenv; load_dotenv() at the very top of your main script, before any Agent or Crew instantiation
OPENAI_API_KEY is set to an empty string or invalid format (not starting with 'sk-')
Verify your OpenAI API key is valid by visiting https://platform.openai.com/account/api-keys. Copy the full key (e.g., 'sk-proj-...') and set it correctly: export OPENAI_API_KEY='sk-proj-your-actual-key-here'
Code: broken vs fixed
import os
from crewai import Agent, Task, Crew
# BROKEN: No OPENAI_API_KEY set, and no explicit llm passed to Agent
agent = Agent(
role='Data Analyst',
goal='Analyze sales data',
backstory='Expert analyst with 10 years experience'
# BUG: llm parameter is missing — Agent will try to use default from OPENAI_API_KEY env var
# If OPENAI_API_KEY is not set, agent.llm will be None
)
task = Task(
description='Analyze Q4 sales',
agent=agent
)
crew = Crew(
agents=[agent],
tasks=[task]
)
# This line crashes: AttributeError: 'NoneType' object has no attribute 'call'
result = crew.kickoff() import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew
from langchain_openai import ChatOpenAI
# FIXED: Load environment variables from .env file
load_dotenv()
# Verify OPENAI_API_KEY is set before creating LLM
api_key = os.environ.get('OPENAI_API_KEY')
if not api_key:
raise ValueError('OPENAI_API_KEY environment variable is not set. Set it before running this script.')
# FIXED: Create explicit LLM instance and pass it to Agent
llm = ChatOpenAI(model='gpt-4o-mini', api_key=api_key)
agent = Agent(
role='Data Analyst',
goal='Analyze sales data',
backstory='Expert analyst with 10 years experience',
llm=llm # FIXED: Now agent has a valid LLM instance
)
task = Task(
description='Analyze Q4 sales',
agent=agent
)
crew = Crew(
agents=[agent],
tasks=[task]
)
result = crew.kickoff()
print('Crew execution completed successfully') Workaround
If you cannot modify environment setup or load_dotenv() in your deployment, bypass CrewAI's default LLM lookup by creating a mock LLM class that returns hardcoded responses: class DummyLLM: def call(self, messages): return 'response'. Pass this to your Agent. This is NOT recommended for production but allows code to run while you fix the underlying environment issue.
Prevention
Always validate OPENAI_API_KEY exists before instantiating any Agent or Crew. Use a config module that centralizes LLM setup: create a get_llm() function that checks environment variables, raises clear errors if missing, and returns a configured ChatOpenAI instance. Pass this to all Agents. In CI/CD, ensure OPENAI_API_KEY is set as a secret environment variable before the deployment container starts. Use structured logging to record which LLM each Agent is initialized with.