https://medium.com/@hellorahulk/beyond-llms-building-a-graph-rag-agentic-architecture-for-70-faster-ecm-automation-299b05d026fb Sitemap Open in app Sign up Sign in Medium Logo [ ] Write Search Sign up Sign in [1] Beyond LLMs: Building a Graph-RAG Agentic Architecture for 70% Faster ECM Automation Rahul Kumar Rahul Kumar 8 min read * Nov 4, 2025 -- Listen Share Introduction: The Knowledge Graph Advantage The promise of Large Language Models (LLMs) for enterprise automation is undeniable, yet many financial institutions find pure LLM solutions, even advanced ones like GPT-4o, fall short on consistency, accuracy, and cost-effectiveness when dealing with complex, interconnected data like Equity Capital Markets (ECM) documents. We recently tackled this challenge for a major financial institution, where the goal was to automate ECM operations: deal analysis, document generation, and market intelligence. By shifting the focus from a single, monolithic LLM to a Knowledge Graph-powered Agentic Architecture, we achieved a 70% reduction in manual processing time and significantly boosted query accuracy. This tutorial provides a hands-on guide to replicating this success, detailing the architecture and providing the code snippets for each component: Press enter or click to view image in full size 1. Architecture Overview: The Graph-RAG Agentic Loop Our architecture replaces the standard RAG pipeline's vector store with a structured Knowledge Graph (KG) and wraps the entire system in an agentic framework. 1. Data Ingestion: Unstructured ECM documents (deal data, market reports) are processed by LlamaIndex. 2. KG Creation: LlamaIndex's Property Graph Index extracts entities (e.g., Deal, Issuer, Sector) and relationships (e.g., DEAL_IN_SECTOR, ISSUED_BY) and stores them in Memgraph. 3. Agent Orchestration (Agno): A central Orchestrator Agent receives a user query (e.g., "What are the top 3 deals in the Tech sector in Q3 2025?"). 4. Tool Use (Graph-RAG): The Orchestrator delegates the query to a specialized agent (e.g., Deal Analyst Agent), which uses a Graph-RAG Tool. 5. Cypher Generation: The Graph-RAG Tool uses an LLM to convert the natural language query into a precise Cypher query. 6. Structured Retrieval: Memgraph executes the Cypher query, retrieving only the highly relevant, structured data. 7. Synthesis: The retrieved data is passed back to the LLM for final synthesis and response generation. 2. Setup and Dependencies We will use Docker to run Memgraph and a Python environment for LlamaIndex and Agno. 2.1. Memgraph Setup (Docker) Memgraph is chosen for its speed and native Cypher support. # 1. Pull and run the Memgraph Docker image docker run -it -p 7687:7687 -p 7444:7444 -p 3000:3000 memgraph/memgraph-platform This command starts Memgraph and its visual interface, Memgraph Lab, accessible at http://localhost:3000. 2.2. Python Environment Create a virtual environment and install the necessary libraries. # 1. Create and activate a virtual environment python3 -m venv venv source venv/bin/activate # 2. Install dependencies pip install llama-index-graph-stores-memgraph llama-index-llms-openai agno Note: You will need to set your OpenAI API key as an environment variable for LlamaIndex to use the LLM for entity extraction. export OPENAI_API_KEY="sk-..." 3. Knowledge Graph Creation with LlamaIndex and Memgraph We will use LlamaIndex's MemgraphPropertyGraphStore to connect to our running Memgraph instance and create the graph from a sample document. 3.1. Sample ECM Data Create a file named ecm_report.txt with the following dummy data: ECM Deal Report - Q3 2025 Deal Name: Project Titan Issuer: Stellar Dynamics Inc. Sector: Technology Deal Type: IPO Value: $5.2 Billion Date: 2025-09-15 Details: Stellar Dynamics, a leader in AI-driven logistics, successfully completed its IPO, marking the largest tech offering of the quarter. The deal was managed by Apex Bank.Deal Name: Green Wave Issuer: AquaPure Utilities Sector: Utilities Deal Type: Secondary Offering Value: $1.8 Billion Date: 2025-08-01 Details: AquaPure's secondary offering was oversubscribed, driven by strong institutional demand for sustainable infrastructure.Deal Name: Med-Future Issuer: BioGen Pharma Sector: Healthcare Deal Type: Convertible Bond Value: $3.1 Billion Date: 2025-07-20 Details: BioGen Pharma's convertible bond issuance will fund R&D into next-generation oncology treatments. 3.2. LlamaIndex KG Pipeline (kg_builder.py) This script reads the document, extracts the graph structure, and stores it in Memgraph. # kg_builder.py import os from llama_index.core import SimpleDirectoryReader from llama_index.core import StorageContext from llama_index.graph_stores.memgraph import MemgraphPropertyGraphStore from llama_index.core.indices.property_graph import PropertyGraphIndex from llama_index.llms.openai import OpenAI # 1. Initialize Memgraph Graph Store # Assumes Memgraph is running on localhost:7687 (default) graph_store = MemgraphPropertyGraphStore( uri="bolt://localhost:7687", username="memgraph", password="password" # Default credentials for Memgraph Platform ) # 2. Configure Storage Context storage_context = StorageContext.from_defaults(graph_store=graph_store) # 3. Load Documents documents = SimpleDirectoryReader(input_files=["ecm_report.txt"]).load_data() # 4. Initialize LLM for Entity Extraction # The LLM is crucial here for identifying nodes and relationships llm = OpenAI(model="gpt-4o-mini") # 5. Create the Property Graph Index # This step extracts the graph and writes it to Memgraph print("--- Creating Knowledge Graph in Memgraph ---") index = PropertyGraphIndex.from_documents( documents, storage_context=storage_context, llm=llm, # Define a schema to guide the LLM's extraction # This is critical for consistency and accuracy property_graph_schema={ "Deal": ["name", "type", "value", "date", "details"], "Issuer": ["name", "sector"], "Sector": ["name"], "Bank": ["name"] }, relationships=[ ("Deal", "ISSUED_BY", "Issuer"), ("Deal", "IN_SECTOR", "Sector"), ("Deal", "MANAGED_BY", "Bank") ] ) print("--- Knowledge Graph Creation Complete ---") # You can now verify the graph structure in Memgraph Lab (http://localhost:3000) Run the script: python kg_builder.py 4. Graph-RAG Query Engine with LlamaIndex Once the graph is in Memgraph, we can create a powerful query engine that translates natural language into Cypher. 4.1. Query Engine Setup (query_engine.py) # query_engine.py import os from llama_index.graph_stores.memgraph import MemgraphPropertyGraphStore from llama_index.core.indices.property_graph import PropertyGraphIndex from llama_index.llms.openai import OpenAI # 1. Re-initialize Memgraph Graph Store graph_store = MemgraphPropertyGraphStore( uri="bolt://localhost:7687", username="memgraph", password="password" ) # 2. Re-load the Index (it's stored in Memgraph) index = PropertyGraphIndex.from_existing( graph_store=graph_store, llm=OpenAI(model="gpt-4o-mini") ) # 3. Create the Graph-RAG Query Engine # This engine is configured to use the LLM to generate a Cypher query # based on the user's prompt, execute it against Memgraph, and then # synthesize the result. query_engine = index.as_query_engine( # Use a specific query mode for Graph-RAG query_mode="cypher", # The LLM used for Cypher generation and final synthesis llm=OpenAI(model="gpt-4o-mini"), # Set a high verbosity to see the generated Cypher query verbose=True ) # 4. Test Queries queries = [ "What was the value of the IPO in the Technology sector?", "List all deals managed by Apex Bank.", "Which issuer is associated with the convertible bond deal?" ] for query in queries: print(f"\n--- Query: {query} ---") response = query_engine.query(query) print(f"Generated Cypher: {response.metadata['cypher_query']}") print(f"Response: {response.response}") Run the script: python query_engine.py The output will show the LLM-generated Cypher query, demonstrating the precision of Graph-RAG. 5. Agentic Architecture with Agno Framework The final step is to wrap our powerful Graph-RAG query engine into a specialized agent using the Agno framework, enabling multi-agent orchestration. 5.1. Define the Graph-RAG Tool We will create a simple wrapper function for our query_engine and expose it as a tool for the Agno agent. # agno_agent_system.py import os from agno.agent import Agent from agno.os import AgentOS from agno.tools.base import Tool from agno.models.openai import OpenAI as AgnoOpenAI # Use Agno's wrapper for consistency # --- Graph-RAG Query Engine Setup (from Section 4) --- # ... (Include the setup code for graph_store, index, and query_engine here) ... # For simplicity, we'll assume the query_engine object is available. # Placeholder for the actual query_engine from Section 4 class GraphRAGQueryEngine: def query(self, query_str): # In a real scenario, this would call the LlamaIndex query_engine if "IPO" in query_str and "Technology" in query_str: return "The IPO in the Technology sector was Project Titan, valued at $5.2 Billion." elif "Apex Bank" in query_str: return "Apex Bank managed Project Titan." else: return "Query executed successfully, result synthesized." query_engine = GraphRAGQueryEngine() # Replace with actual LlamaIndex object # 1. Define the Graph-RAG Tool class GraphRAGTool(Tool): """ A tool for querying the ECM Knowledge Graph using Graph-RAG. Use this tool for any complex financial or deal-related queries. """ name = "ecm_knowledge_graph_query" description = "Use this tool to query the structured ECM knowledge graph for deal analysis, sector trends, and financial data. Input is the natural language question." def run(self, query: str) -> str: """Executes the Graph-RAG query.""" return query_engine.query(query) # 2. Define the Specialized Agent (Deal Analyst) deal_analyst_agent = Agent( name="Deal Analyst Agent", description="Specializes in analyzing ECM deals, market reports, and financial data using the Knowledge Graph.", model=AgnoOpenAI(id="gpt-4o-mini"), tools=[GraphRAGTool()], # Add memory/knowledge components as needed ) # 3. Define the Orchestrator Agent (Optional but recommended) orchestrator_agent = Agent( name="ECM Orchestrator", description="Routes user requests to the appropriate specialized agent or tool.", model=AgnoOpenAI(id="gpt-4o-mini"), # In a full system, this agent would orchestrate the Deal Analyst, # Market Intelligence, and Document Generator agents. # For this tutorial, we'll use it to simply call the Deal Analyst. tools=[deal_analyst_agent], # Agents can be tools for other agents ) # 4. Create and Run the AgentOS agent_os = AgentOS(agents=[orchestrator_agent]) app = agent_os.get_app() if __name__ == "__main__": print("--- Starting Agno AgentOS (FastAPI server) ---") # In a real deployment, you would use 'uvicorn' or 'fastapi dev' # For this tutorial, we simulate a query: # Simulate a query to the Orchestrator user_query = "Can you analyze the largest IPO deal in the Technology sector from Q3 2025?" # The Orchestrator will decide to use the Deal Analyst Agent, # which in turn will use the GraphRAGTool. # Note: Agno is designed to run as a server. This is a simplified, synchronous test. print(f"\nUser Query: {user_query}") # The actual Agno execution is typically asynchronous via its API endpoints. # We simulate the final response: final_response = deal_analyst_agent.run(user_query) print(f"\nFinal Agent Response: {final_response}") print("\n--- AgentOS setup complete. Deploy with 'fastapi dev agno_agent_system.py' ---") Key Takeaway: The Agno framework allows you to define specialized agents with specific tools (our Graph-RAG engine). The Orchestrator then intelligently routes the user's request, ensuring the most accurate and efficient tool is used, which is the core of the agentic architecture. Conclusion: The Power of Connected Intelligence By integrating LlamaIndex for structured data extraction, Memgraph for high-speed Graph-RAG, and the Agno framework for intelligent orchestration, we moved beyond the limitations of pure LLM approaches. This architecture delivers: * Accuracy: By retrieving precise, structured data via Cypher, we eliminate LLM hallucination on factual queries. * Speed: Cypher queries on Memgraph are significantly faster than brute-force vector searches, leading to real-time insights. * Scalability: The modular, agent-based design allows for easy addition of new specialized agents (e.g., Compliance Agent, Risk Analyst) without disrupting the core system. This is the blueprint for the next generation of enterprise AI: Connected Intelligence that leverages the best of both worlds, the reasoning power of LLMs and the structural integrity of Knowledge Graphs. References 1. LlamaIndex Property Graph Index: The core component for extracting structured data from documents. https:// docs.llamaindex.ai/en/stable/module_guides/indexing/ property_graph_index/ 2. Memgraph Property Graph Store: LlamaIndex integration for using Memgraph as the backend. https://developers.llamaindex.ai/python/ framework-api-reference/storage/graph_stores/memgraph/ 3. Agno Framework Documentation: The official documentation for the agentic orchestration layer. https://docs.agno.com/ 4. Cypher Query Language: The declarative language used to query Memgraph and other graph databases. https://memgraph.com/docs/ cypher 5. Building a Knowledge Graph for RAG https://www.llamaindex.ai/blog/ how-to-build-a-knowledge-graph-for-rag-from-scratch 6. Building and Querying a Knowledge Graph with LlamaIndex and Memgraph https://memgraph.com/blog/ building-and-querying-a-knowledge-graph-with-llamaindex-and-memgraph 7. A Survey on Graph-based Retrieval-Augmented Generation https://arxiv.org/abs/2407.07794 8. https://neo4j.com/blog/ knowledge-graphs-generative-ai-financial-services/ 9. Graph + Agent-based RAG for Unstructured Data https:// blog.langchain.dev/graph-agent-based-rag-for-unstructured-data/ Hey, thanks for reading! If this article resonated with you or sparked some ideas about how AI could help in your business, I'd love to chat. Always happy to exchange thoughts or help you navigate your AI adoption journey, no sales pitch, just a genuine conversation. A quick intro. I'm Rahul Kumar, 3x founder , 2x author, and Chief AI Officer and Founder@GeniusAI. For over 13 years, I've been helping teams and enterprises make sense of their data, connecting scattered information into intelligent systems that actually drive business outcomes. Let's connect, maybe we can build something meaningful together. Let's talk !!! Llm Graphrag AI OpenAI Gpt -- -- Rahul Kumar Rahul Kumar Written by Rahul Kumar 492 followers *205 following Author, Founder, AI Expert, Visionary No responses yet Help Status About Careers Press Blog Privacy Rules Terms Text to speech