A Local Copy of the Subgraph for Your Agent: Relate Pull and Push with Neo4j
William Lyon
December 1, 2026
23 min read
Relate by Neo4j is an open source embedded graph database that runs alongside your agent. It speaks Cypher, needs no server, and keeps a whole graph in a single file. That might make it sound like an alternative to Neo4j. In practice the two work together.
Neo4j is the organization-wide graph, the one that many people and applications share. An agent working on a single task rarely needs all of it. With Relate, the agent can pull down just the subgraph that's relevant to its task or workflow, analyze it locally, and then write the new, enriched data back to Neo4j.
Why give the agent its own copy? It needs somewhere to write intermediate results, and anything written straight into the organization's graph is visible to everyone as soon as it commits. A local copy gives the agent room to try things, and it gives us one place, the push, to check what goes back.
relate pull and relate push are the two commands that connect Relate to Neo4j, and in this post we'll use them end to end. We'll pull 0.7% of a 568,315-node Neo4j graph into a local Relate file, answer some supply-chain questions against it, and write the findings into the local copy. Then we'll push to Neo4j two ways. First we'll copy the whole subgraph into a Neo4j database of its own. Then we'll do the write-back: a second task that pushes only its new findings into a database that already holds the graph.
What you'll learn: how relate pull selects a subgraph with a Cypher query and what that query may return, how relate push chooses the properties it merges on, and how to push findings back without overwriting other people's edits. What you need: Relate 1.5.0 and Docker. The demo database needs no account. Some Cypher helps.
All the files are in relate-companies-pull-push-example. The numbers and outputs below are from real runs on Relate 1.5.0, and the queries are trimmed from the repo files to fit the page. I built the example with Claude Code driving the Relate CLI, and there's more on how that went near the end.
What Pull and Push Do
relate pullruns a Cypher query on a Neo4j database and stores the nodes, relationships and paths it returns in a new local store (a single.rlfile).relate pushwrites a local store, or a scoped part of it, into a Neo4j database as batchedMERGEstatements.
Both go through Neo4j's HTTP Query API. That gives us a short checklist for whether this will work with a particular database:
- The Query API port has to be reachable from where Relate runs. A Bolt URI is fine in the config: Relate maps
neo4j+s://host:7687tohttps://host:7473. - Authentication is a username and password.
- A pull runs your query and reads the schema, so a read-only user should be enough. I haven't confirmed that with a locked-down role. A push needs write access.
It's tempting to call this git for graphs. The checkout half of that holds up: we get a private copy that is cheap to snapshot, selected by a query we can review. The rest of git isn't here. There is no history, no merge, no conflict detection and no revert, and a push never deletes a node or a relationship. Each command makes a one-time copy, so a push behaves more like rsync without --delete than like git push.
The Example: Nvidia's Supply Chain
Standing in for the organization-wide graph is companies2, a public demo database hosted by Neo4j. It's a knowledge graph of organizations, people, patents, filings and news articles: 568,315 nodes and 2,530,106 relationships across fifteen labels. The task for the agent is to map Nvidia's supply chain: who supplies Nvidia and its competitors, and which suppliers they share. That only needs the companies around Nvidia, so we can leave the 117,142 patents where they are.
The connection details go in an env file:
# demo.env
NEO4J_URI=neo4j+s://demo.neo4jlabs.com:7687
NEO4J_USERNAME=companies2
NEO4J_PASSWORD=companies2
NEO4J_DATABASE=companies2
Those are public demo credentials, which is the only reason the file is in the repo. Keep real ones out of git.
Step 1: Pull the Subgraph
The thing we write for a pull is a shape query: ordinary Cypher that runs on the Neo4j server and returns the part of the graph we want. This one has three statements.
Here is the first statement. The *0..1 hop keeps Nvidia itself in the set along with its 18 competitors, and I'll call those 19 companies the core.
MATCH (:Organization {name: $seed})
-[:HAS_COMPETITOR*0..1]->(o:Organization)
WITH DISTINCT o
MATCH p = (o)-[:HAS_SUPPLIER|HAS_CUSTOMER
|HAS_PARTNER|HAS_COMPETITOR]->(:Organization)
RETURN p;
The second statement returns every relationship between the companies the first one found, and the third returns the cities and countries where the core companies and their suppliers have primary offices. The whole file is cypher/01-pull.cypher.
One command runs all three and writes a new file:
relate pull --from demo.env --query cypher/01-pull.cypher \
--param seed=Nvidia --database ./nvidia.rl
pulled 3928 new / 3074 reused nodes, 58384 new / 5002 reused rels
(+ 0 constraints, 3 indexes; 17 schema skipped) to ./nvidia.rl
That took about 8 seconds. The file holds 1,832 organizations, 2,017 cities and 79 countries, which is 0.7% of the nodes in the source and 2.3% of its relationships. Each node and relationship is stored once, however many times the three statements return it.
The query is a file in the repo, so it gets reviewed like any other code, and seed=AMD would pull a different neighborhood.
The Rules for a Shape Query
There are four, and each is checked against this version.
- Every item in the final
RETURNhas to be a node, a relationship or a path. We can aggregate and filter on the way there, butRETURN o, o.nameis rejected withrejected item: o.name. - A relationship is only stored if both of its end nodes are returned somewhere in the same pull. A bare
RETURN rinto an empty store pulls 0 nodes and 0 relationships and reports success. The second statement above returns bare relationships on purpose, to avoid sending the same large company nodes thousands of times, and it works because the first statement already pulled every company involved. - Nodes arrive whole, with every label and every property. There is no way to leave a property behind.
- The destination has to be empty. A second pull into the same store fails with
pull writes into an empty database only.
The second rule is the one to watch with an agent, because nothing fails. It's worth having the agent check node and relationship counts after every pull.
Step 2: Analyze Locally
From here on everything runs against nvidia.rl and nothing touches the demo server.
relate query -d ./nvidia.rl --param seed=Nvidia \
< cypher/02-analyze.cypher
That file has five read queries. Let's look at the first one, which asks which outside suppliers the most core companies depend on:
MATCH (:Organization {name: $seed})
-[:HAS_COMPETITOR*0..1]->(c:Organization)
WITH collect(DISTINCT c) AS core
UNWIND core AS c
MATCH (c)-[:HAS_SUPPLIER]->(s:Organization)
WHERE NOT s IN core
WITH s, count(DISTINCT c) AS coreCustomers
RETURN s.name AS chokepoint, coreCustomers
ORDER BY coreCustomers DESC, chokepoint LIMIT 7
chokepoint coreCustomers
Micron Technology 11
Corning 9
Accenture 8
Nokia 8
Oracle 8
SK Hynix 8
Taiwan Semiconductor 8
Micron Technology supplies 11 of the 19. A memory maker, a glass maker and a foundry are what I'd expect to see. Accenture and Oracle are there because HAS_SUPPLIER covers every kind of supplier.
The second query looks for companies that Nvidia doesn't buy from but its suppliers do. The top three are Applied Materials, Nokia and Sun Microsystems, each reached through seven of Nvidia's suppliers. Applied Materials is a believable answer: Taiwan Semiconductor, Micron Technology, Samsung Electronics and Intel all list it as a supplier. Sun Microsystems is not, since it hasn't existed as a company since 2010.
Three caveats about these answers:
- The source caps every company at 100 relationships of a type. 209 organizations have exactly 100 suppliers and none have more, so the numbers for the biggest companies are truncated.
- A company can have several "primary" offices. The 583 companies we have locations for share 3,805 of them.
- A local copy only answers for what was pulled. I ran the second query against the full graph and the top ten rows matched, but a query that walks further than the pull would return a wrong answer with no error.
Inside the engine each of the five queries took under 6 milliseconds. Sent to the demo server, the same queries took 350 to 500 milliseconds each, with a new HTTPS connection every time. That gap is small next to how long a model spends thinking, so I wouldn't pull a subgraph for read speed alone. The stronger reason comes next.
Step 3: Write the Findings into the Local Copy
So far the answers only exist in a terminal. Next we'll write them into the graph as properties and relationships, so later queries can use them. First, a copy of the untouched pull:
relate clone ./nvidia.rl ./nvidia-raw.rl
relate query -d ./nvidia.rl --param seed=Nvidia \
< cypher/03-enrich.cypher
The enrichment script does four things:
- Tags the 19 core companies with
ecosystemandecosystemRole(seedorcompetitor). - Gives 681 outside suppliers a
coreCustomerCount, and setsisChokepointwhen that count is 6 or more. The threshold is arbitrary; 20 suppliers pass it. - Adds 115
SHARES_SUPPLIERS_WITHrelationships between core companies. - Adds 43
INDIRECTLY_DEPENDS_ONrelationships from Nvidia to companies that at least four of its suppliers buy from.
The last of those looks like this:
MATCH (n:Organization {name: $seed})
-[:HAS_SUPPLIER]->(t1:Organization)
-[:HAS_SUPPLIER]->(t2:Organization)
WHERE t2 <> n AND NOT EXISTS { (n)-[:HAS_SUPPLIER]->(t2) }
WITH n, t2, collect(DISTINCT t1.name) AS via
WHERE size(via) >= 4
MERGE (n)-[r:INDIRECTLY_DEPENDS_ON]->(t2)
SET r.viaCount = size(via), r.via = via,
r.derivedBy = 'relate-example', r.derivedOn = date()
Two things in that script are there because of what comes later.
Every relationship the script creates carries derivedBy and derivedOn. Once these land in a database other people use, somebody will want to know which facts came from the source and which ones an agent inferred, and there is no undo for a push. With the marker, removing them is one query.
The node properties are the weak part of this design. ecosystemRole isn't scoped to a seed, so pushing a second run for seed=AMD into the same database would overwrite the first. Findings modeled as new relationships are easier to mark, easier to remove and, as we'll see, safer to push.
None of this exists anywhere except nvidia.rl yet. If the agent had got it wrong, I'd go back to the clone.
Before You Push: Constraints on Both Sides
Constraints matter twice, and both times caught us out.
In the local store, they are the merge keys. Push reads the store's uniqueness constraints to decide which property to MERGE each label on. Look at the pull output above: 0 constraints. The demo database has a uniqueness constraint on Organization.uri, but it defaults to Cypher 5, where SHOW CONSTRAINTS reports the type as UNIQUENESS, and Relate skips that with a warning. Under Cypher 25 the same constraint is reported as NODE_PROPERTY_UNIQUENESS, which Relate does copy. So for a database that defaults to Cypher 5, expect to recreate the constraints yourself. The enrichment script starts with them:
CREATE CONSTRAINT organization_uri IF NOT EXISTS
FOR (o:Organization) REQUIRE o.uri IS UNIQUE;
CREATE CONSTRAINT city_id IF NOT EXISTS
FOR (c:City) REQUIRE c.id IS UNIQUE;
CREATE CONSTRAINT country_uri IF NOT EXISTS
FOR (c:Country) REQUIRE c.uri IS UNIQUE;
A dry run of the untouched clone, which has no constraints, reports every label as (unkeyed) and skips all 62,312 rows.
On the target, they are the speed. Push doesn't create constraints or indexes on the target, and without an index every MERGE scans the label. The same push took 108 seconds into an empty Neo4j with no constraints and under 6 seconds with them. That's with 1,832 organizations, and the gap grows with the size of the target.
Option A: Copy the Subgraph into a New Database
The simplest way to finish is to put the whole subgraph, findings included, into a Neo4j database of its own. It's also how you'd carve a piece of the organization's graph out for a team or an application that only needs that piece. The repo has a compose file for a Neo4j container bound to localhost, and a script with the three constraints:
docker compose up -d --wait
docker exec -i companies-local \
cypher-shell -u neo4j -p relate-demo-pw \
< cypher/00-target-schema.cypher
Before sending anything, let's ask for the plan. I've left out the count columns, five relationship rows and all but one statement, and wrapped that one to fit:
relate push --to local.env --database ./nvidia.rl --dry-run
entity total merge key policy
City 2017 id add
Country 79 uri add
Organization 1832 uri add
(Organization)-[:HAS_PARTNER]->(Organization) 20553 add
(Organization)-[:HAS_SUPPLIER]->(Organization) 8932 add
(Organization)-[:IN_CITY]->(City) 3805 add
...
Organization: UNWIND $rows AS row
MERGE (n:`Organization` {`uri`: row.key.`uri`})
SET n += row.props
...
dry run: 11 entities, 62470 rows planned; nothing was sent
The dry run is a plan. It lists what will be written, how many rows, which property each label is merged on, and the shape of every statement. It doesn't contact the target, so it can't say what already exists there or which values would be overwritten. That's enough for an empty database, and a real limitation for Option B.
relate push --to local.env --database ./nvidia.rl
pushed 62470 rows in 20 statements (27197270 bytes) in 5832 ms
A second run against the unchanged target reported 0 created and 62,470 updated, so repeating a push doesn't duplicate anything.
I didn't plan to test a failure, but I got one. I first ran this on Relate 1.3.0, where a zoned DATETIME property made one batch fail with HTTP 400. The batches before it stayed written: 52,562 of the 58,542 relationships were in Neo4j and the rest were not. So a push is not one transaction. Relate 1.5.0 pushes that property correctly.
Property types come through. A quick check in Neo4j:
MATCH (n:Organization {name: 'Nvidia'})
RETURN n.foundingDate AS founded,
valueType(n.foundingDate) AS type
founded type
1993-04-05 "DATE NOT NULL"
I also pushed one property each of DATE, ZONED DATETIME, LOCAL DATETIME, ZONED TIME, DURATION and POINT from a test store, and each arrived as the same type.
Option B: Write the Findings Back
This is the write-back from the introduction: the organization's graph stays where it is, and the agent sends back what it learned. I can't show that on the public demo server, which isn't mine to write to. So the database from Option A stands in for one that other people use. It's a single-user container on localhost holding only what the first push wrote, so this section shows the mechanics and is no test against a busy production graph.
We'll give it a second task: how exposed is each core company to each country? That needs less of the graph than before:
MATCH p = (c:Organization)-[:HAS_SUPPLIER]->(:Organization)
WHERE c.ecosystemRole IS NOT NULL
RETURN p;
MATCH (c:Organization)-[:HAS_SUPPLIER]->(s:Organization)
WHERE c.ecosystemRole IS NOT NULL
WITH DISTINCT s
MATCH p = (s)-[ic:IN_CITY]->(:City)-[:IN_COUNTRY]->(:Country)
WHERE ic.isPrimary
RETURN p
ecosystemRole is a property the first run wrote, so this pull only works because of the earlier enrichment.
relate pull --from local.env --query cypher/04-pull-exposure.cypher \
--database ./exposure.rl
pulled 2792 new / 2158 reused nodes, 7031 new / 1101 reused rels
(+ 3 constraints, 0 indexes; 0 schema skipped) to ./exposure.rl
This time the three constraints came down with the data, because my Neo4j 2026.10 container reports them the Cypher 25 way.
To see what a push does to data that changed while we were away, we need a teammate, and a short script plays one. Between the pull and the push it edits Nvidia on the target, adding a riskOwner property that the local copy has never seen and changing nbEmployees from 36,000 to 42,000:
docker exec -i companies-local \
cypher-shell -u neo4j -p relate-demo-pw \
< cypher/06-simulate-teammate-edit.cypher
The enrichment adds a SOURCES_FROM relationship from each core company to each country where its suppliers have a primary office, and a chokepointSuppliers count on Country nodes:
relate query -d ./exposure.rl < cypher/05-enrich-exposure.cypher
Now the part that needs care. A push scoped to --types SOURCES_FROM sends those relationships and both end nodes of each, and it sends the end nodes whole. With the default policy every property on those nodes is overwritten with the value we pulled. That would put Nvidia's nbEmployees back to 36,000 without any warning, and the dry run wouldn't show it.
We want the Country nodes updated, because they carry the new score, and the Organization nodes left alone, because we changed nothing on them. Update policies can be set per label:
relate push --to local.env --database ./exposure.rl \
--types SOURCES_FROM --nodes-update Organization=skip
entity total created updated skipped
Country 79 0 79 0
Organization 18 0 0 18
(Organization)-[:SOURCES_FROM]->(Country) 548 548 0 0
pushed 627 rows in 2 statements (298576 bytes) in 147 ms
That created 548 relationships and updated 79 Country nodes, 17 of which gained a score. The 18 Organization nodes were skipped, and Nvidia still has riskOwner and 42,000 employees. (It's 18 and not 19 because VIA Technologies has no suppliers in this data.) The other 3,831 nodes in the database were never sent.
The result is there for the next person or the next agent:
MATCH (c:Organization)-[e:SOURCES_FROM]->(:Country {name: 'Taiwan'})
RETURN c.name AS company, e.suppliers AS suppliers, e.share AS share
ORDER BY e.share DESC, company LIMIT 4
company suppliers share
"MediaTek" 5 0.313
"Nvidia" 10 0.103
"Dell" 8 0.08
"Intel" 8 0.08
share is the fraction of a company's suppliers with a primary office in that country, and the fractions overlap because a supplier can count toward several countries. Dell and Intel both come out at exactly 8 of 100, which is the cap showing through.
How Push Decides What to Write
Merge Keys
Push takes its merge keys from the uniqueness and key constraints in the local store, and a composite constraint gives a composite key. A node is merged on its key, and a relationship is merged between two nodes matched by theirs:
UNWIND $rows AS row
MERGE (n:`Organization` {`uri`: row.key.`uri`})
SET n += row.props
UNWIND $rows AS row
MATCH (a:`Organization` {`uri`: row.start.`uri`})
MATCH (b:`Organization` {`uri`: row.end.`uri`})
MERGE (a)-[r:`HAS_SUPPLIER`]->(b)
SET r += row.props
Rows travel as parameters, 5,000 to a batch. Some details:
- A label with no constraint is reported as
(unkeyed)and skipped. Supply a key with--merge-key Label:prop, or accept duplicates with--unkeyed-labels create. - If a node is missing its key property, the push stops while planning and sends nothing.
- With no relationship key there is one relationship of a type in each direction between two nodes, so parallel relationships of the same type collapse into one.
--rel-key TYPE:propgives a type its own key. - A node with several labels is merged on one of them. In a test, a
Person:Employeenode merged on theEmployeekey, so check the dry run if you have multi-label nodes. - Pull doesn't keep Neo4j's element ids. A later push finds the same node again only for labels that have a unique property.
Scopes
A push takes at most one scope flag, and no flag means the whole store.
| Scope | What is in play |
|---|---|
--types SOURCES_FROM | those relationships, plus both end nodes of each |
--labels Organization,Country | those nodes, plus the relationships between them |
--query '...' | whatever a Cypher query on the local store returns |
| none | everything |
--query follows the shape-query rules from Step 1. A relationship without both end nodes in scope is reported as (dangling) and skipped.
Update Policies
--nodes-update decides what happens to a node that already exists on the target. Here is the Option B push under four settings, with the simulated teammate edit in place before each run:
--nodes-update | riskOwner | nbEmployees | new Country scores |
|---|---|---|---|
add (the default) | kept | overwritten with 36,000 | sent |
replace | removed | overwritten with 36,000 | sent |
skip | kept | kept at 42,000 | not sent |
Organization=skip | kept | kept at 42,000 | sent |
add runs SET n += row.props and replace runs SET n = row.props, which is why replace also removes properties that exist only on the target. skip writes only nodes that are missing.
There is a matching --rels-update for relationships, and an --updated-at flag that skips a row when the target's copy has a newer timestamp. That flag only protects rows with a timestamp on both sides. This dataset has none, and I haven't tested it.
Why Not Just...
...give the agent a read-only user? For pure analysis, do that. It's simpler and the data is never stale. A local copy is worth having when the agent needs to write intermediate results, or when we don't want its queries running on the shared server.
...restore a dev or staging database? That needs a server per agent and gives each one the whole graph. A pull gives each agent one small file. Getting findings back into the shared database is the same problem either way.
...use APOC to export the subgraph? APOC can export a query's results as a Cypher script, which covers much of what push does. What it doesn't provide is somewhere to query and change the subgraph without running another Neo4j.
Limitations
- Each command makes a copy. There is no history, no diff against the target, no conflict detection and no undo. Removing a pushed finding is hand-written Cypher, which is what
derivedByis for. - Whole nodes travel in both directions. A push can't send one property.
- A push is not one transaction. If a batch fails, the batches before it stay.
- The pulled file is an unprotected copy of whatever the pulling user could read. Pull with a user that can't read what shouldn't leave, and delete the file when the task is done.
- Relate keeps the whole graph in memory and allows one writer per store. I tested 3,928 nodes and nothing bigger.
- I didn't test two agents pushing at once. On nodes they both touch, the last push wins, property by property.
Letting an Agent Do This
I built this example with Claude Code driving the Relate CLI through Relate's agent skill, which relate skill install sets up. A few things from that session are worth knowing before you hand this workflow to an agent:
- It read the schema first (labels, relationship types and counts) through Neo4j's Query API, and chose Nvidia after comparing a handful of well-known companies.
- Its first "Nvidia and its competitors" query matched
HAS_COMPETITORin both directions and came back with 155 companies, among them Boeing, Sweetgreen and Manchester United. Following the relationship outward only gives 18. - Its first pull was rejected by the server with a Cypher aggregation error, and it rewrote the query.
- It had both env files and ran every command itself, pushes included. That was fine for a throwaway container on my machine.
For an organization's graph I'd split the work up:
- The agent pulls with a read-only credential and never sees a write credential.
- The agent's deliverable is the enriched store and the output of
relate push --dry-run. - A person or a CI job runs the real push, with the scope and the per-label policy written down in the same pull request as the shape query.
- Findings go in as new relationships marked with
derivedBy, withskipon every label the agent had no reason to change. - The agent asserts counts after each pull and push, because several of the failures above report success: a bare
RETURN rthat pulls nothing, a label skipped for having no key.
What's Next?
There's a list of things I haven't tried: --updated-at, two agents pushing at the same time, a pull an order of magnitude bigger than this one, and Aura as either end. If you try any of them, or run this against your own graph, I'd like to hear how it goes. If I get to them first, the newsletter below is where you'll hear about it.
Watch the Demo
I walked through this demo live on Neo4j's Going Meta livestream. The recording below starts where the Relate demo begins:
Resources
- relate-companies-pull-push-example on GitHub - the queries, env files and compose file from this post
- Relate, the Relate docs and relatedb/relate on GitHub
- Where relate fits
- A Graph Database in Your Pocket - my earlier post on Relate, extensions and the Swift client
- Neo4j Query API - the HTTP interface that pull and push use
- The demo on the Going Meta livestream - the recording, starting at the Relate segment
The companies2 dataset is a public demo graph hosted by Neo4j. Company relationships in it come from an automated knowledge graph and are not a statement of fact about any company's suppliers, customers or competitors.
Stay Updated
Get notified about new posts and videos