Overview
Knowledge in CrewAI is a powerful system that allows AI agents to access and utilize external information sources during their tasks. Think of it as giving your agents a reference library they can consult while working.Key benefits of using Knowledge:
- Enhance agents with domain-specific information
- Support decisions with real-world data
- Maintain context across conversations
- Ground responses in factual information
Quickstart Examples
Vector store (RAG) client configuration
CrewAI exposes a provider-neutral RAG client abstraction for vector stores. The default provider is ChromaDB, and Qdrant is supported as well. You can switch providers using configuration utilities. Supported today:- ChromaDB (default)
- Qdrant
Code
Basic String Knowledge Example
Code
Web Content Knowledge Example
You need to install
docling for the following example to work: uv add doclingCode
Supported Knowledge Sources
CrewAI supports various types of knowledge sources out of the box:Text Sources
- Raw strings
- Text files (.txt)
- PDF documents
Structured Data
- CSV files
- Excel spreadsheets
- JSON documents
Text File Knowledge Source
PDF Knowledge Source
CSV Knowledge Source
Excel Knowledge Source
JSON Knowledge Source
Please ensure that you create the ./knowledge folder. All source files (e.g., .txt, .pdf, .xlsx, .json) should be placed in this folder for centralized management.
Agent vs Crew Knowledge: Complete Guide
Understanding Knowledge Levels: CrewAI supports knowledge at both agent and crew levels. This section clarifies exactly how each works, when they’re initialized, and addresses common misconceptions about dependencies.
How Knowledge Initialization Actually Works
Here’s exactly what happens when you use knowledge:Agent-Level Knowledge (Independent)
What Happens During crew.kickoff()
When you call crew.kickoff(), here’s the exact sequence:
Storage Independence
Each knowledge level uses independent storage collections:Complete Working Examples
Example 1: Agent-Only Knowledge
Example 2: Both Agent and Crew Knowledge
Example 3: Multiple Agents with Different Knowledge
Knowledge Configuration
You can configure the knowledge configuration for the crew or agent.Code
Supported Knowledge Parameters
List of knowledge sources that provide content to be stored and queried. Can include PDF, CSV, Excel, JSON, text files, or string content.
Name of the collection where the knowledge will be stored. Used to identify different sets of knowledge. Defaults to “knowledge” if not provided.
Custom storage configuration for managing how the knowledge is stored and retrieved. If not provided, a default storage will be created.
Knowledge Storage Transparency
Understanding Knowledge Storage: CrewAI automatically stores knowledge sources in platform-specific directories using ChromaDB for vector storage. Understanding these locations and defaults helps with production deployments, debugging, and storage management.
Where CrewAI Stores Knowledge Files
By default, CrewAI uses the same storage system as memory, storing knowledge in platform-specific directories:Default Storage Locations by Platform
macOS:Finding Your Knowledge Storage Location
To see exactly where CrewAI is storing your knowledge files:Controlling Knowledge Storage Locations
Option 1: Environment Variable (Recommended)
Option 2: Custom Knowledge Storage
Option 3: Project-Specific Knowledge Storage
Default Embedding Provider Behavior
Default Embedding Provider: CrewAI defaults to OpenAI embeddings (
text-embedding-3-small) for knowledge storage, even when using different LLM providers. You can easily customize this to match your setup.Understanding Default Behavior
Customizing Knowledge Embedding Providers
Configuring Azure OpenAI Embeddings
When using Azure OpenAI embeddings:- Make sure you deploy the embedding model in Azure platform first
- Then you need to use the following configuration:
Advanced Features
Query Rewriting
CrewAI implements an intelligent query rewriting mechanism to optimize knowledge retrieval. When an agent needs to search through knowledge sources, the raw task prompt is automatically transformed into a more effective search query.How Query Rewriting Works
- When an agent executes a task with knowledge sources available, the
_get_knowledge_search_querymethod is triggered - The agent’s LLM is used to transform the original task prompt into an optimized search query
- This optimized query is then used to retrieve relevant information from knowledge sources
Benefits of Query Rewriting
Improved Retrieval Accuracy
By focusing on key concepts and removing irrelevant content, query rewriting helps retrieve more relevant information.
Context Awareness
The rewritten queries are designed to be more specific and context-aware for vector database retrieval.
Example
Knowledge Events
CrewAI emits events during the knowledge retrieval process that you can listen for using the event system. These events allow you to monitor, debug, and analyze how knowledge is being retrieved and used by your agents.Available Knowledge Events
- KnowledgeRetrievalStartedEvent: Emitted when an agent starts retrieving knowledge from sources
- KnowledgeRetrievalCompletedEvent: Emitted when knowledge retrieval is completed, including the query used and the retrieved content
- KnowledgeQueryStartedEvent: Emitted when a query to knowledge sources begins
- KnowledgeQueryCompletedEvent: Emitted when a query completes successfully
- KnowledgeQueryFailedEvent: Emitted when a query to knowledge sources fails
- KnowledgeSearchQueryFailedEvent: Emitted when a search query fails
Example: Monitoring Knowledge Retrieval
Custom Knowledge Sources
CrewAI allows you to create custom knowledge sources for any type of data by extending theBaseKnowledgeSource class. Let’s create a practical example that fetches and processes space news articles.
Space News Knowledge Source Example
Debugging and Troubleshooting
Debugging Knowledge Issues
Check Agent Knowledge Initialization
Verify Knowledge Storage Locations
Test Knowledge Retrieval
Inspect Knowledge Collections
Check Knowledge Processing
Common Knowledge Storage Issues
“File not found” errors:Knowledge Reset Commands
Clearing Knowledge
If you need to clear the knowledge stored in CrewAI, you can use thecrewai reset-memories command with the --knowledge option.
Command
Best Practices
Content Organization
Content Organization
- Keep chunk sizes appropriate for your content type
- Consider content overlap for context preservation
- Organize related information into separate knowledge sources
Performance Tips
Performance Tips
- Adjust chunk sizes based on content complexity
- Configure appropriate embedding models
- Consider using local embedding providers for faster processing
One Time Knowledge
One Time Knowledge
- With the typical file structure provided by CrewAI, knowledge sources are embedded every time the kickoff is triggered.
- If the knowledge sources are large, this leads to inefficiency and increased latency, as the same data is embedded each time.
- To resolve this, directly initialize the knowledge parameter instead of the knowledge_sources parameter.
- Link to the issue to get complete idea Github Issue
Knowledge Management
Knowledge Management
- Use agent-level knowledge for role-specific information
- Use crew-level knowledge for shared information all agents need
- Set embedders at agent level if you need different embedding strategies
- Use consistent collection naming by keeping agent roles descriptive
- Test knowledge initialization by checking agent.knowledge after kickoff
- Monitor storage locations to understand where knowledge is stored
- Reset knowledge appropriately using the correct command types
Production Best Practices
Production Best Practices
- Set
CREWAI_STORAGE_DIRto a known location in production - Choose explicit embedding providers to match your LLM setup and avoid API key conflicts
- Monitor knowledge storage size as it grows with document additions
- Organize knowledge sources by domain or purpose using collection names
- Include knowledge directories in your backup and deployment strategies
- Set appropriate file permissions for knowledge files and storage directories
- Use environment variables for API keys and sensitive configuration
