How to Fix 'async event loop error' in CrewAI (Python)
What the error means
If you’re seeing RuntimeError: This event loop is already running or a CrewAI-flavored variant like async event loop error, you’ve got an asyncio lifecycle problem, not a CrewAI-specific bug. It usually shows up when you call async code from a place that already has an active event loop, like Jupyter, FastAPI, Streamlit, or another async framework.
In practice, this happens when you mix asyncio.run(), await, and sync wrappers in the wrong place. CrewAI agents, tasks, and crews can run fine in Python, but the moment you nest event loops or block inside one, Python throws.
The Most Common Cause
The #1 cause is calling asyncio.run() inside an environment that already has a running loop.
This is common when people copy a working script into Jupyter or a web app and keep the same entrypoint pattern. With CrewAI, the mistake usually looks like wrapping crew.kickoff() or an async tool call the wrong way.
Broken vs fixed
| Broken pattern | Right pattern |
|---|---|
Calls asyncio.run() from inside an existing loop | Uses await in async contexts |
| Blocks on async work from sync code in notebooks | Uses notebook-friendly execution |
Triggers RuntimeError: This event loop is already running | Keeps one clear async boundary |
# BROKEN
import asyncio
from crewai import Agent, Task, Crew
agent = Agent(
role="Researcher",
goal="Find facts",
backstory="You research company data."
)
task = Task(
description="Summarize the latest quarterly report.",
agent=agent
)
crew = Crew(agents=[agent], tasks=[task])
# This breaks in Jupyter / FastAPI / any running loop
result = asyncio.run(crew.kickoff())
print(result)
# FIXED: async context
from crewai import Agent, Task, Crew
agent = Agent(
role="Researcher",
goal="Find facts",
backstory="You research company data."
)
task = Task(
description="Summarize the latest quarterly report.",
agent=agent
)
crew = Crew(agents=[agent], tasks=[task])
async def main():
result = await crew.kickoff()
print(result)
# In a script:
# import asyncio
# asyncio.run(main())
If you’re in Jupyter, don’t use asyncio.run() at all. Just do:
result = await crew.kickoff()
That’s the clean fix for most cases.
Other Possible Causes
1. Mixing sync and async tool functions
If your CrewAI agent uses tools that are declared sync but internally call async code, you’ll get loop errors or hanging behavior.
# BROKEN
from crewai_tools import tool
import asyncio
@tool("fetch_data")
def fetch_data():
return asyncio.run(fetch_remote_data()) # bad inside running loop
# FIXED
from crewai_tools import tool
@tool("fetch_data")
async def fetch_data():
return await fetch_remote_data()
If the tool framework expects sync functions only, move the async work outside the tool or wrap it at the app boundary, not inside the tool itself.
2. Running CrewAI inside FastAPI request handlers with nested loops
FastAPI endpoints are already async. If you call asyncio.run() there, Python will complain immediately.
# BROKEN
from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get("/run")
async def run_crew():
result = asyncio.run(crew.kickoff())
return {"result": str(result)}
# FIXED
from fastapi import FastAPI
app = FastAPI()
@app.get("/run")
async def run_crew():
result = await crew.kickoff()
return {"result": str(result)}
3. Using Streamlit callbacks with blocking async code
Streamlit reruns scripts often and doesn’t like nested event-loop management.
# BROKEN
import streamlit as st
import asyncio
if st.button("Run"):
result = asyncio.run(crew.kickoff())
st.write(result)
# FIXED
import streamlit as st
if st.button("Run"):
result = await crew.kickoff()
st.write(result)
If your Streamlit version or runtime doesn’t support top-level await, move the async work into a separate service layer and call it through a sync wrapper outside Streamlit’s render path.
4. Old Python or incompatible dependency versions
Some event-loop issues come from mismatched versions of crewai, pydantic, langchain, or Python itself. I’ve seen this with environments pinned to older interpreter versions and half-upgraded packages.
Check your stack:
python --version
pip show crewai langchain pydantic anyio asyncio-run-in-threadpool 2>/dev/null
pip freeze | grep -E "crewai|langchain|pydantic|anyio"
A safe move is to upgrade to a supported Python version and reinstall dependencies cleanly:
pip install -U crewai langchain pydantic anyio
How to Debug It
- •
Read the exact traceback
- •If you see
RuntimeError: This event loop is already running, you’re nesting loops. - •If you see
coroutine was never awaited, you forgot anawait. - •If you see errors around
Task,Crew, or tool execution, check where async boundaries are being crossed.
- •If you see
- •
Find every
asyncio.run()- •Search your codebase for it.
- •Remove it from notebooks, FastAPI routes, Streamlit callbacks, and any function that may already run inside an event loop.
- •
Trace where CrewAI is called
- •Look at whether you’re calling:
- •
crew.kickoff() - •a custom async tool
- •an LLM client directly inside a sync function
- •
- •The bug is usually one layer above where the stack trace points.
- •Look at whether you’re calling:
- •
Isolate with a minimal script
- •Run CrewAI in a plain
.pyfile with one agent and one task. - •If it works there but fails in your app, the issue is your host runtime, not CrewAI itself.
- •Run CrewAI in a plain
Example minimal test:
import asyncio
from crewai import Agent, Task, Crew
agent = Agent(role="Tester", goal="Verify execution", backstory="Tests things.")
task = Task(description="Say hello.", agent=agent)
crew = Crew(agents=[agent], tasks=[task])
async def main():
print(await crew.kickoff())
asyncio.run(main())
If this works in terminal but fails in notebook or API server, stop debugging CrewAI internals and fix your runtime integration.
Prevention
- •Keep one rule: sync entrypoints use
asyncio.run(), async entrypoints useawait. - •Don’t put event-loop management inside tools, agents, or task logic.
- •Test CrewAI flows in both:
- •plain Python scripts
- •your actual host runtime like FastAPI or Jupyter
- •Pin compatible versions of Python and dependencies before shipping to production.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit