The Query Generation Agent
Part of the Zero to Operational for Amazon Neptune using Agentic AI series. This agent generates the queries to answer the questions described in your use case.
This article is part of the Zero to Operational on Amazon Neptune using Agentic AI series.
Agent Goals
The next agent in our workflow is the Query Generation Agent. Here we provide the agent with a detailed use case description containing a list of questions to answer and a connection to the Neptune cluster containing data, and it generates and tests a query to answer each question.
Our Sample Workflow
In our series, we started with the topic Anti-Money Laundering and Financial Crime Investigation. The Use Case Generator Agent created this use case from that topic, which we did not edit and will use as-is.
After passing this use case and the connection to our Neptune cluster to the query generation agent, we get the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Perfect! Now let me create the final optimized queries:
{
"results": [
{
"question": "Which accounts are participating in circular transaction patterns that could indicate layering activity in money laundering schemes?",
"query": "MATCH (a1:Account)<-[:TRANSFERS_FROM]-(t1:Transaction)-[:TRANSFERS_TO]->(a2:Account)<-[:TRANSFERS_FROM]-(t2:Transaction)-[:TRANSFERS_TO]->(a1) WHERE a1.account_number <> a2.account_number RETURN DISTINCT a1.account_number, a2.account_number, t1.amount, t2.amount, t1.timestamp, t2.timestamp ORDER BY t1.timestamp DESC LIMIT 10"
},
{
"question": "What are the shortest paths connecting a flagged high-risk entity to other accounts that received funds within a specific time window?",
"query": "MATCH (flagged:Entity {is_flagged: true})-[:OWNS_ACCOUNT]->(start_account:Account)<-[:TRANSFERS_FROM]-(t:Transaction)-[:TRANSFERS_TO]->(connected_account:Account) WHERE t.timestamp >= '2024-01-01T00:00:00.000Z' RETURN flagged.name, connected_account.account_number, t.amount, t.timestamp ORDER BY t.timestamp DESC LIMIT 10"
},
{
"question": "Which shell companies or intermediary accounts appear most frequently in transaction paths between known suspicious entities?",
"query": "MATCH (e1:Entity)-[:OWNS_ACCOUNT]->(a1:Account)<-[:TRANSFERS_FROM|TRANSFERS_TO]-(t:Transaction)-[:TRANSFERS_FROM|TRANSFERS_TO]->(a2:Account)<-[:OWNS_ACCOUNT]-(e2:Entity) WHERE e1.is_shell_company = true AND e1 <> e2 RETURN e1.name, count(*) as transaction_frequency ORDER BY transaction_frequency DESC LIMIT 10"
}
]
}
Important notes:
- While the agent will test each query it generates to ensure it does not cause an error, do not confuse that there is no guarantee the query generates results. The nature of the data creation process is that data describing complex scenarios like money laundering may not have generated organically. The Scenario Data Generator handles these situations when the query returns no results.
- The LLM often does not generate exact JSON without commentary despite our wishes. We use a series of regular expressions to try to extract just the JSON for downstream workflow steps. In the future we should investigate using the structured output capabilities of the agent.
Agent Components
Let's look at the key components of the Query Generation Agent. The prompts shown here were tested with Anthropic Claude 3.7 and 4.0.
System Prompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
You are a Cypher query generator. Your response will be JSON containing a list of questions and query pairs. The queries must contain ONLY a valid Cypher statement.
The incoming text will often contain more than just questions. Find the section labeled Questions and create a query for each question desired to be answered.
You should test the queries with the query execution tool to ensure they work.
If the question is asking about a specific entity (e.g., supplier X) and you do not know a specific entity to test the query, you can use the Neptune execution tool to find a valid value (e.g., MATCH (x:Supplier) RETURN ID(x) LIMIT 1)
If you get an error testing the query, try again to generate a valid query
# WORKFLOW
1. Use neptune tool to get schema
2. Generate Cypher query (read-only queries)
3. Test with execution tool
4. Return ONLY the JSON containing the questions and final working Cypher statement
# OUTPUT REQUIREMENTS
- A JSON formatted representation of the questions and queries.
- Each query must be on a single line
You MUST use this exact structure:
{
"results": [
{
"question": "Question 1",
"query": "Query for Question 1"
},
{
"question": "Question 2",
"query": "Query for Question 1"
},
{
"question": "Question N",
"query": "Query for Question N"
}
]
}
# DO NOT
- Add any commentary or Markdown.
FULL EXAMPLE:
<input>
A graph database is ideally suited for supply chain and logistics management as it can model the complex web of relationships between suppliers, manufacturers, distributors, retailers, and customers. The interconnected nature of modern supply chains—with multiple tiers of suppliers, various transportation routes, distribution centers, and retail locations—creates a natural graph structure. With a graph database, companies can visualize and analyze the entire network to optimize routes, identify dependencies, manage inventory across locations, and quickly assess the impact of disruptions. When delays or shortages occur, graph traversals can instantly identify affected downstream components or products, allowing for proactive mitigation strategies rather than reactive responses.
Questions:
- What is the full impact radius if supplier X experiences a production delay, and which customers will ultimately be affected?
- What are the alternative routing options that minimize delivery time if a particular distribution center becomes unavailable?
- Which components in our product catalog have the highest supply chain risk based on supplier concentration and geographical distribution of sourcing?
</input>
<output>
{'results':[
{'question': 'What is the full impact radius if supplier X experiences a production delay, and which customers will ultimately be affected?', 'query':'MATCH ... (full query to answer question)'},
{'question': 'What are the alternative routing options that minimize delivery time if a particular distribution center becomes unavailable?', 'query':'MATCH ... (full query to answer question)'},
{'question': 'Which components in our product catalog have the highest supply chain risk based on supplier concentration and geographical distribution of sourcing?', 'query':'MATCH ... (full query to answer question)'},
]}
</output>Tools
Neptune MCP Server : get_graph_schema and run_opencypher_query tools
User Message Template
1
2
3
<input>
{descriptive_text}
</input>Costs
The example provided here resulted in 76,000 input tokens, 3,400 output tokens, 23,000 cache input read tokens, and 1,300 cache output read tokens. This would be a cost of approximately 0.30 USD.
What's Next
If you are just getting started building a graph solution on Amazon Neptune, create this agent using your favorite agentic AI framework (personally I use Strands Agents SDK ) and LLM and use it create a conceptual model for your graph. In the next article, we will describe a set of agents that will use this schema to populate your Neptune cluster with sample data. Have you implemented ways to improve this or have ideas on how to do so? Share them in the comments or reach out to me directly on LinkedIn .
Disclaimer
These prompts and agent ideas are not supported products in their own right, but educational examples to help Neptune customers use build their applications. Before introducing these prompts and agents into our organization, the methods described should be thoroughly tested, secured, and optimized according to your business's security standards & policies.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article