CrewAI + HatiData: Cross-Agent Memory
Build a CrewAI crew where researcher and writer agents share a persistent memory namespace for seamless knowledge transfer across agent roles.
What You'll Build
A CrewAI crew where researcher and writer agents share a persistent memory namespace for seamless knowledge transfer.
Prerequisites
$pip install crewai hatidata-agent
$hati init
$OpenAI API key
Architecture
┌──────────────┐ ┌──────────────────┐ ┌──────────────┐ │ Researcher │──▶│ HatiData │───▶│ Engine │ │ Agent │ │ Shared Namespace │ │ + Vectors │ └──────────────┘ └──────────────────┘ └──────────────┘ ┌──────────────┐ ▲ │ Writer │──────────┘ │ Agent │ reads researcher's findings └──────────────┘
Key Concepts
- ●Shared namespaces: multiple agents write to and read from the same namespace, enabling automatic knowledge sharing
- ●Agent attribution: every memory records the agent_id that created it, so you can trace the provenance of any piece of knowledge
- ●Semantic discovery: the writer does not need to know what the researcher stored — semantic search surfaces relevant findings automatically
- ●Accumulative knowledge: the shared namespace grows over time, so long-running projects build up an ever-richer knowledge base
Step-by-Step Implementation
Install Dependencies
Install CrewAI and the HatiData agent SDK.
pip install crewai hatidata-agent
hati initHatiData initialized successfully.
Proxy running on localhost:5439Note: Shared memory namespaces let agents discover each other's findings via semantic search.
Set Up Shared Namespace
Create a shared memory namespace that both agents will use for knowledge exchange.
from hatidata_agent import HatiDataAgent
# Both agents connect to the same namespace
researcher = HatiDataAgent(host="localhost", port=5439, agent_id="researcher-agent")
writer = HatiDataAgent(host="localhost", port=5439, agent_id="writer-agent")
# Verify namespace is empty
count = researcher.query("""
SELECT COUNT(*) AS cnt
FROM _hatidata_memory.memories
WHERE namespace = 'project-alpha'
""")
print(f"Shared namespace 'project-alpha': {count[0]['cnt']} memories")Shared namespace 'project-alpha': 0 memoriesNote: Namespaces are created implicitly on first write. All agents with access can read and write.
Researcher Agent Stores Knowledge
The researcher agent stores findings in the shared namespace.
# Researcher stores multiple findings
findings = [
"Global AI infrastructure spending will reach $200B by 2027 (IDC forecast)",
"Edge computing reduces latency by 75% for real-time AI inference workloads",
"Kubernetes is the dominant orchestration platform for AI/ML workloads at 78% adoption",
"Vector databases are growing 340% YoY as RAG architectures become mainstream",
]
for finding in findings:
researcher.execute(f"""
SELECT store_memory(
'{finding}',
'project-alpha'
)
""")
print(f"Researcher stored {len(findings)} findings in 'project-alpha'")
# Verify storage
count = researcher.query("""
SELECT COUNT(*) AS cnt
FROM _hatidata_memory.memories
WHERE namespace = 'project-alpha'
""")
print(f"Total memories in namespace: {count[0]['cnt']}")Researcher stored 4 findings in 'project-alpha'
Total memories in namespace: 4Writer Agent Retrieves Knowledge
The writer agent uses semantic search to find relevant findings from the researcher.
# Writer searches for relevant findings to write about
results = writer.query("""
SELECT agent_id, content, created_at
FROM _hatidata_memory.memories
WHERE namespace = 'project-alpha'
AND semantic_match(embedding, 'AI infrastructure market trends and spending', 0.65)
ORDER BY semantic_rank(embedding, 'AI infrastructure market trends and spending') DESC
LIMIT 3
""")
print(f"Writer found {len(results)} relevant findings:")
for r in results:
print(f" [{r['agent_id']}] {r['content'][:80]}...")
# Writer stores a synthesis
writer.execute("""
SELECT store_memory(
'Article draft: AI infrastructure spending is accelerating, driven by edge computing and vector database adoption',
'project-alpha'
)
""")
print("\nWriter stored synthesis back to shared namespace")Writer found 2 relevant findings:
[researcher-agent] Global AI infrastructure spending will reach $200B by 2027 (IDC forecast)...
[researcher-agent] Vector databases are growing 340% YoY as RAG architectures become mainstr...
Writer stored synthesis back to shared namespaceNote: The writer discovers findings by semantic meaning. It does not need to know which specific memories the researcher stored.
Run the Full CrewAI Crew
Define a complete CrewAI crew with researcher and writer agents sharing HatiData memory.
from crewai import Agent, Task, Crew
research_agent = Agent(
role="Senior Researcher",
goal="Find and store key market insights about AI infrastructure",
backstory="You are an expert technology researcher. Store all findings in the project-alpha namespace.",
)
writer_agent = Agent(
role="Technical Writer",
goal="Synthesize research findings into a compelling article",
backstory="You write technology articles. Search project-alpha namespace for research findings.",
)
research_task = Task(
description="Research AI infrastructure trends and store 5 key findings in HatiData shared memory",
agent=research_agent,
)
writing_task = Task(
description="Query shared memory for research findings and write a 500-word article",
agent=writer_agent,
)
crew = Crew(
agents=[research_agent, writer_agent],
tasks=[research_task, writing_task],
verbose=True,
)
result = crew.kickoff()
print(f"Article length: {len(result.raw)} characters")[Researcher] Storing finding: AI infrastructure spending forecast...
[Researcher] Storing finding: Edge computing latency improvements...
[Writer] Retrieving research findings from shared memory...
[Writer] Found 5 relevant findings
[Writer] Drafting article...
Article length: 2,847 characters