How to Integrate LangGraph for pension funds with LangSmith for RAG

By Cyprian AaronsUpdated 2026-04-22
langgraph-for-pension-fundslangsmithrag

Combining LangGraph for pension funds with LangSmith gives you a clean way to build regulated RAG systems that are traceable end to end. You get graph-based orchestration for pension-specific workflows, plus observability on retrieval, prompts, tool calls, and outputs when auditors or ops teams ask what happened.

This matters when your agent is answering retirement policy questions, benefits queries, or document-heavy support requests. You need deterministic flow control from LangGraph and execution traces from LangSmith so you can debug bad retrievals, measure answer quality, and prove how a response was produced.

Prerequisites

  • Python 3.10+
  • langgraph
  • langchain
  • langchain-openai or another LLM provider package
  • langsmith
  • A LangSmith API key
  • OpenAI API key or equivalent model credentials
  • A vector store for RAG, such as:
    • Chroma
    • FAISS
    • Pinecone
  • Pension fund documents loaded into your retrieval index
  • Basic familiarity with LangGraph state graphs and LangChain retrievers

Install the packages:

pip install langgraph langchain langchain-openai langsmith chromadb

Set environment variables:

export LANGSMITH_API_KEY="lsv2_..."
export LANGSMITH_TRACING="true"
export LANGSMITH_PROJECT="pension-rag"
export OPENAI_API_KEY="sk-..."

Integration Steps

  1. Build the RAG components first.

You want the retriever and LLM working before wiring the graph. This keeps the failure surface small when you start tracing.

from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.documents import Document

docs = [
    Document(page_content="Pension withdrawals require identity verification and benefit eligibility checks."),
    Document(page_content="Retirement age rules vary by plan type and jurisdiction."),
]

splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_documents(docs)

embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(
    documents=chunks,
    embedding=embeddings,
    collection_name="pension_fund_docs",
)

retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
  1. Define graph state and nodes.

For pension fund workflows, keep state explicit. That makes it easier to trace retrieval context, final answers, and any control-flow decisions.

from typing import TypedDict, List
from langchain_core.messages import BaseMessage, HumanMessage, AIMessage

class GraphState(TypedDict):
    question: str
    context: List[str]
    answer: str

def retrieve_context(state: GraphState):
    docs = retriever.invoke(state["question"])
    return {"context": [d.page_content for d in docs]}

def generate_answer(state: GraphState):
    context_text = "\n\n".join(state["context"])
    prompt = (
        "You are a pension fund assistant.\n"
        f"Question: {state['question']}\n"
        f"Context:\n{context_text}\n\n"
        "Answer using only the provided context."
    )
    response = llm.invoke([HumanMessage(content=prompt)])
    return {"answer": response.content}
  1. Wire the workflow with LangGraph.

Use StateGraph, add nodes, connect edges, then compile. This is the orchestration layer that controls how RAG runs for each request.

from langgraph.graph import StateGraph, START, END

graph_builder = StateGraph(GraphState)
graph_builder.add_node("retrieve_context", retrieve_context)
graph_builder.add_node("generate_answer", generate_answer)

graph_builder.add_edge(START, "retrieve_context")
graph_builder.add_edge("retrieve_context", "generate_answer")
graph_builder.add_edge("generate_answer", END)

app = graph_builder.compile()
  1. Turn on LangSmith tracing for visibility.

LangSmith captures runs automatically when tracing is enabled through env vars. For more control in production systems, create explicit traces around graph invocations using traceable.

from langsmith import traceable

@traceable(name="pension_rag_query")
def run_query(question: str):
    result = app.invoke({"question": question, "context": [], "answer": ""})
    return result

result = run_query("What checks are required before a pension withdrawal?")
print(result["answer"])

If you want to tag runs by environment or tenant:

import os

os.environ["LANGSMITH_PROJECT"] = "pension-rag-prod"
os.environ["LANGSMITH_TRACING"] = "true"
  1. Add structured metadata for audits.

For regulated workflows, attach metadata like plan type, jurisdiction, or document version. LangSmith will preserve this in traces if you pass it through your run wrapper or chain components.

from langsmith.run_helpers import traceable

@traceable(name="pension_rag_query", metadata={"domain": "pensions", "workflow": "benefits-support"})
def run_query_with_metadata(question: str):
    return app.invoke({
        "question": question,
        "context": [],
        "answer": ""
    })

print(run_query_with_metadata("What is the retirement age under this plan?"))

Testing the Integration

Run a simple query and confirm two things:

  • The graph returns an answer from retrieved context.
  • The run appears in LangSmith under your project name.
test_question = "What checks are required before a pension withdrawal?"
output = app.invoke({"question": test_question, "context": [], "answer": ""})

print("ANSWER:")
print(output["answer"])

Expected output:

ANSWER:
Pension withdrawals require identity verification and benefit eligibility checks.

In LangSmith, you should see a trace named something like pension_rag_query, with child runs for retrieval and generation if your graph nodes are instrumented through traced components.

Real-World Use Cases

  • Member support agents

    • Answer pension policy questions with citations from internal plan documents.
    • Trace every retrieval step when support teams challenge an answer.
  • Claims and withdrawals assistants

    • Guide users through eligibility checks before submitting withdrawal requests.
    • Use LangSmith traces to inspect failures in retrieval or prompt formatting.
  • Compliance review workflows

    • Route high-risk questions through extra validation nodes in LangGraph.
    • Use LangSmith to audit which documents influenced each response.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides