Memory and Delegation — Making Agents Remember and Collaborate
Why Memory and Delegation Matter
Without memory, each agent conversation starts from scratch. The agent forgets what was discussed, what decisions were made, and what context was established. Memory solves this. Delegation allows agents to ask other agents for help — creating true collaboration.
Why this matters for your career:
- Memory is what separates toy agents from production systems
- Delegation enables complex workflows where specialists collaborate
- Understanding memory types helps you choose the right persistence strategy
- Agent collaboration is the key advantage of multi-agent systems over single-agent
CrewAI Memory Types
CrewAI supports several memory types:
| Memory Type | Storage | Scope | Persistence | Use Case | |-------------|---------|-------|-------------|----------| | Short-term | In-memory | Current crew run | Ephemeral | Recent context within a conversation | | Long-term | Local file or DB | Across crew runs | Persistent | User preferences, learned patterns | | Entity | Local file or DB | Specific entities | Persistent | Facts about people, places, things | | User | Local file or DB | Per user | Persistent | User-specific preferences and history |
Enabling Memory
from crewai import Crew, Process
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
process=Process.sequential,
memory=True, # Enable all memory types
verbose=True
)
Short-Term Memory
Short-term memory keeps recent conversation context within a single crew run. It is automatically enabled when memory=True.
What it remembers:
- Previous messages in the conversation
- Recently completed tasks
- Current task context
- Agent outputs from recent steps
Limitations:
- Cleared when the crew run ends
- Limited by the LLM's context window
Long-Term Memory
Long-term memory persists information across different crew runs using a local SQLite database or custom storage backend.
crew = Crew(
agents=[support_agent],
tasks=[support_task],
memory=True,
long_term_memory=True, # Default when memory=True
verbose=True
)
# First run: agent learns about a user's preference
crew.kickoff(inputs={'query': 'I prefer lakeside campsites with fishing'})
# Second run: agent remembers the preference
crew.kickoff(inputs={'query': 'Recommend a campsite for next weekend'})
# Agent: "You mentioned you prefer lakeside spots with fishing. How about..."
Entity Memory
Entity memory extracts and stores facts about entities (people, places, organizations) from conversations.
crew = Crew(
agents=[booking_agent],
tasks=[booking_task],
memory=True,
entity_memory=True,
verbose=True
)
# Agent extracts and remembers:
# - Entity: "Sunset Ridge Campground"
# - Location: "Taroko National Park"
# - Price: "$45/night"
# - Amenities: "water, toilet, fire pit"
# - Rating: "4.5 stars"
Custom Memory Storage
from crewai.memory.storage import SQLiteStorage
# Long-term memory stored in a SQLite database
crew = Crew(
agents=[agent],
tasks=[task],
memory=True,
long_term_memory=True,
storage=SQLiteStorage(
db_path='./memory/crew_memory.db'
),
verbose=True
)
Delegation
Delegation allows one agent to ask another agent for help. This is the key mechanism for collaboration in CrewAI.
How Delegation Works
Researcher Agent: "I need to research campsites near Taipei."
↓ (asks for help)
Search Specialist Agent: "Searching database for campsites near Taipei..."
↓ (returns results)
Researcher Agent: "Based on the search results, the best options are..."
Enabling Delegation
from crewai import Agent, Task, Crew, Process
# Agents with delegation enabled
researcher = Agent(
role='Camping Researcher',
goal='Find the best campsites based on user requirements.',
backstory='You are a camping expert who knows all the best spots.',
allow_delegation=True, # Can ask other agents for help
verbose=True
)
data_analyst = Agent(
role='Campsite Data Analyst',
goal='Query the database and analyze campsite data.',
backstory='You are a data expert with access to the campsite database.',
allow_delegation=False, # Focused on its own task
verbose=True
)
weather_agent = Agent(
role='Weather Specialist',
goal='Check weather forecasts for camping locations.',
backstory='You are a meteorologist specializing in mountain weather.',
allow_delegation=True,
verbose=True
)
Task with Delegation
research_task = Task(
description='''
Find the best campsites for a family camping trip in July.
Requirements:
- Family with young children (ages 5 and 8)
- Need water and toilet facilities
- Prefer lakeside or riverside
- Within 2 hours drive of Taipei
If you need data from the database, delegate to the Campsite Data Analyst.
If you need weather information, delegate to the Weather Specialist.
Provide 3 recommendations with:
- Campsite name and location
- Distance from Taipei
- Amenities available
- July weather forecast
- Why it's suitable for young children
''',
agent=researcher
)
crew = Crew(
agents=[researcher, data_analyst, weather_agent],
tasks=[research_task],
process=Process.hierarchical, # Use hierarchical for delegation
manager_llm='gpt-4o',
verbose=True
)
result = crew.kickoff()
print(result)
Delegation Patterns
Sequential Delegation
Agent A → Agent B → Agent C (each passes work to the next)
Process.sequential
# Step 1: Researcher finds campsites
# Step 2: Weather specialist checks forecast
# Step 3: Writer creates the final report
Hierarchical Delegation
A manager agent delegates tasks to specialist agents and synthesizes results.
Process.hierarchical
# Manager (GPT-4o) coordinates:
# → Researcher: find campsites
# → Weather specialist: check weather
# → Safety expert: evaluate risks
# Manager combines all inputs into final recommendation
When to Use Each
| Process | Best For | Pros | Cons | |---------|----------|------|------| | Sequential | Clear step-by-step workflows | Simple, predictable, easy to debug | Slower (serial) | | Hierarchical | Complex tasks needing coordination | Flexible, parallel delegation | Requires good manager LLM |
Best Practices
| Practice | Reason |
|----------|--------|
| Enable memory for continuity | Agents remember context across interactions |
| Use entity memory for facts | Extract and store structured information |
| Enable delegation for complex tasks | Agents can ask specialists for help |
| Use hierarchical process for coordination | Manager handles task distribution |
| Use sequential for linear workflows | Clear step-by-step execution |
| Store long-term memory in SQLite | Persistent across sessions |
| Limit delegation to agents that need it | Not every agent needs to delegate |
| Set allow_delegation=True carefully | Only agents that interact with others need it |
Summary
Memory and delegation are what make CrewAI agents truly collaborative and intelligent. Short-term memory keeps conversation context, long-term memory persists across sessions, entity memory extracts facts, and delegation enables agents to work together.
Key takeaways:
- Short-term memory: current conversation context (ephemeral)
- Long-term memory: persists across crew runs (SQLite database)
- Entity memory: stores facts about people, places, things
- User memory: per-user preferences and history
- Enabling delegation allows agents to ask other agents for help
- Sequential process: agents execute tasks one after another
- Hierarchical process: a manager agent coordinates specialists
- Use
allow_delegation=Trueon agents that coordinate others - Use
Process.hierarchicalwith a good manager LLM (GPT-4o) - Store memory in SQLite for persistence across restarts
What's Next: ECPay Payment Agent
The next chapter builds a real-world ECPay payment integration agent — generating payment URLs, verifying webhooks, and handling refunds.