Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:CrewAIInc CrewAI Knowledge Attachment Config

From Leeroopedia

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:

  1. During Crew initialization (__init__ or model validator), the framework checks if knowledge_sources is non-empty
  2. A Knowledge object is created with:
    • collection_name derived from the crew identifier
    • sources set to the provided knowledge_sources
    • embedder set to the crew's embedder field
  3. knowledge.add_sources() is called to ingest all sources
  4. The Knowledge object is stored internally for use during task execution

Agent-Level Attachment

When knowledge_sources is set on an Agent:

  1. During Agent initialization, the framework creates a separate Knowledge object
  2. The collection name is derived from the agent's identifier (ensuring isolation from crew-level knowledge)
  3. Sources are ingested into the agent-specific collection
  4. 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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment