Chapter 15: Building a QA agent with LangGraph
The introduction previews an application that will combine chapter 14’s knowledge-graph querying techniques into a LangGraph pipeline with a Streamlit interface.
After this chapter you will be able to
- Shared state communication. Trace how a simple RAG workflow uses shared state: the question is stored first, retrieval adds documents, and answer generation reads both before adding an answer.
- Graph execution and branching. Explain what nodes and edges represent in a LangGraph workflow and how the next node can be selected based on a preceding agent’s output.
- LangGraph workflow and state. Describe how the KG querying process maps to LangGraph: each processing step is a discrete agent function that receives and updates workflow state.
- Centralized text configuration. Explain why the QA system keeps prompt templates and KG annotations in a configuration component rather than in the main implementation.
- Query guidance and prompt references. Describe the three elements in the configuration and how they support the LLM and pipeline.
- Technical and conceptual schema gap. Explain why the question-answering system needs to transform the schema it can extract from the graph database.
- Configurable filtering and enrichment. Describe how the skip list and description section turn the technical schema into a business-oriented view.
- Shared agent state. Explain how the LangGraph state object lets pipeline agents communicate and establishes responsibility for updating it.
- Initial pipeline state preparation. Describe how the intent detection and schema extraction agents prepare state for later steps. The first selects a visualization type from the question; the second supplies an LLM-friendly KG schema and initializes the retry counter.
- Contextual Cypher query generation. Explain how the text-to-Cypher agent turns a question into a query using the graph schema, configuration annotations, and the current visualization selection, then records its output in state.
- Query results and failures. Describe how the query execution agent formats successful results according to visualization intent and records information when execution fails.
- Post-execution routing decisions. Trace how the dynamic edge routes a failed execution toward retry, or a successful execution toward summarization or completion.
- LangGraph workflow assembly. Explain how the implemented agents and routing are assembled into one typed LangGraph workflow.
- Streaming interface rationale. Explain why the application uses an interface layer instead of relying on LangGraph’s invoke mode or handling stream updates directly.
- Question-processing generator flow. Trace how the question-processing generator supplies events as the pipeline advances through its stages.
- Event contract and responsibilities. Describe the information in each event, distinguish its three response types, and explain why the current state accompanies it.
- Expert-emulating interface requirements. Describe the interactions and feedback the interface must support for the question-answering pipeline.
- Streamlit pipeline fit. Explain how Streamlit’s features meet the interface requirements and connect to the LangGraph pipeline.
- Graph-centered questioning workflow. Describe how the graph canvas and question input work together to let users explore the knowledge graph and ask natural-language questions about it.
- Selection-based question context. Explain how selecting graph nodes lets users refer to them naturally in a question, and why that interaction matters for testing context-awareness.
- Adaptive response history. Describe how the history area presents different answer formats and updates during processing, and explain what those updates reveal to users.
- Event-driven question flow. Trace how a submitted question reaches the LangGraph pipeline and why the interface shows updates before the final answer.
- Temporary and persistent updates. Explain how temporary Streamlit placeholders and MessageHistory work together, including what happens at an END event.
- Investigative graph connections. Trace how Crime, ANPRCamera, CameraEvent, Vehicle, and Person nodes connect, and explain how those connections support spatial, temporal, and relationship-based investigative queries.
- Initial query interpretation. Explain how the system interprets the request for one crime currently under investigation and chooses how to present the result.
- Selection-aware camera search. Explain how selecting the crime node lets the investigator ask for ANPR cameras within 1 km of “the selected crime” without supplying coordinates or calculating distances.
- Dual-view spatial insight. Describe how the graph and interactive map together show the crime and camera, and why the camera’s position might offer an investigative lead.
- Selection grounded vehicle query. Explain how the investigator searches for vehicles by selecting the ANPR camera and asking for detections on June 15, 2023, of black vehicles with plates starting with EB.
- Constraints from the selected crime. Explain how the system uses the selected crime node to obtain vehicle-description and incident-date constraints without the investigator restating them.
- Investigative context in summaries. Explain how stating the investigator’s role and asking which vehicles seem more likely to be involved changes the summary, even though the query returns the same vehicles.
- Ownership and criminal history connection. Trace how the investigation links a previously flagged vehicle to its owner’s criminal record, and explain why a prior conviction for the same type of offense is significant.
- Observability as an improvement foundation. Explain why the system’s transparency is useful beyond debugging: visible component decisions help teams collect feedback and identify improvements.
- Expert emulation as design guide. Explain how modeling expert behavior guides improvements to the QA system when it faces new challenges.
- Observability as improvement input. Explain how information recorded for each query can reveal user intentions, query patterns, and outcome effectiveness, allowing improvements to follow actual use rather than predetermined rules.
- Complaint and success patterns. Describe how complaint-like questions could be categorized into recurring pain points, and how reasoning from especially helpful interactions could inform the example database.
- Expert-like schema enrichment. Explain how preliminary analysis of a KG’s data patterns could add context to its base schema and improve downstream query generation and result interpretation.
- Layered schema views. Describe how a multilayer schema could give an agent both broad domain views and the detailed descriptions needed to generate specific queries.
- Fine-tuning for graph understanding. Explain why the section proposes fine-tuning as an alternative to putting schema information and examples in prompts, and what benefits it might offer.
What it builds on
- Enriching schemas with descriptive annotations (chapter 14) · The use of descriptive annotations to give the LLM graph context.
- Thinking in queries: From text to Cypher (chapter 14) · The task of turning a question into a graph query.
- From schema to LLM-ready context (chapter 14) · The need to turn schema information into LLM-ready context.
- Intent detection: Understanding user expectations (chapter 14) · Interpreting what a user expects from a question.
- A practical approach to schema representation (chapter 14) · Representing graph schema information for LLM use
Section by section
Introduction
The introduction previews an application that will combine chapter 14’s knowledge-graph querying techniques into a LangGraph pipeline with a Streamlit interface.
15.1 Building the LangGraph pipeline
The section introduces LangGraph’s shared-state and directed-graph model for coordinating the stages of a knowledge-graph question-answering pipeline.
- Shared state communication: Trace how a simple RAG workflow uses shared state: the question is stored first, retrieval adds documents, and answer generation reads both before adding an answer.
Why it matters: Shared state lets specialized components use earlier results while keeping their communication decoupled. - Graph execution and branching: Explain what nodes and edges represent in a LangGraph workflow and how the next node can be selected based on a preceding agent’s output.
Why it matters: The graph defines both the work performed at each step and how the workflow proceeds or branches. - Pipeline coordination rationale: Justify why shared state and flexible flow control suit the expert-emulating KG querying architecture described here.
Why it matters: It connects LangGraph’s general workflow model to the chapter’s KG querying application.
Key terms: shared state, document retrieval, answer generation, directed graphs, nodes, edges, dynamic edge resolution, coherent state, flexibility in flow control, expert-emulating architecture
15.1.1 System architecture overview
This section maps the KG question-answering process onto a LangGraph workflow and explains how providers and a processing interface support it.
- LangGraph workflow and state: Describe how the KG querying process maps to LangGraph: each processing step is a discrete agent function that receives and updates workflow state.
Why it matters: This mapping explains how the question-answering process becomes an executable workflow. - Configuration and schema providers: Distinguish the two providers that support the central workflow and state what each supplies.
Why it matters: Keeping these inputs in providers gives each backend component a clear responsibility. - Frontend processing interface: Explain how the question processing interface connects frontend applications to the LangGraph workflow.
Why it matters: It lets a frontend show progress and results without taking over the workflow’s question-answering logic.
Key terms: LangGraph workflow, agent function, workflow state, configuration provider, schema provider, prompt templates, database schema, question processing interface, event stream, state updates
15.1.2 Configuring pipeline components
This section explains how a centralized configuration component stores the QA pipeline’s guidance and prompt references while exposing templates and annotations through a clean interface.
- Centralized text configuration: Explain why the QA system keeps prompt templates and KG annotations in a configuration component rather than in the main implementation.
Why it matters: Separating text resources from code makes the pipeline easier to maintain and refine. - Query guidance and prompt references: Describe the three elements in the configuration and how they support the LLM and pipeline.
Why it matters: These resources give the LLM guidance for generating queries while keeping stage-specific prompts organized. - Template and annotation access: Explain how Jinja2 templates and the ChainConfiguration interface separate template authoring and configuration storage from their use.
Why it matters: The interface lets pipeline code use prompts and annotations without taking on their storage and formatting details.
Key terms: configuration component, prompt templates, KG annotations, separation of concerns, operational notes, examples, prompt template references, query generation, Jinja2, ChainConfiguration
15.1.3 Schema translation service
The section explains how a schema provider turns a Neo4j technical schema into a business-oriented schema that LLMs can use to generate queries.
- Technical and conceptual schema gap: Explain why the question-answering system needs to transform the schema it can extract from the graph database.
Why it matters: This explains why exposing the database schema directly to the LLM is not enough. - Configurable filtering and enrichment: Describe how the skip list and description section turn the technical schema into a business-oriented view.
Why it matters: Filtering and enrichment give the LLM useful business context without burdening its prompt with implementation details. - Schema provider transformation flow: Trace how the schema provider extracts, transforms, and supplies schema information for query generation.
Why it matters: This connects the configuration to the schema the LLM actually uses when constructing queries.
Key terms: conceptual schema, technical schema, schema provider, skip list, description section, YAML, business-level descriptions, apoc.meta.schema, get_schema, apply_configuration
15.1.4 State management design
This section explains how a shared LangGraph state carries information between pipeline agents and records what the pipeline needs to route work and handle errors.
- Shared agent state: Explain how the LangGraph state object lets pipeline agents communicate and establishes responsibility for updating it.
Why it matters: Shared state explains how one agent’s work becomes available to the next. - State fields and pipeline progress: Describe the kinds of information held in the state and why retaining them matters as the pipeline progresses.
Why it matters: These fields let the pipeline retain both its results and the context needed to decide what happens next.
Key terms: state object, shared memory space, chain of responsibility, question, output_type, schema, query, results_error, summary
15.1.5 Pipeline agent implementation
This section shows how specialized LangGraph agents update shared state, execute and route a KG query, and form a question-answering workflow.
- Initial pipeline state preparation: Describe how the intent detection and schema extraction agents prepare state for later steps. The first selects a visualization type from the question; the second supplies an LLM-friendly KG schema and initializes the retry counter.
Why it matters: These state updates give later agents the intended output format, usable graph structure, and a starting point for retries. - Contextual Cypher query generation: Explain how the text-to-Cypher agent turns a question into a query using the graph schema, configuration annotations, and the current visualization selection, then records its output in state.
Why it matters: Selection-aware queries let users ask concise follow-up questions about what they are viewing. - Query results and failures: Describe how the query execution agent formats successful results according to visualization intent and records information when execution fails.
Why it matters: The agent supplies appropriately shaped results or the error context needed for a retry. - Post-execution routing decisions: Trace how the dynamic edge routes a failed execution toward retry, or a successful execution toward summarization or completion.
Why it matters: One workflow can respond differently to failed queries and to different kinds of successful answers. - Graph and map summaries: Describe what the generate-summary agent uses and what it adds to state for graph and map answers.
Why it matters: The summary adds explanatory context to graph and map results. - LangGraph workflow assembly: Explain how the implemented agents and routing are assembled into one typed LangGraph workflow.
Why it matters: Assembly turns the individual agent functions and routing decision into a single usable pipeline.
Key terms: intent detection agent, output_type, output_reason, schema extraction agent, Neo4jSchema, retries, text-to-Cypher agent, annotations, selection state, query
15.1.6 Pipeline integration layer
This section explains how a question-processing interface turns LangGraph pipeline execution into frontend-friendly events that provide progress feedback while keeping presentation separate.
- Streaming interface rationale: Explain why the application uses an interface layer instead of relying on LangGraph’s invoke mode or handling stream updates directly.
Why it matters: This explains how the application gives users timely feedback without making the frontend manage raw pipeline updates. - Question-processing generator flow: Trace how the question-processing generator supplies events as the pipeline advances through its stages.
Why it matters: The generator lets the frontend receive intermediate information without turning question processing into a collection of separate calls. - Event contract and responsibilities: Describe the information in each event, distinguish its three response types, and explain why the current state accompanies it.
Why it matters: A consistent event contract gives the frontend enough information to respond appropriately without coupling its presentation choices to the pipeline.
Key terms: invoke mode, stream execution mode, interface layer, generator function, initial configuration and state, intent detection, pipeline stage, response type, response payload, current state
15.2 Streamlit application
This section explains why a Streamlit interface fits the LangGraph question-answering pipeline’s interaction, feedback, and prototyping needs.
- Expert-emulating interface requirements: Describe the interactions and feedback the interface must support for the question-answering pipeline.
Why it matters: These requirements let users interact with the system and see how it processes their questions. - Streamlit pipeline fit: Explain how Streamlit’s features meet the interface requirements and connect to the LangGraph pipeline.
Why it matters: The framework makes the pipeline’s interactions and intermediate progress visible with less integration work. - Prototyping over production specialization: Justify choosing Streamlit for developing and testing this system while acknowledging its stated deployment tradeoff.
Why it matters: It clarifies that the interface choice serves validation and demonstration, not necessarily every production need.
Key terms: interactive graph visualization, chat-like interface, real-time feedback, state information, Streamlit, session state, automatic UI updating, Python-first approach, custom components, prototyping
15.2.1 Application overview
The section explains how the application interface supports graph exploration, context-aware questions, and visible, multiformat answers.
- Graph-centered questioning workflow: Describe how the graph canvas and question input work together to let users explore the knowledge graph and ask natural-language questions about it.
Why it matters: These two areas make graph exploration and questioning part of one workflow. - Selection-based question context: Explain how selecting graph nodes lets users refer to them naturally in a question, and why that interaction matters for testing context-awareness.
Why it matters: Selection makes questions easier to phrase and provides a way to check whether the system uses what the user selected. - Adaptive response history: Describe how the history area presents different answer formats and updates during processing, and explain what those updates reveal to users.
Why it matters: Users receive an answer suited to the information and can see progress rather than waiting for an unexplained final result.
Key terms: graph canvas, question input area, KG, Selection column, context-awareness, visual context, query generation, history area, interactive maps, tables
15.2.2 LangGraph integration
This section explains how Streamlit displays LangGraph pipeline progress immediately while preserving a complete, rendered conversation history.
- Event-driven question flow: Trace how a submitted question reaches the LangGraph pipeline and why the interface shows updates before the final answer.
Why it matters: The user can see that a question is being processed instead of waiting without feedback. - Temporary and persistent updates: Explain how temporary Streamlit placeholders and MessageHistory work together, including what happens at an END event.
Why it matters: This distinction explains how the app offers live progress without losing the final conversation record. - Message content display: Describe what MessageHistory stores and how the interface presents different kinds of pipeline output.
Why it matters: Pipeline results remain reviewable and appear in a form suited to their content.
Key terms: event-driven pattern, question processing interface, LangGraph pipeline, Streamlit placeholders, MessageHistory, END event, permanent conversation history, message dictionaries, update method, display_message
15.3 Expert-emulating investigation
Introduces the knowledge-graph connections that support an investigation across crimes, surveillance sightings, vehicles, and people.
- Investigative graph connections: Trace how Crime, ANPRCamera, CameraEvent, Vehicle, and Person nodes connect, and explain how those connections support spatial, temporal, and relationship-based investigative queries.
Why it matters: This graph structure explains what evidence an investigator can connect in a query.
Key terms: Crime, ANPRCamera, CameraEvent, Vehicle, Person, COMMITTED
15.3.1 Identifying the initial case
An initial crime query shows how the QA agent selects an active case, presents it as a graph node, and surfaces details for the investigation.
- Initial query interpretation: Explain how the system interprets the request for one crime currently under investigation and chooses how to present the result.
Why it matters: This explains how a plain-language request becomes a useful starting point for investigating a case. - Initial case context: Describe what the initial result reveals and how the interface helps the user understand it.
Why it matters: The case details and visible reasoning give the user both leads to pursue and a way to check how the question was interpreted.
Key terms: explicit constraints, implicit expectations, graph visualization, Cypher query, criminal trespass, narrative, chat interface, summary
15.3.2 Spatial analysis of surveillance coverage
The section shows how a selection-aware spatial query finds a nearby ANPR camera and uses graph and map views to make its investigative value apparent.
- Selection-aware camera search: Explain how selecting the crime node lets the investigator ask for ANPR cameras within 1 km of “the selected crime” without supplying coordinates or calculating distances.
Why it matters: The investigator can search around a known incident without manually working out its location. - Dual-view spatial insight: Describe how the graph and interactive map together show the crime and camera, and why the camera’s position might offer an investigative lead.
Why it matters: Seeing the locations on a map helps the investigator recognize why this camera’s data could be worth checking.
Key terms: crime node, ANPR camera, selected crime, spatial query processing, graph visualization, interactive map, adaptive response
15.3.3 Vehicle pattern detection
This section shows how an investigator combines a selected ANPR camera with incident details to find and inspect matching vehicles.
- Selection grounded vehicle query: Explain how the investigator searches for vehicles by selecting the ANPR camera and asking for detections on June 15, 2023, of black vehicles with plates starting with EB.
Why it matters: Combining the selected graph element with incident details narrows the search to relevant vehicle detections. - Vehicle detection results: Describe what the expanded graph and summary show after the vehicle query, and why the vehicles remain potential rather than confirmed suspects.
Why it matters: The results turn camera detections into investigative leads without establishing that any vehicle was involved in the incident.
Key terms: ANPR camera, current selection, temporal constraint, property matching, vehicle nodes, detection events, potential suspects
15.3.4 Context-aware request refinement
This section shows how a request grounded in a selected crime node and an investigator’s intent can return the same vehicles while producing a more useful analysis of their detection times.
- Constraints from the selected crime: Explain how the system uses the selected crime node to obtain vehicle-description and incident-date constraints without the investigator restating them.
Why it matters: It lets an investigator refer to the selected case rather than repeat details already stored with it. - Investigative context in summaries: Explain how stating the investigator’s role and asking which vehicles seem more likely to be involved changes the summary, even though the query returns the same vehicles.
Why it matters: A useful investigative response can come from interpreting the same results more carefully, not just retrieving different results.
Key terms: selected crime node, node properties, constraints, investigative context, summarization, temporal patterns
15.3.5 Historical record analysis
The section shows how adding vehicle owners’ criminal histories to an ongoing investigation can reveal a relevant connection and help the agent present it as an investigative lead.
- Ownership and criminal history connection: Trace how the investigation links a previously flagged vehicle to its owner’s criminal record, and explain why a prior conviction for the same type of offense is significant.
Why it matters: The owner’s relevant history adds context to an existing lead without establishing that the vehicle was involved. - Contextual evidence and investigative summary: Describe how the refined request adds owner history to the existing vehicle investigation and how the system presents the resulting connection to support investigative decisions.
Why it matters: The investigator can refine a natural-language request and receive a summary that relates new background evidence to earlier findings.
Key terms: ownership relationships, criminal records, criminal trespass, query refinements, spatial, temporal, and historical, summary agent
15.4 Future directions and enhancements
This section explains why the QA agent is a foundation for improvement: its visible decisions support feedback, and its expert-emulating design guides changes.
- Observability as an improvement foundation: Explain why the system’s transparency is useful beyond debugging: visible component decisions help teams collect feedback and identify improvements.
Why it matters: Visible decisions give teams a basis for improving the system rather than just troubleshooting it. - Expert emulation as design guide: Explain how modeling expert behavior guides improvements to the QA system when it faces new challenges.
Why it matters: Expert behavior provides a practical guide for deciding how the foundation should evolve.
Key terms: transparency, observability, feedback, expert emulation, schema context, reasoned steps
15.4.1 Learning from use
This section explains how data from actual use could guide improvements to the QA agent.
- Observability as improvement input: Explain how information recorded for each query can reveal user intentions, query patterns, and outcome effectiveness, allowing improvements to follow actual use rather than predetermined rules.
Why it matters: It explains why observing the agent's use can guide its evolution. - Complaint and success patterns: Describe how complaint-like questions could be categorized into recurring pain points, and how reasoning from especially helpful interactions could inform the example database.
Why it matters: It shows how feedback from both poor and helpful responses can become actionable. - Similar-query handling: Explain how clusters of similar user questions could motivate specialized handling, such as selecting relevant schema portions or more focused examples.
Why it matters: It gives a concrete way that observed question patterns could improve downstream processing.
Key terms: comprehensive observability, actual use patterns, complaint-like questions, LLM-based analysis, pain points, chain of reasoning, example database, clusters of similar queries, specialized handling, schema
15.4.2 Enhancing core capabilities
The section proposes two ways to make a KG question-answering agent handle schemas more like a human expert: enriching them from data patterns and organizing them at multiple levels of detail.
- Expert-like schema enrichment: Explain how preliminary analysis of a KG’s data patterns could add context to its base schema and improve downstream query generation and result interpretation.
Why it matters: A schema grounded in actual data patterns could give the agent more useful context for its queries and answers. - Layered schema views: Describe how a multilayer schema could give an agent both broad domain views and the detailed descriptions needed to generate specific queries.
Why it matters: Layered views could let the agent navigate a large KG without losing the detail needed for precise queries.
Key terms: schema enrichment agents, base schema, data patterns, multilayer schema management, domain-focused views, levels of abstraction
15.4.3 Advanced evolution paths
This section argues that fine-tuned, KG-aware components could address the scaling limits of prompt-based schema understanding without replacing the system’s expert-emulating pipeline.
- Fine-tuning for graph understanding: Explain why the section proposes fine-tuning as an alternative to putting schema information and examples in prompts, and what benefits it might offer.
Why it matters: It explains why an effective prompt-based approach might still need to evolve as the KG grows. - Selective pipeline evolution: Describe how fine-tuned components could be introduced without giving up the existing expert-emulating architecture.
Why it matters: Improving a component need not mean discarding the pipeline’s visible reasoning structure.
Key terms: in-context learning, fine-tuning, schema, KG-aware agents, expert-emulating architecture, in-context learning components, observable pipeline structure, step-by-step reasoning
15.5 Summary
Concepts explained in this chapter
- LangGraphLangGraph is a framework for building stateful, graph-structured workflows and agent applications powered by language models.
- AI agentAn agent is a computational system that can use information and perform actions to accomplish tasks.
- automated query generationQuery generation is the process of producing an executable database query from a natural-language request, structured intent, or other specifications.
- retryA retry mechanism is a control procedure that repeats a failed operation according to configured retry conditions.
- StateGraphStateGraph is a LangGraph class for constructing workflows whose nodes operate over shared typed state.
- agent stateAgent state is a structured record containing the information exchanged and accumulated by agents during a workflow.
- dynamic routingDynamic routing is a workflow technique that selects the next processing path according to runtime state or results.
- query executionQuery execution is the process of running a database query and processing the records or errors it produces.
- schema extractionSchema extraction is the technique of identifying and structuring schema elements from a data source or schema description.
- StreamlitStreamlit is an open-source Python framework for creating interactive web applications from data and machine-learning code.
- conceptual schemaA conceptual schema is a domain-focused representation of a knowledge graph's essential entities, relationships, properties, and types, excluding technical implementation details.
- edgeA relationship, or edge, is a graph connection representing a meaningful association between nodes.
- summarizationSummarization is the process of condensing information into a shorter representation that preserves its most relevant factual content.
- fine-tuningFine-tuning is adapting a pretrained model by continuing its training on targeted data or objectives.
- technical database schemaA technical database schema is an implementation-level specification of a database's structures, elements, relationships, properties, metadata, and administrative details.
- retrieval-augmented generationRetrieval-augmented generation is a technique that supplies a generative language model with relevant information retrieved from an external source before answer generation.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
