20 minute read

How to Use LangSmith Human Feedback Loops to Improve LangGraph RAG Accuracy

How to Use LangSmith Human Feedback Loops to Improve LangGraph RAG Accuracy

Imagine you have a super-smart helper, an AI, that answers questions by looking up facts in a giant library. This is a Retrieval Augmented Generation (RAG) system. You want this helper to always give you the correct and most helpful answers.

Sometimes, even super-smart helpers make mistakes or get confused. That’s where you, a human, come in! By giving your AI helper “feedback,” you can teach it to be even better and more accurate.

This guide will show you how to use LangSmith human feedback loops to make your LangGraph RAG system incredibly accurate. We’ll explore how your valuable insights can transform your AI’s performance, ensuring it always hits the mark.

What is RAG and Why Accuracy Matters?

RAG, or Retrieval Augmented Generation, is like having an AI brain that can also read books. When you ask it a question, it first “retrieves” information from a special knowledge base, like a digital library. Then, it “augments” or adds this information to its own thoughts, and finally “generates” an answer.

Think of it as looking up facts before answering a tough question. This process helps the AI give you specific, up-to-date answers and stops it from just guessing. However, if the AI pulls up the wrong book or misreads a page, its answer won’t be accurate.

Accuracy is super important because you need to trust the information your AI provides. If a RAG system gives wrong answers, it can be misleading and unhelpful. That’s why we need ways to check and improve its performance.

You can build powerful RAG applications using frameworks like LangGraph, which allows you to define complex steps for your AI agent. To learn more about setting up multi-step AI agents, you might find this post helpful: LangGraph: Building Multi-Step AI Agents with StateGraph.

Introducing LangSmith: Your AI Helper’s Report Card

LangSmith is like a special observation tool and report card for your AI systems. It helps you see exactly what your AI is doing behind the scenes, step by step. When your LangGraph RAG answers a question, LangSmith records every action it takes.

This detailed recording, called “tracing,” is incredibly helpful for understanding why your AI gives certain answers. It shows you which documents it looked at and how it used them to generate its response. Without LangSmith, it’s hard to know if your AI made a good choice or a silly mistake.

LangSmith is also where you can give your AI system its “report card.” This is what we call human-in-the-loop evaluation, meaning a human is actively involved in checking and improving the AI’s work.

The Power of Human Feedback Loops

Human feedback loops are simple but incredibly powerful. They mean you, as a human, tell your AI if its answer was good, bad, or somewhere in between. This isn’t just a one-time thing; it’s a “loop” because your feedback helps the AI learn, and then you give more feedback on its improved answers.

Imagine teaching a puppy new tricks. You reward it when it does well and gently guide it when it makes a mistake. Human feedback works similarly for your LangGraph RAG system. It helps the AI understand what “good” and “accurate” truly mean from a human perspective.

These loops are vital for your LangGraph RAG because they directly address its weaknesses. If your AI keeps retrieving irrelevant information, your feedback points that out. If it writes unclear answers, your feedback helps it learn to be clearer. This human-in-the-loop evaluation is the fastest way to boost your LangGraph RAG’s accuracy.

Setting Up Your LangGraph RAG for Feedback

To get human feedback, your LangGraph RAG system needs to be set up to “talk” to LangSmith. This usually involves integrating LangSmith tracing into your application. When you run your LangGraph RAG, LangSmith will automatically record all the steps and results.

A basic LangGraph RAG flow might involve a node to retrieve documents and another node to generate an answer. Each time this flow runs, LangSmith creates a “trace” that shows you everything. This trace is where you’ll later add your human feedback.

Here’s a simplified idea of how you might start a LangGraph and ensure LangSmith can track it. You’d typically set an environment variable for LangSmith to automatically trace.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from langgraph.graph import StateGraph, END
from langchain_core.messages import HumanMessage
from langchain_community.llms import OpenAI # Example LLM
from langchain_community.vectorstores import Chroma # Example vector store
from langchain_openai import OpenAIEmbeddings
from langchain_core.documents import Document

# Set LangSmith environment variables (replace with your actual API key)
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
# os.environ["LANGCHAIN_API_KEY"] = "YOUR_LANGSMITH_API_KEY"
# os.environ["LANGCHAIN_PROJECT"] = "LangGraph RAG Feedback Demo" # Your project name

# For this example, let's assume a simple vector store and LLM setup
# In a real app, you'd load your actual vector store and LLM.
vectorstore = Chroma.from_documents(
    [
        Document(page_content="The quick brown fox jumps over the lazy dog.", metadata={"source": "fable"}),
        Document(page_content="Apples are a type of fruit, rich in vitamins.", metadata={"source": "nutrition"}),
        Document(page_content="Python is a popular programming language.", metadata={"source": "tech"}),
    ],
    OpenAIEmbeddings()
)
retriever = vectorstore.as_retriever()
llm = OpenAI(temperature=0)

# Define a simple RAG state
class GraphState:
    question: str
    documents: list = []
    answer: str = ""

# Define the nodes in your LangGraph
def retrieve(state: GraphState):
    print("---RETRIEVE---")
    question = state.question
    documents = retriever.invoke(question)
    return {"documents": documents, "question": question}

def generate(state: GraphState):
    print("---GENERATE---")
    question = state.question
    documents = state.documents
    
    # Simple prompt for generation
    prompt = f"Based on the following documents, answer the question.\n\nDocuments: {' '.join([doc.page_content for doc in documents])}\n\nQuestion: {question}\nAnswer:"
    
    answer = llm.invoke(prompt)
    return {"answer": answer}

# Build the LangGraph
workflow = StateGraph(GraphState)

workflow.add_node("retrieve", retrieve)
workflow.add_node("generate", generate)

workflow.set_entry_point("retrieve")
workflow.add_edge("retrieve", "generate")
workflow.add_edge("generate", END)

app = workflow.compile()

# Example usage (will generate a trace in LangSmith if environment variables are set)
# inputs = {"question": "What is Python?"}
# for output in app.stream(inputs):
#     for key, value in output.items():
#         print(f"Finished running: {key}: {value}")

With LangSmith environment variables configured, every time you run app.stream() or app.invoke(), a trace will appear in your LangSmith project. This trace is your window into how your LangGraph RAG behaved.

Collecting Human Feedback in LangSmith

Once your LangGraph RAG is sending traces to LangSmith, you can start collecting feedback. This is the crucial step where you, or other users, tell the system what you think of its answers. LangSmith offers several ways to gather this valuable input.

Annotation Queues: Your Feedback Inbox

Think of annotation queues as a special inbox for tasks that need human review. You can set up queues in LangSmith specifically for your LangGraph RAG traces. For example, you might create a queue called “RAG Answer Quality.”

When a trace lands in this queue, it’s waiting for a human to look at it and provide feedback. You can assign different types of feedback to these queues, ensuring you collect the specific insights you need. This helps you manage which RAG responses need evaluation.

You can configure an annotation queue in the LangSmith UI, specifying the type of feedback you want to collect. For instance, you could ask for a “relevance score” or a “correctness check.”

Thumbs Up/Down and Feedback Scores: Simple Signals

Sometimes, you just need a quick way to say “good” or “bad.” LangSmith allows users to give a simple “thumbs up” or “thumbs down” to a RAG response. This is a very fast way to get immediate reactions to your AI’s performance.

For more detailed but still easy feedback, you can use feedback scores. These are often on a scale, like 0-1 (bad to good) or 1-5 (poor to excellent). For example, a user might give an answer a “2” if it’s mostly wrong or a “5” if it’s perfect. These scores provide a quantitative way to measure satisfaction.

Imagine your LangGraph RAG answers a question about “what is the capital of France?” If it correctly says “Paris,” you’d give it a thumbs up or a score of 5. If it said “Rome,” you’d give it a thumbs down or a score of 1. These simple signals quickly tell you if your AI is on the right track.

Detailed Comments: The “Why” Behind the Feedback

While scores and thumbs up/down are useful, detailed comments tell you the “why.” If an answer was bad, why was it bad? Was the information incomplete, incorrect, or just confusing? LangSmith lets humans add free-form text comments to any trace.

These comments are incredibly valuable because they provide context. For example, a comment might say: “The retrieved document mentioned the answer, but the final generated text completely ignored it.” Or, “The answer was mostly correct, but it missed the specific detail about the latest product version.”

Such detailed feedback helps you pinpoint the exact stage in your LangGraph RAG where things went wrong. It’s like a detective finding clues to solve a mystery.

Analyzing Feedback to Find RAG Problems

Collecting feedback is just the first step. The real magic happens when you analyze it. LangSmith provides tools to help you sift through the feedback and identify patterns. This analysis helps you understand your LangGraph RAG’s strengths and weaknesses.

LangSmith Dashboard: Seeing Your AI’s Performance

The LangSmith dashboard is your control center for viewing all the collected feedback. You can see aggregated scores, the number of thumbs up/down, and all the comments. It’s like looking at a summary report of your AI helper’s overall performance.

You can filter these results to focus on specific problems. For instance, you might want to see only the traces that received a “thumbs down” or a low score. This helps you quickly find the instances where your LangGraph RAG struggled the most.

You can then dive into individual traces that have negative feedback to see exactly what happened. This allows you to identify common types of errors that your RAG system is making.

Connecting Feedback to LangGraph Steps

One of LangSmith’s most powerful features is its ability to link human feedback directly to the individual steps of your LangGraph. When you look at a trace, you can see each node: the retrieval step, the generation step, and any other steps you’ve defined.

If a human gives a “thumbs down” on an answer, you can then click into that trace and examine the retrieved documents. Were they relevant? Or did the RAG pull up completely unrelated information? This helps you determine if the problem lies with the retrieval part of your RAG.

Alternatively, if the documents were good but the answer was still poor, the problem might be in the generation step. Perhaps the prompt was unclear, or the LLM struggled to synthesize the information correctly. Understanding these connections helps you target your improvements effectively.

For example, if many “thumbs down” traces show that the retrieved documents were off-topic, it’s a strong signal that your retrieval mechanism needs work. You might need to refine your embedding model, adjust how you chunk your documents, or even explore different vector stores. To understand how to build robust RAG applications with vector stores, check out Build RAG Applications with LangChain and Vector Stores in 2026.

Using Feedback to Improve Your LangGraph RAG

Once you’ve analyzed the feedback and identified the problems, it’s time to make changes. This is where the “loop” aspect of human feedback truly comes into play. You use the insights to make your LangGraph RAG better, and then you collect more feedback on the improved version.

Iterative Improvement: The Feedback Loop in Action

Improving your LangGraph RAG is an ongoing journey, not a one-time fix. Each piece of feedback, whether positive or negative, is a learning opportunity. You make small, targeted changes, deploy them, and then monitor the new feedback to see if your changes worked. This is called iterative improvement.

Think of it like tuning a musical instrument. You play a note, listen, adjust, and then play again until it sounds perfect. With human feedback, you’re constantly tuning your RAG system for maximum accuracy and relevance. This continuous cycle ensures your AI helper gets smarter over time.

Targeted Adjustments Based on Feedback

Different types of feedback point to different parts of your LangGraph RAG system that need attention. By understanding where the problem lies, you can make very specific and effective changes.

Improving Retrieval

If human feedback consistently indicates that the retrieved documents are irrelevant or not comprehensive enough, your retrieval system needs an upgrade. This is often the most common source of RAG errors.

You can improve retrieval by:

  • Adjusting chunking strategy: How you break down large documents can greatly affect what gets retrieved. Smaller, more focused chunks might be better for specific questions. Consider techniques like semantic text splitting to chunk by meaning, which can be found in LangChain Semantic Text Splitter: Chunk by Meaning for Better RAG.
  • Using a better embedding model: The model that turns your text into numerical vectors (embeddings) directly impacts search quality.
  • Refining retriever settings: Experiment with k (number of documents to retrieve), different search types (e.g., maximum marginal relevance), or more advanced techniques like hybrid search. Hybrid search, which combines keyword and semantic search, can significantly boost retrieval accuracy. Learn more about it in LangChain Weaviate Hybrid Search for Scalable RAG.

For example, if feedback says, “The RAG always misses the key detail about ‘feature X’ even though it’s in the document,” you might need to re-evaluate your chunking strategy or even add specific metadata to your documents to help the retriever.

Refining Generation

If the retrieved documents are good, but the AI’s answer is still poor (e.g., incomplete, confusing, or just plain wrong), then the generation step needs attention. This is where the Large Language Model (LLM) combines the retrieved information with your prompt to form an answer.

You can refine generation by:

  • Adjusting the prompt: The instructions you give the LLM are crucial. Make sure your prompt is clear, concise, and guides the LLM to use the retrieved context effectively. Add instructions like “Summarize the following documents to answer the question, do not include information not present in the documents.”
  • Using a different LLM: Some LLMs are better at summarization, others at creative writing. Try experimenting with different models.
  • Adding an output parser: Sometimes, the LLM generates text that isn’t quite in the format you want. An output parser can structure the LLM’s raw text into a clean, usable format. This can make answers more consistent and easier to understand. For a deeper dive, check out LangChain Custom Output Parser Tutorial.

For instance, if feedback repeatedly notes, “The answer contains irrelevant details not asked for,” you’d revise your generation prompt to emphasize conciseness and strict adherence to the question.

Optimizing LangGraph Flow

Sometimes, the issue isn’t just one step but how the steps interact within your LangGraph. Your feedback might reveal that your multi-step agent isn’t making the best decisions about when to retrieve, when to generate, or when to stop.

You can optimize your LangGraph flow by:

  • Adjusting conditional edges: These are the “if-then” rules that decide the next step in your graph. For example, if feedback shows the RAG always retrieves even when it already has enough information, you might add a “should_retrieve” node with a conditional edge that only triggers retrieval if the initial context is insufficient.
  • Adding new nodes: You might realize your RAG needs an extra step, like a “rewrite query” node before retrieval to make searches more effective, or a “self-reflection” node after generation to critique its own answer.

If feedback points to the agent getting stuck in loops or taking unnecessary steps, it’s a clear sign to review your LangGraph’s state transitions and conditional logic. Understanding how to build robust state graphs is key, as discussed in LangGraph: Building Multi-Step AI Agents with StateGraph.

Advanced Feedback Techniques: Towards RLHF

Once you get good at collecting and using human feedback, you can start exploring more advanced techniques. These can push your LangGraph RAG’s accuracy even further, making it truly exceptional.

Using Feedback for Model Fine-tuning

The ultimate goal for a lot of human feedback is to make your AI models learn directly from your insights. When you collect enough high-quality human feedback, especially detailed comments and scores, you create a valuable dataset. This dataset can then be used to fine-tune your underlying Large Language Models (LLMs).

Fine-tuning means taking an existing LLM and training it further on your specific feedback data. This teaches the model to generate answers that better align with human preferences and expectations for your particular task. It’s like giving your AI helper specialized training after its basic schooling. This process is a foundational component of Reinforcement Learning from Human Feedback (RLHF).

RLHF is a super advanced way to train AI. Instead of just showing the AI good examples, you teach it by giving it “rewards” (positive feedback) for good behavior and “penalties” (negative feedback) for bad behavior. The AI then learns to maximize its rewards, making it produce answers that humans prefer. This is how many of today’s best AI models have become so impressive, by constantly learning from human choices and preferences.

Automated Evaluation with Human-Annotated Datasets

Imagine you’ve given feedback on hundreds or thousands of your LangGraph RAG’s answers. You now have a “gold standard” dataset: questions paired with human-approved answers, or questions paired with answers marked as good/bad. This dataset is extremely powerful.

You can use this human-annotated dataset to perform automated evaluations. This means that every time you make a change to your LangGraph RAG, you can quickly run it against this dataset. The system can then automatically check how many of its answers match the human-approved ones or how many achieve a high feedback score. This allows you to test new versions of your RAG much faster than waiting for new human feedback.

This automated testing helps you quickly see if your improvements actually made things better, or if they accidentally introduced new problems. It’s like having an automatic quality control checker that uses what humans have already taught the AI.

Practical Example: Improving a LangGraph RAG Agent with Human Feedback

Let’s walk through a concrete example. Imagine you’ve built a LangGraph RAG agent designed to answer customer questions about your company’s complex product manuals.

Scenario: Product Manual RAG Agent

Your RAG agent uses a vector store filled with all your product documentation. Customers ask questions like “How do I troubleshoot error code P201?” or “What are the dimensions of model XYZ?” The LangGraph flow retrieves relevant sections from the manuals and then generates an answer.

Problem: User Complaints and Inaccurate Answers

After deploying your agent, you start hearing complaints. Some users say the answers are irrelevant, not addressing their specific question. Others report that the answers miss crucial details, especially when the relevant information is buried in a long manual section.

LangSmith Setup for Feedback

You decide to implement LangSmith human feedback loops.

  1. Tracing: You ensure your LangGraph application is integrated with LangSmith, so every customer query and the agent’s response is traced automatically.

    1
    2
    3
    4
    5
    6
    
    import os
    # Make sure these are set in your environment
    os.environ["LANGCHAIN_TRACING_V2"] = "true"
    os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
    os.environ["LANGCHAIN_API_KEY"] = "YOUR_ACTUAL_LANGSMITH_API_KEY"
    os.environ["LANGCHAIN_PROJECT"] = "Product Manual RAG" # A clear project name
    
  2. Annotation Queues: In LangSmith, you create two custom feedback types and corresponding annotation queues:

    • “Answer Relevance”: A score from 1 (irrelevant) to 5 (perfectly relevant).
    • “Detail Coverage”: A simple “thumbs up” (all details included) or “thumbs down” (missing key details). You ask your support team to review a sample of agent responses daily using these feedback types in the annotation queue.

Feedback Collection

Your support team starts reviewing RAG responses.

  • For a question like “How to fix P201 error?”, if the RAG answers with information about “P301 error”, it gets an “Answer Relevance” score of 1 and “Detail Coverage” thumbs down.
  • If it gives a generally correct answer but misses a critical safety warning from the manual, it gets a “Detail Coverage” thumbs down and perhaps a “3” for “Answer Relevance.”

Analysis in LangSmith

After a week, you have a good amount of feedback. You go to your LangSmith dashboard and filter runs with low “Answer Relevance” scores or “thumbs down” on “Detail Coverage.”

  • Observation 1: Many traces with low “Answer Relevance” scores show that the “retrieve” node pulled up documents about similar but incorrect product models or error codes. This indicates a retrieval problem.
  • Observation 2: Many “thumbs down” for “Detail Coverage” occur when the question requires specific details from very long document sections. You notice that the LLM often summarizes too broadly, losing specific instructions, even if the correct section was retrieved. This suggests a generation problem and potentially an issue with how the context is provided.

Actionable Insights & Improvement

Based on your analysis, you devise a two-pronged improvement plan for your LangGraph RAG.

  1. Improve Retrieval for Better Relevance:
    • Insight: Irrelevant retrievals.
    • Action: You decide to enhance your vector store’s indexing by using a more sophisticated text splitter. Instead of simple character splitting, you implement a semantic text splitter which breaks documents into chunks based on meaning, hoping to create more coherent and relevant chunks. This helps the retriever pull up truly relevant pieces. You refer to LangChain Semantic Text Splitter: Chunk by Meaning for Better RAG.
    • You also explore using a hybrid search (LangChain Weaviate Hybrid Search for Scalable RAG) in your vector store to combine keyword exactness with semantic understanding.
    • You might also fine-tune your query expansion node in LangGraph to generate several rephrased queries before retrieving, covering more search angles.
  2. Refine Generation for Better Detail Coverage:
    • Insight: Missing key details, especially from long contexts.
    • Action: You modify the prompt for the “generate” node in your LangGraph. You add a specific instruction: “Carefully read all provided documents. Ensure your answer is comprehensive and includes all critical steps and warnings from the context related to the user’s question. Do not skip details.”
    • You also consider adding an intermediate “summarize_long_documents” node in your LangGraph, perhaps using a custom output parser, to pre-process very long retrieved documents into a concise summary before passing them to the final generation step. This helps prevent the main LLM from being overwhelmed and ensures key details are highlighted. You recall LangChain Custom Output Parser Tutorial for ideas.

    Here’s an example of how a modified generation prompt might look in your LangGraph code:

    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
    
    def generate_improved(state: GraphState):
        print("---GENERATE (Improved)---")
        question = state.question
        documents = state.documents
            
        # New, more explicit prompt instructions
        prompt_template = """
        You are an expert technical support assistant.
        Based on the following product manual documents, answer the user's question thoroughly and accurately.
        Ensure you extract ALL relevant steps, warnings, and details directly from the provided text.
        Do not invent information. If the answer is not in the documents, state that clearly.
    
        Documents:
        {documents_content}
    
        Question: {question}
    
        Detailed Answer:
        """
            
        documents_content = "\n\n".join([doc.page_content for doc in documents])
        final_prompt = prompt_template.format(documents_content=documents_content, question=question)
            
        answer = llm.invoke(final_prompt)
        return {"answer": answer}
    
    # You would then update your workflow to use generate_improved
    # workflow.add_node("generate", generate_improved)
    

After implementing these changes, you deploy the updated LangGraph RAG. You then restart the human feedback loop, asking your support team to continue evaluating responses. You expect to see improved “Answer Relevance” scores and fewer “thumbs down” for “Detail Coverage,” proving the effectiveness of your human-in-the-loop evaluation.

Conclusion

Using LangSmith human feedback loops is like giving your LangGraph RAG system a personal tutor. By providing clear, consistent feedback – from simple thumbs up/down to detailed comments and scores – you directly guide your AI to better accuracy. This human-in-the-loop evaluation is essential for building trustworthy and highly effective RAG applications.

Remember, improving your RAG is an ongoing journey. With LangSmith, you have the tools to continuously monitor, evaluate, and refine your LangGraph RAG, ensuring it keeps getting smarter and more helpful over time. Start collecting feedback today and watch your AI helper transform!

Leave a comment