CrewAI Knowledge
Knowledge in CrewAI enables agents to reference explicit facts when performing tasks. Knowledge sources supply structured or unstructured information directly, allowing agents to answer questions accurately, provide support or generate content that relies on real-world data. By using knowledge sources, we can tailor our agents to specific domains, ensure reliability and enhance context-aware interactions.
Exploring Available Knowledge Sources
CrewAI supports multiple knowledge sources to supply agents with factual information from different formats. To explore all available sources and their documentation, the help() function can be used.
import crewai
help(crewai.knowledge.source)
Output:

Understanding Knowledge Sources
CrewAI offers multiple knowledge sources to feed information to agents Each is suited for different types of data and use cases.
1. base_file_knowledge_source
Base class for knowledge sources that read from files. It provides a foundation for other file-based sources.
- file_path: Path to the file containing knowledge.
- chunk_size: Maximum size of text chunks for processing.
- split_method: Method to split content into chunks (e.g., sentence, paragraph).
file_source = BaseFileKnowledgeSource(
file_path="sample_data.txt",
chunk_size=2000,
split_method="sentence"
)
Note: We will demonstrate a full example implementation towards the end of the article.
2. base_knowledge_source
Generic base class for creating custom knowledge sources. Useful when building specialized sources.
- name: Name of the knowledge source.
- verbose: Enable debug output.
custom_source = BaseKnowledgeSource(
name="custom_source",
verbose=True
)
3. crew_docling_source
Integrates knowledge from Crew’s internal Docling system. Ideal for agents that reuse Crew documentation.
- doc_id: ID of the Docling document to load.
- verbose: Enable debug output.
docling_source = CrewDoclingSource(
doc_id="doc_12345",
verbose=True
)
4. csv_knowledge_source
Loads knowledge from CSV files making it suitable for tabular data.
- file_path: Path to the CSV file.
- delimiter: Column separator (default is ,).
- encoding: File encoding.
- verbose: Enable debug output.
csv_source = CSVKnowledgeSource(
file_path="Sample_data.csv",
delimiter=",",
encoding="utf-8",
verbose=True
)
5. excel_knowledge_source
Loads knowledge from Excel spreadsheets, supporting structured business or research data.
- file_path: Path to the Excel file.
- sheet_name: Sheet to read.
- encoding: File encoding.
- verbose: Enable debug output.
excel_source = ExcelKnowledgeSource(
file_path="sample_data.xlsx",
sheet_name="Sheet1",
encoding="utf-8",
verbose=True
)
6. json_knowledge_source
Loads knowledge from JSON files, ideal for hierarchical or structured data.
- file_path: Path to the JSON file.
- encoding: File encoding.
- verbose: Enable debug output.
json_source = JSONKnowledgeSource(
file_path="sample_data.json",
encoding="utf-8",
verbose=True
)
7. pdf_knowledge_source
Loads knowledge from PDF documents. Suitable for reports, manuals and research papers.
- file_paths: List of PDF files.
- chunk_size: Maximum characters per chunk.
- chunk_overlap: Overlap between chunks to preserve context.
pdf_source = PDFKnowledgeSource(
file_paths=["sample_data.pdf"],
chunk_size=4000,
chunk_overlap=200
)
8. string_knowledge_source
Loads knowledge from a string of text. Useful for small snippets or predefined facts.
- content: The text containing knowledge.
- verbose: Enable debug output.
string_source = StringKnowledgeSource(
content="Our product, the XPS 13, has a storage capacity of 512GB SSD.",
verbose=True
)
9. text_file_knowledge_source
Loads knowledge from plain text files. Ideal for notes, documentation or FAQs in text format.
- file_path: Path to the text file.
- encoding: File encoding.
- verbose: Enable debug output.
text_source = TextFileKnowledgeSource(
file_path="sample_data.txt",
encoding="utf-8",
verbose=True
)
These knowledge sources allow you to feed structured or unstructured information from multiple formats, making your agents capable of handling multiple domains effectively.
Creating an Agent with StringKnowledgeSource
We will be implementing a CrewAI agent that can answer questions about a product using a string knowledge source.
- First we create a StringKnowledgeSource to provide factual information about the XPS 13.
- Then we define an Agent with a specific role, goal and backstory, linking it to the knowledge source.
- Then we set up a Task that instructs the agent to answer product-related questions.
- Then we create a Crew to manage the agent and its tasks.
- Then we run the Crew with a sample question and print the agent’s response.
from crewai import Agent, Task, Crew
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
import os
os.environ['OPENAI_API_KEY'] = "Your_API_Key"
content = "Our product, the XPS 13, has a storage capacity of 512GB SSD."
string_source = StringKnowledgeSource(content=content)
support_agent = Agent(
role="Technical Support Specialist",
goal="Provide accurate product information and support.",
backstory="You are a good support specialist",
knowledge_sources=[string_source]
)
support_task = Task(
description="Answer the customer's question about product specifications.",
expected_output="A clear answer to the customer's query based on the knowledge source.",
agent=support_agent
)
crew = Crew(
agents=[support_agent],
tasks=[support_task]
)
result = crew.kickoff(inputs={"question": "What is the storage capacity of the XPS 13?"})
print(result)
Output:
"Our product, the XPS 13, has a storage capacity of 512GB SSD."