Overview

15 Building a QA agent with LangGraph

This chapter presents a practical, expert‑emulated question answering system over knowledge graphs that pairs large language models with LangGraph for orchestration and Streamlit for an interactive front end. The approach mirrors how human experts work: understanding schema context, planning steps, and constructing queries, while remaining observable at every stage. Users interact through a chat-like interface and receive real-time feedback as the pipeline progresses, with results rendered in the most suitable form—graphs, maps, tables—and complemented by concise, context-aware summaries.

The solution is structured as a modular, state-driven pipeline in LangGraph, where each node is a specialized agent that reads and writes to a shared state. A Configuration Provider centralizes prompts, examples, and domain notes, while a Schema Provider converts Neo4j’s technical schema into an LLM-friendly conceptual view via filtering and enrichment. The agents implement the end-to-end flow: intent detection, schema extraction, text-to-Cypher generation (enriched by annotations and the user’s current selection), query execution with robust error handling, dynamic routing for retries and summarization, and final summary generation. An integration layer exposes pipeline execution as a typed event stream, enabling frontends to track progress and outcomes cleanly; Streamlit’s MessageHistory manages a persistent conversation while placeholders surface live updates.

A hands-on investigation illustrates the system’s capabilities: starting from an active crime, the user locates nearby ANPR cameras, retrieves vehicles matching color and partial plate constraints on the incident date, and—with added investigative context—receives deeper summaries that spotlight suspicious temporal patterns. The analysis culminates by linking vehicles to owners with relevant criminal histories, demonstrating how spatial, temporal, and historical signals converge into actionable insights. Looking ahead, the chapter outlines evolution paths powered by observability—mining success and pain points to refine prompts and examples, enriching and layering schemas for scale, sharpening intent detection, and exploring fine‑tuned, knowledge‑graph‑aware components to boost accuracy and efficiency while preserving the transparent, expert‑emulated design.

Overview of the system architecture introduced in the previous chapter. We'll implement this using Streamlit to handle user input (questions and user selection) and output (visualization and summaries), while LangGraph will orchestrate the core pipeline.
State-based communication between agent functions in LangGraph. The diagram illustrates how agents remain decoupled while communicating through an evolving state object. Each agent function receives and updates the global state independently.
LangGraph implementation of the knowledge graph querying pipeline. The solid arrows show the main flow from intent detection through schema extraction and query execution, while dashed arrows indicate conditional paths based on query execution outcomes. This directed graph structure directly maps each component of our expert-emulated approach to a LangGraph agent function.
Backend architecture showing how the LangGraph pipeline integrates with supporting components. The Configuration Provider manages prompts and settings, while the Schema Provider handles database schema access. The Question Processing Interface bridges the core pipeline with frontend applications through an event-based API.
System architecture diagram highlighting the Configuration Provider component. The provider manages system configuration and prompt templates needed by LangGraph agents to process user questions.
System architecture diagram emphasizing the Schema Provider component, which connects to the graph database to extract and transform technical schema information into LLM-friendly formats.
LangGraph implementation of the knowledge expert-emulated graph querying pipeline.
Post-query execution routing logic (highlighted) in the QA pipeline, showing decision paths for retry, summarization, and direct completion
Pipeline integration architecture showing the Question Processing Interface mediating between LangGraph state updates and frontend interactions
Application interface layout demonstrating a question answering system with selection capabilities, interactive graph visualization, and real-time response tracking
Focused schema visualization showing how Crime, ANPRCamera, CameraEvent, Vehicle, and Person nodes interconnect for investigative queries.
Initial query response showing a crime node currently under investigation. The interface displays the current selection, the node's detailed properties in the selection panel (left), the node visualization in the canvas (center), and the query processing details in the chat interface (right).
Spatial query response showing an ANPR camera near the crime location. The system automatically chose a map visualization to display the spatial relationship between the crime and the nearby ANPR camera
Vehicle query results showing matching vehicles and their detection events. Each path represents a complete vehicle detection record, with timestamps visible on the event nodes. The system's response includes both the graph visualization and a detailed summary of each vehicle's properties.
Enhanced analysis showing the same vehicle data with investigative context. The system augments its response with an analysis section that identifies patterns of interest, demonstrating how additional context leads to more insightful summarization of the same underlying data.
Final investigative insight revealing criminal history. The graph expands to show that a vehicle owner has connections to multiple prior crimes, including a previous criminal trespass. The summary provides a detailed breakdown of the prior offenses, demonstrating the system's ability to integrate temporal, spatial, and historical evidence into a cohesive investigative narrative.

Reference

  1. Bhatia, K., Narayan, A., De Sa, C., & Ré, C. (2023). TART: A plug-and-play Transformer module for task-agnostic reasoning. arXiv preprint.
  2. Lester, B., Al-Rfou, R., & Constant, N. (2021). The Power of Scale for Parameter-Efficient Prompt Tuning. In Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing (pp. 3045-3059). Association for Computational Linguistics.

FAQ

What is LangGraph and why is it a good fit for building a knowledge-graph QA pipeline?LangGraph is a framework for stateful, multi-actor applications powered by LLMs. It uses a shared state (a global “whiteboard”) for agent communication and a directed graph of nodes (agents) with dynamic edges for flow control. This makes it ideal for orchestrating complex, reasoning-heavy pipelines like intent detection, schema extraction, query generation, and summarization.
How does the expert-emulated approach map to a LangGraph workflow?Each expert step becomes a node (agent function) that reads and updates a shared AgentState. Edges define the standard sequence (intent → schema → text-to-Cypher → execution), while conditional edges route based on runtime outcomes (e.g., retry on error, summarize for graph/map, end for tables).
What information is stored in the AgentState and how is it used?AgentState tracks the full pipeline context: question, output_type and reasoning, LLM-friendly schema, generated Cypher and its reasoning, execution errors, retries and error info, plus summary text and analysis flags. Agents read and write these fields to coordinate work and enable routing decisions.
What do the Configuration Provider and Schema Provider do?The Configuration Provider centralizes prompts, notes, and examples (via Jinja2 templates) to keep logic clean and tunable. The Schema Provider extracts the technical schema (apoc.meta.schema), filters non-business elements via a skip list, and enriches what remains with business descriptions to produce an LLM-friendly conceptual schema.
How does the intent detection agent work?It takes the user’s question, runs an intent detection prompt, and returns the output_type (table, graph, or map) plus the reasoning. This informs both visualization choices and downstream handling (e.g., whether summarization is needed).
How is natural language converted to Cypher with context awareness?The text-to-Cypher agent merges the current state with configuration annotations and the user’s current selection from the graph UI. It prompts the LLM to generate Cypher and returns the query, the reasoning, and the raw response for debugging—allowing users to reference selected nodes naturally (e.g., “the selected crime”).
How are queries executed and errors handled?The execution agent runs the Cypher, formats results based on output_type (list of records for graph/map, DataFrame for table), and captures any errors. It records errors and a detailed “information” message, increments a retry counter, and preserves successful results for visualization or summarization.
What is the post-execution routing logic?It is a dynamic edge: if results_error exists and retries are below 3, it routes back to text-to-Cypher for retry; otherwise it ends. If successful, graph/map outputs route to summarization, while table outputs end directly.
How does the Streamlit integration provide real-time feedback?A Question Processing Interface exposes the pipeline as a generator that streams events: “update” (progress), “result” (reasoning/errors/summary), and visualization payloads (graph/map/table). Streamlit placeholders show live updates, while a MessageHistory object persists the final conversation state when an END event arrives.
What future enhancements are suggested?Improvements include learning from usage (collecting pain points and successful examples), schema enrichment and multi-layer schema views, more nuanced intent detection, and potentially fine-tuned, KG-aware agents to scale beyond in-context prompts and improve performance.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Knowledge Graphs and LLMs in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Knowledge Graphs and LLMs in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Knowledge Graphs and LLMs in Action ebook for free