How to orchestrate multi-agent workflows with CrewAI

CrewAI lets you define teams of specialised AI agents that collaborate to complete complex tasks. This guide covers creating agents with roles, assigning tasks, and running a complete crew pipeline.

What CrewAI is and why it matters

CrewAI is a Python framework for orchestrating role-playing autonomous AI agents. Instead of a single agentAgentAn LLM-powered system that can take actions, use tools, and pursue multi-step goals autonomously without human input at each step.Learn more → trying to do everything, CrewAI lets you define a 'crew' — a team where each agent has a specific role (researcher, writer, critic), a goal, and a backstory that shapes its behaviour.

The key insight behind CrewAI is that specialised agents outperform generalist ones on complex tasks. A researcher agent optimised for gathering information hands its output to a writer agent optimised for clear prose — the combination produces better results than a single agent doing both.

Install and set up

Install CrewAI: `pip install crewai crewai-tools`. You also need an LLMLLMLarge Language Model — a neural network trained on vast amounts of text data that can understand and generate human-quality language across a wide range of tasks.Learn more → backend — CrewAI works with any OpenAI-compatible API. Set your key: `export OPENAI_API_KEY=sk-...`. For local models, point CrewAI at Ollama: set `OPENAI_API_BASE=http://localhost:11434/v1` and use model `ollama/llama3.3`.

CrewAI uses the `langchain` LLM abstraction under the hood. You can also configure Anthropic, Google Gemini, or any other LangChain-supported provider by setting the appropriate environment variables.

Define your agents

Each agent is created with `Agent(role=..., goal=..., backstory=..., verbose=True, llm=...)`. The `role` is a short label ('Senior Researcher'), `goal` describes what the agent is trying to achieve, and `backstory` is a paragraph describing the agent's expertise and personality — this shapes how it reasons.

Example: `researcher = Agent(role='Market Research Analyst', goal='Find accurate, up-to-date information on AI industry trends', backstory='You are an expert analyst who delivers concise, fact-backed insights without filler.', tools=[SerperDevTool()], llm=ChatOpenAI(model='gptGPTGenerative Pre-trained Transformer — the model architecture and family name behind OpenAI's most famous models, from GPT-2 to GPT-5.Learn more →-4o-mini'))`. The `tools` list gives the agent access to external capabilities like web search.

Define tasks and assemble the crew

Tasks specify the work each agent must complete: `Task(description='Research the top 5 LLM API providers in 2025. Include pricing and context windowContext WindowThe maximum amount of text (measured in tokens) that a model can read and reason over in a single interaction, including your prompt, any documents, and previous conversation history.Learn more → sizes.', agent=researcher, expected_output='A bullet-point list with name, pricing, and context window for each provider.')`. The `expected_output` field is critical — it tells the agent what 'done' looks like.

Assemble the crew: `crew = Crew(agents=[researcher, writer], tasks=[research_task, write_task], process=Process.sequential, verbose=True)`. Use `Process.sequential` to run tasks in order, or `Process.hierarchical` to have a manager agent delegate tasks dynamically.

Run the crew and handle output

Kick off the crew: `result = crew.kickoff(inputs={'topic': 'open-source LLMs in 2025'})`. The inputs dict is interpolated into task descriptions wherever you use `{topic}` placeholders. Execution typically takes 30–120 seconds for a two-agent crew with web search.

The result is a string containing the final agent's output. For structured data, instruct the last agent to respond as JSON and parse the result: `import json; data = json.loads(result.raw)`. CrewAI also supports Pydantic output schemas via `output_pydantic` on the final task.

Production tips

Enable memory to let agents learn from previous runs: set `memory=True` on the Crew. CrewAI stores short-term (conversation), long-term (SQLite), and entity memory (key facts) automatically. This is particularly useful for iterative research tasks.

Control costs by assigning cheaper models (GPT-4o-mini, Claude Haiku) to simpler agents and expensive models only to the agents that need deep reasoning. A three-agent crew where only the final synthesis step uses GPT-4o can reduce API costs by 60–70% compared to using GPT-4o throughout.