Implementation:CrewAIInc CrewAI Knowledge Attachment Config
Appearance
Metadata
| Field | Value |
|---|---|
| Implementation Name | Knowledge Attachment Config |
| Workflow | Knowledge_RAG_Pipeline |
| Category | Agent Configuration |
| Repository | crewAIInc/crewAI |
| Implements | Principle:CrewAIInc_CrewAI_Knowledge_Attachment |
Overview
Concrete configuration fields on Crew and Agent classes for attaching knowledge sources with automatic ingestion provided by the CrewAI framework. These fields enable declarative knowledge configuration that triggers automatic Knowledge object creation and source ingestion during initialization.
Source References
| Class | File | Lines |
|---|---|---|
| Crew | src/crewai/crew.py | L299-305 |
| BaseAgent | src/crewai/agents/agent_builder/base_agent.py | L172-175 |
Fields
Crew Knowledge Fields
class Crew(BaseModel):
# ... other fields ...
knowledge_sources: list[BaseKnowledgeSource] | None = None
embedder: EmbedderConfig | None = None
Agent Knowledge Fields
class BaseAgent(BaseModel):
# ... other fields ...
knowledge_sources: list[BaseKnowledgeSource] | None = None
embedder: EmbedderConfig | None = None
Import
from crewai import Crew, Agent
I/O Contract
| Direction | Type | Description |
|---|---|---|
| Input | None | List of knowledge source instances to attach (optional) |
| Input | None | Embedding provider configuration for knowledge (optional) |
| Output | Auto-created Knowledge object |
Knowledge object with ingested sources, stored internally on the Crew or Agent |
Initialization Behavior
Crew-Level Attachment
When knowledge_sources is set on a Crew:
- During Crew initialization (
__init__or model validator), the framework checks ifknowledge_sourcesis non-empty - A
Knowledgeobject is created with:collection_namederived from the crew identifiersourcesset to the providedknowledge_sourcesembedderset to the crew'sembedderfield
knowledge.add_sources()is called to ingest all sources- The Knowledge object is stored internally for use during task execution
Agent-Level Attachment
When knowledge_sources is set on an Agent:
- During Agent initialization, the framework creates a separate
Knowledgeobject - The collection name is derived from the agent's identifier (ensuring isolation from crew-level knowledge)
- Sources are ingested into the agent-specific collection
- During task execution, both crew-level and agent-level knowledge are queried
Code Examples
Crew with Knowledge Sources
from crewai import Crew, Agent, Task
from crewai.knowledge.source import PDFKnowledgeSource
pdf_source = PDFKnowledgeSource(
file_paths=["docs/product_guide.pdf", "docs/faq.pdf"],
chunk_size=4000,
chunk_overlap=200,
)
agent = Agent(
role="Support Agent",
goal="Answer customer questions accurately",
backstory="Experienced customer support specialist",
)
task = Task(
description="Answer: How do I reset my password?",
expected_output="Step-by-step password reset instructions",
agent=agent,
)
crew = Crew(
agents=[agent],
tasks=[task],
knowledge_sources=[pdf_source], # Crew-level attachment
embedder={
"provider": "openai",
"config": {"model": "text-embedding-3-small"},
},
)
# Knowledge is automatically ingested during Crew construction
# During task execution, relevant chunks from product_guide.pdf and faq.pdf
# are retrieved and added to the agent's prompt
result = crew.kickoff()
Agent with Specialized Knowledge
from crewai import Agent
from crewai.knowledge.source import TextFileKnowledgeSource
legal_docs = TextFileKnowledgeSource(
file_paths=["legal/terms_of_service.txt", "legal/privacy_policy.txt"],
)
legal_agent = Agent(
role="Legal Reviewer",
goal="Review content for legal compliance",
backstory="Corporate legal expert",
knowledge_sources=[legal_docs], # Agent-level attachment
embedder={
"provider": "openai",
"config": {"model": "text-embedding-3-small"},
},
)
Combined Crew and Agent Knowledge
from crewai import Crew, Agent, Task
from crewai.knowledge.source import PDFKnowledgeSource, CSVKnowledgeSource
# Shared knowledge for all agents
shared_source = PDFKnowledgeSource(file_paths=["docs/company_handbook.pdf"])
# Agent-specific knowledge
sales_data = CSVKnowledgeSource(file_paths=["data/sales_report.csv"])
sales_agent = Agent(
role="Sales Analyst",
goal="Analyze sales performance",
backstory="Data-driven sales expert",
knowledge_sources=[sales_data], # Agent-specific
)
general_agent = Agent(
role="General Assistant",
goal="Help with general questions",
backstory="Helpful assistant",
)
crew = Crew(
agents=[sales_agent, general_agent],
tasks=[...],
knowledge_sources=[shared_source], # Crew-wide
embedder={"provider": "openai", "config": {"model": "text-embedding-3-small"}},
)
# sales_agent has access to both company_handbook.pdf AND sales_report.csv
# general_agent has access to only company_handbook.pdf
Related Pages
- Principle:CrewAIInc_CrewAI_Knowledge_Attachment -- The principle this implements
- Implementation:CrewAIInc_CrewAI_Knowledge_Constructor -- Knowledge class created automatically by attachment
- Implementation:CrewAIInc_CrewAI_Knowledge_Source_Classes -- Source classes passed as knowledge_sources
- Implementation:CrewAIInc_CrewAI_Embedder_Config -- Embedder configuration type used by the embedder field
- Implementation:CrewAIInc_CrewAI_Knowledge_Storage_Search -- Storage backend used for retrieval during execution
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment