How to Build a transaction monitoring Agent Using LlamaIndex in Python for pension funds
A transaction monitoring agent for pension funds watches contribution, benefit, transfer, and investment activity for patterns that look inconsistent with policy, regulation, or member behavior. It matters because pension operations are high-trust, high-volume, and audit-heavy: missing a suspicious transfer, duplicate payment, or policy breach creates compliance exposure fast.
Architecture
- •
Data ingestion layer
- •Pulls transactions from core pension admin systems, custodians, payment rails, and file drops.
- •Normalizes records into a single schema with member ID, account ID, transaction type, amount, timestamp, and jurisdiction.
- •
Policy and controls knowledge base
- •Stores fund rules, AML/KYC policies, contribution limits, transfer restrictions, and exception procedures.
- •Indexed with LlamaIndex so the agent can ground decisions in current policy text.
- •
Transaction retrieval index
- •Holds recent transactions and historical cases.
- •Used to compare a new event against prior similar events and known suspicious patterns.
- •
Reasoning and triage agent
- •Uses LlamaIndex tools to retrieve policy context and transaction history.
- •Produces a risk assessment, explanation, and recommended action: clear, review, or escalate.
- •
Case management output
- •Writes alerts into a queue or case system with evidence links.
- •Keeps an immutable audit trail for compliance review.
- •
Controls layer
- •Enforces data residency, redaction of PII where needed, role-based access, and human approval for escalations.
Implementation
1) Build indexes for policy documents and transaction history
For pension funds, keep policy text separate from raw transaction records. That lets you update controls without rebuilding everything else.
from llama_index.core import Document
from llama_index.core import VectorStoreIndex
from llama_index.core import Settings
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI
Settings.llm = OpenAI(model="gpt-4o-mini")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")
policy_docs = [
Document(text="""
Pension fund transfer policy:
- Transfers above $50,000 require manual review.
- Any transfer to an unverified destination must be escalated.
- Benefit payments must match approved payroll schedules.
- Contribution reversals require documented approval.
""", metadata={"source": "policy_transfer_v3"}),
]
transaction_docs = [
Document(
text="txn_id=T1001 member=88321 type=transfer amount=78000 destination=external_bank status=posted",
metadata={"txn_id": "T1001", "member_id": "88321", "type": "transfer"}
),
Document(
text="txn_id=T1002 member=88321 type=contribution amount=1200 source=employer status=posted",
metadata={"txn_id": "T1002", "member_id": "88321", "type": "contribution"}
),
]
policy_index = VectorStoreIndex.from_documents(policy_docs)
txn_index = VectorStoreIndex.from_documents(transaction_docs)
policy_retriever = policy_index.as_retriever(similarity_top_k=2)
txn_retriever = txn_index.as_retriever(similarity_top_k=3)
2) Expose retrieval as tools
LlamaIndex agents work well when you keep tools narrow. One tool should answer “what does policy say?”, another should answer “what similar transactions exist?”
from llama_index.core.tools import QueryEngineTool
from llama_index.core.query_engine import RetrieverQueryEngine
policy_engine = RetrieverQueryEngine.from_args(policy_retriever)
txn_engine = RetrieverQueryEngine.from_args(txn_retriever)
policy_tool = QueryEngineTool.from_defaults(
query_engine=policy_engine,
name="policy_lookup",
description="Look up pension fund policies, thresholds, and escalation rules."
)
txn_tool = QueryEngineTool.from_defaults(
query_engine=txn_engine,
name="transaction_lookup",
description="Find similar transactions and historical examples."
)
3) Create the monitoring agent and run a risk review
This pattern gives you explainability: the agent cites retrieved policy snippets and prior transactions before making a recommendation.
from llama_index.core.agent import ReActAgent
agent = ReActAgent.from_tools(
tools=[policy_tool, txn_tool],
llm=Settings.llm,
verbose=True,
)
alert_input = """
Review this transaction:
txn_id=T2009 member=99102 type=transfer amount=78000 destination=external_bank status=pending
Determine whether it should be cleared or escalated.
Return:
- risk_level
- reason
- recommended_action
- evidence
"""
response = agent.chat(alert_input)
print(response)
4) Turn the response into a case record
In production you do not want free-form output only. Parse the result into a structured case payload for downstream systems.
import json
case_payload = {
"case_type": "transaction_monitoring",
"source_txn_id": "T2009",
"result_text": str(response),
}
# Example: send to your case system / queue here
print(json.dumps(case_payload, indent=2))
Production Considerations
- •
Data residency
- •Keep indexes in-region if your pension fund operates under local residency requirements.
- •If using managed LLM APIs, confirm where embeddings and prompts are processed.
- •
Auditability
- •Persist every retrieved document chunk used in a decision.
- •Store the final prompt/response pair plus model version so compliance can reconstruct the alert.
- •
Guardrails
- •Redact account numbers, national IDs, and beneficiary details before sending text to the model when possible.
- •Force structured outputs for risk level and action; do not rely on natural language alone.
- •
Monitoring
- •Track false positives by alert category: transfers, contributions, benefit payments, reversals.
- •Measure retrieval quality separately from model quality; bad retrieval will poison triage even if the model is strong.
Common Pitfalls
- •
Mixing policies with raw transactions in one index
- •This makes retrieval noisy and harder to audit.
- •Keep control documents indexed separately from operational data.
- •
Letting the agent decide without thresholds
- •Pension monitoring needs deterministic rules alongside LLM reasoning.
- •Hard-code escalation thresholds like amount limits and destination verification checks before calling the agent.
- •
Ignoring evidence retention
- •If you cannot show why an alert was raised six months later, the system is weak for compliance use.
- •Save retrieved context IDs, timestamps, model version, and final disposition for every case.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
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