Build a Strands Agent with Context Graph Memory
This tutorial walks you through building your first AI agent with persistent memory using Neo4j Agent Memory and the AWS Strands Agents SDK.
What You’ll Build
A conversational AI agent that:
-
Remembers conversations across sessions
-
Extracts and stores entities (people, organizations, projects)
-
Builds a knowledge graph of relationships
-
Uses Amazon Bedrock for LLM and embeddings
Prerequisites
-
Python 3.10+
-
AWS account with Bedrock access
-
Neo4j Aura account (or local Neo4j instance)
-
Basic Python knowledge
Step 1: Set Up Your Environment
Create a new project directory and virtual environment:
mkdir strands-memory-agent
cd strands-memory-agent
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
Install the required packages:
pip install neo4j-agent-memory[aws,strands] python-dotenv
This installs:
-
neo4j-agent-memory- The core memory library -
boto3- AWS SDK for Bedrock access -
strands-agents- AWS Strands Agents SDK
Step 2: Configure Credentials
Create a .env file with your credentials:
# Neo4j Configuration
NEO4J_URI=neo4j+s://xxxx.databases.neo4j.io
NEO4J_USER=neo4j
NEO4J_PASSWORD=your-password
# AWS Configuration (uses default credentials if not set)
AWS_REGION=us-east-1
|
For Neo4j Aura:
|
Step 3: Create the Agent
Create a file called agent.py:
"""Strands Agent with Neo4j Context Graph memory."""
import os
from dotenv import load_dotenv
from strands import Agent
from neo4j_agent_memory.integrations.strands import context_graph_tools
# Load environment variables
load_dotenv()
# Create Context Graph tools for the agent
tools = context_graph_tools(
neo4j_uri=os.environ["NEO4J_URI"],
neo4j_user=os.environ.get("NEO4J_USER", "neo4j"),
neo4j_password=os.environ["NEO4J_PASSWORD"],
embedding_provider="bedrock",
aws_region=os.environ.get("AWS_REGION", "us-east-1"),
)
# Create the agent with memory tools
agent = Agent(
model="anthropic.claude-sonnet-4-20250514-v1:0",
tools=tools,
system_prompt="""You are a helpful assistant with persistent memory.
You have access to a knowledge graph that stores:
- Conversation history
- Entities (people, organizations, projects)
- Relationships between entities
- User preferences
Use your memory tools to:
1. Search for relevant context before answering questions
2. Store important facts the user shares
3. Remember user preferences
4. Track relationships between entities
Be conversational and reference memories when relevant.""",
)
def main():
"""Run an interactive chat session."""
print("Neo4j Memory Agent")
print("=" * 40)
print("Chat with an AI that remembers!")
print("Type 'quit' to exit.\n")
while True:
user_input = input("You: ").strip()
if user_input.lower() in ("quit", "exit", "q"):
print("Goodbye!")
break
if not user_input:
continue
# Get response from agent
response = agent(user_input)
print(f"\nAssistant: {response}\n")
if __name__ == "__main__":
main()
Step 4: Run the Agent
python agent.py
Try these example conversations:
You: Hi, I'm Alex and I work at Acme Corp as a software engineer. Assistant: Nice to meet you, Alex! I've stored that you work at Acme Corp as a software engineer. How can I help you today? You: What company do I work for? Assistant: Based on my memory, you work at Acme Corp as a software engineer!
Step 5: Explore the Knowledge Graph
Open Neo4j Browser or Bloom to see the entities and relationships:
// See all entities
MATCH (e:Entity) RETURN e LIMIT 25
// See relationships
MATCH (e1:Entity)-[r]->(e2:Entity) RETURN e1, r, e2 LIMIT 25
// Find Person entities
MATCH (p:Entity:Person) RETURN p.name, p.created_at
Available Tools
The agent has access to these Context Graph tools:
| Tool | Description |
|---|---|
|
Search for relevant memories and entities using semantic search |
|
Explore relationships around an entity with configurable depth |
|
Store important information with automatic entity extraction |
|
Retrieve user preferences by category |
Understanding the Context Graph
As you chat, the agent builds a context graph in Neo4j:
-
Messages are stored with embeddings for semantic search
-
Entities (Person, Organization, Project) are extracted automatically
-
Relationships connect entities to messages and to each other
-
Preferences capture user likes, dislikes, and settings
Next Steps
Now that you’ve built your first Strands agent:
-
Configure Bedrock Embeddings - Optimize embedding model selection
-
Advanced Strands Integration - Custom tools and configuration
-
Use Hybrid Memory - Combine short-term and long-term memory
Troubleshooting
Bedrock Access Denied
Ensure your AWS account has model access:
-
Go to AWS Console → Bedrock → Model access
-
Enable Claude and Titan models
-
Wait for access to be granted (may take a few minutes)
Summary
You’ve built an AI agent with persistent memory that:
-
Stores conversations in Neo4j
-
Extracts entities automatically
-
Remembers context across sessions
-
Uses AWS Bedrock for AI capabilities
The context graph grows with each conversation, making the agent increasingly knowledgeable about you and your work.