Home / Articles

Agentic AI with LangChain — Part 5: Agentic RAG with ReAct and

Part 5 of agentic AI with LangChain: agentic RAG where a ReAct agent decides when to retrieve and a reflection step checks the answer before it's returned.

Publisher: REACTAPP.TOP9 min read2059 words

This article is published in English.

Sources: Agentic AI with LangChain — Part 5: Agentic RAG with ReAct and Reflection

Use this as an operator-facing rebuild of the ideas in “Agentic AI with LangChain — Part 5: Agentic RAG with ReAct and Reflection”: clear stages, ordered code slots, and recovery notes that survive a handoff. Overview works best when treated as a measurable surface. Capture one golden transcript, one failure case, and the rollback note before expanding scope. Prefer small, testable units over sprawling scripts. When a step fails, the failure should point at a single responsibility rather than a tangled pipeline.

Technical Concepts

For Technical Concepts, define the inputs, the owner of the step, and the exit criteria before changing code. Operators should be able to re-run the step from a known checkpoint without guessing hidden state. Treat this stage as a contract between inputs and validated outputs. Name the artifacts, define success checks, and refuse silent partial completion. Cite the passages that actually grounded the answer. Without citations, operators cannot tell hallucination from an indexing gap.

1. System Configurations

For 1. System Configurations, define the inputs, the owner of the step, and the exit criteria before changing code. Operators should be able to re-run the step from a known checkpoint without guessing hidden state. Record timings and token or query cost next to functional results. Cost visibility early prevents surprise bills when the path moves from demo to shared environments. Cite the passages that actually grounded the answer. Without citations, operators cannot tell hallucination from an indexing gap.

OPENAI_API_KEY="<Your OpenAI API Key>"
VECTOR_DB=pinecone
PINECONE_API_KEY=<your Pinecone API Key>
PINECONE_ENVIRONMENT=gcp-starter
LANGSMITH_TRACING=true
LANGSMITH_ENDPOINT="https://api.smith.langchain.com"
LANGSMITH_API_KEY="<Your LangSmith API Key"
LANGSMITH_PROJECT="agent-ai"
TAVILY_API_KEY=<Your TAVILY API Key>
class BaseConfig(BaseSettings):
    OPENAI_API_KEY: Optional[str]
    PINECONE_API_KEY: Optional[str]
    TAVILY_API_KEY: Optional[str]

model_config = SettingsConfigDict(env_file=".env", extra="ignore")

2. Project Structure

For 2. Project Structure, define the inputs, the owner of the step, and the exit criteria before changing code. Operators should be able to re-run the step from a known checkpoint without guessing hidden state. Keep configuration outside application code. Environment files, secret stores, and feature flags belong in one place operators can audit without reading the whole graph. Cite the passages that actually grounded the answer. Without citations, operators cannot tell hallucination from an indexing gap.

project/
├── rag_process/       # Pinecone RAG operations
│   ├── __init__.py
│   ├── extractor.py
|   └── service.py
│
├── graphs             # implement ReAct and Reflection StateGraph
│   ├── nodes.py       # graph nodes. Defined as Python functions
│   ├── graph.py       # graph workflow
│   ├── consts.py      # define node names as constants
│   └── state.py       # typed dictionary to store graph states
|
|
├── tools              # defines Pinecone retriever and get_weather tools
│   ├── retriever_tools.py    # Python function tool to retrieve Pinecone RAG
|   └── weather.py        # typed dictionary to store graph states
|
|── schemas.py          # pydantic data models
|── upload.py           # python functions to upload files asynchronously
├── main.py             # fastAPI app
├── config.py           # pydantic BaseConfig
│── dependencies.py     # dependency inject to fastAPI endpoint function
├── .env                # environment variable definition
├── client.py           # streamlit client
├── .gitignore
└── requirements.txt     # package requirements

3. ReAct Pattern Integration

For 3. ReAct Pattern Integration, define the inputs, the owner of the step, and the exit criteria before changing code. Operators should be able to re-run the step from a known checkpoint without guessing hidden state. Document the happy path and the recovery path together. Retries, human gates, and dead-letter handling are part of the product, not later polish. Cite the passages that actually grounded the answer. Without citations, operators cannot tell hallucination from an indexing gap.

3.1. RAG Retrieval Tool

For 3.1. RAG Retrieval Tool, define the inputs, the owner of the step, and the exit criteria before changing code. Operators should be able to re-run the step from a known checkpoint without guessing hidden state. Prefer small, testable units over sprawling scripts. When a step fails, the failure should point at a single responsibility rather than a tangled pipeline. Cite the passages that actually grounded the answer. Without citations, operators cannot tell hallucination from an indexing gap.

@tool
async def search_agentic_docs(query: str) -> str:
    """
    Good for questions about generative agents, prompt engineering, and adversarial attacks.
    Pass a natural language search query to retrieve context from the agentic ai database.
    """
    # Simply invoke your existing vector service retriever
    docs = await vector_service.retriever.ainvoke(query)

    # Flatten the document content into a single string for the LLM
    return "\n\n".join([d.page_content for d in docs])

3.2. GraphState

For 3.2. GraphState, define the inputs, the owner of the step, and the exit criteria before changing code. Operators should be able to re-run the step from a known checkpoint without guessing hidden state. Treat this stage as a contract between inputs and validated outputs. Name the artifacts, define success checks, and refuse silent partial completion. Cite the passages that actually grounded the answer. Without citations, operators cannot tell hallucination from an indexing gap.

class GraphState(TypedDict):
    """
    Represents the state of our graphs.

    Attributes:
        question: question
        generation: LLM generation
        acceptable: whether the answer is qualified to be acceptable
        messages: list of messages
    """

    question: str
    generation: str
    acceptable: bool
    messages: Annotated[list[AnyMessage], add_messages]
    loop_count: int

3.3. ReAct Nodes

For 3.3. ReAct Nodes, define the inputs, the owner of the step, and the exit criteria before changing code. Operators should be able to re-run the step from a known checkpoint without guessing hidden state. Record timings and token or query cost next to functional results. Cost visibility early prevents surprise bills when the path moves from demo to shared environments. Cite the passages that actually grounded the answer. Without citations, operators cannot tell hallucination from an indexing gap.

4. Reflection Pattern Integration

For 4. Reflection Pattern Integration, define the inputs, the owner of the step, and the exit criteria before changing code. Operators should be able to re-run the step from a known checkpoint without guessing hidden state. Keep configuration outside application code. Environment files, secret stores, and feature flags belong in one place operators can audit without reading the whole graph. Cite the passages that actually grounded the answer. Without citations, operators cannot tell hallucination from an indexing gap. For 4. Reflection Pattern Integration, define the inputs, the owner of the step, and the exit criteria before changing code. Operators should be able to re-run the step from a known checkpoint without guessing hidden state. Prefer small, testable units over sprawling scripts. When a step fails, the failure should point at a single responsibility rather than a tangled pipeline.

4.1. How Reflection Works

When working through 4.1. How Reflection Works, write down the contract first: required inputs, success signal, and what happens on partial failure. That checklist keeps later code changes honest. Treat this stage as a contract between inputs and validated outputs. Name the artifacts, define success checks, and refuse silent partial completion. Measure recall on a fixed question set before tuning prompts. Prompt churn rarely fixes a weak retrieval surface.

4.2. Prompts

When working through 4.2. Prompts, write down the contract first: required inputs, success signal, and what happens on partial failure. That checklist keeps later code changes honest. Record timings and token or query cost next to functional results. Cost visibility early prevents surprise bills when the path moves from demo to shared environments. Cache stable system instructions and tool schemas. Re-sending identical preamble is a common source of burn.

GENERATE_SYSTEM_PROMPT = f"""You are an assistant for question-answering tasks with access to multiple tools.
    use these tools to retrieve context to answer the question. If you don't know the answer,
    just say that you don't know. Use three sentences maximum and keep the answer concise.

    Tool Use Instructions:

    - Use tools as much as possible.
    - Chain tools when needed (e.g., search for info, then use that to search
      for relevant hotels)
    - Be conversational and helpful
    - If you choose to answer a question using your internal knowledge instead of a tool,
      you must be completely certain of the facts. If there is any ambiguity,
      or if the question requires up-to-date or highly specific details, you MUST use a tool to verify.

    """
GRADER_SYSTEM_PROMPT = """""You are a grader assessing whether an LLM generation addresses/resolves a
      question using the context. Generate critique and recommendations for LLM that generated the answer.
      always provide detailed recommendations, including requests for search more relevant context, generate
      answer based on context to reduce hallucination, and generate answer relevant to the question.

      The LLM generating this answer has the access to the following tools to obtain context:
      **get_weather**: search for weather information given a city name
      **search_agentic_docs**:  Good for questions about generative agents, prompt engineering, and adversarial attacks
      **TavilySearch**: General search tool from the internet.

      INSTRUCTIONS:
      (a) Evaluate if the context is relevant to the question
      Provide recommendation if context is not relevant to the question. You can suggest the LLM to use an
      appropriate tool. If there is no context, you must be completely certain of the facts. If the question is
      covered by provided tools, or there is any ambiguity,
      or if the question requires up-to-date or highly specific details, you MUST suggest to use a tool to verify answer.

      (b) Evaluate if the answer is based on the context
      if the answer is not based on the context, it is a hallucination. You should suggest to regenerate the answer
      based on the context.
      (c) Evaluate if the answer resolves the question
      if the answer does not resolves the question, suggest to regenerate the answer

      The LLM generated this answer is strictly instructed to keep its answer CONCISE (maximum of 3 sentences).
      Do NOT penalize the answer for omitting minor details if the core question is answered accurately.
      If all the evaluations are passed, no suggestions should be provided.

      CRITERIA FOR ACCEPTABLE (True):
      - If the answer is accurate, addresses the user's question using the context, and does not hallucinate,
        you MUST set acceptable to True, even if it missed some details from the context.
      - Only set acceptable to False if there is a fundamental flaw (hallucination, wrong information,
        or completely missing the point).

      OUTPUT REQUIREMENTS:

        You must return a structured output with the following fields:

        - acceptable (boolean): True if the answer passed all evaluation,
          False if any of the evaluation fails and recommendations are provided
        - recommendations (string): recommendations you provide

        INPUTS:

        question: {question}
        answer: {answer}
        context: {context}
    """

4.3. Reflection Nodes

When working through 4.3. Reflection Nodes, write down the contract first: required inputs, success signal, and what happens on partial failure. That checklist keeps later code changes honest. Keep configuration outside application code. Environment files, secret stores, and feature flags belong in one place operators can audit without reading the whole graph. Measure recall on a fixed question set before tuning prompts. Prompt churn rarely fixes a weak retrieval surface. When working through 4.3. Reflection Nodes, write down the contract first: required inputs, success signal, and what happens on partial failure. That checklist keeps later code changes honest. Prefer small, testable units over sprawling scripts. When a step fails, the failure should point at a single responsibility rather than a tangled pipeline.

5. Full Workflow

  1. Full Workflow works best when treated as a measurable surface. Capture one golden transcript, one failure case, and the rollback note before expanding scope. Treat this stage as a contract between inputs and validated outputs. Name the artifacts, define success checks, and refuse silent partial completion. Separate chunking policy from retrieval policy. Changing one should not force a rewrite of the other when quality metrics move.

6. Agency-Reliability Tradeoff

7. Closing Summary

Operational checklist