How to Integrate OpenAI for pension funds with Pinecone for AI agents
Combining OpenAI for pension funds with Pinecone gives you a practical agent stack for retrieval-heavy workflows. You use OpenAI to reason over policy, member data, and operational instructions, then Pinecone to store and retrieve the right context fast enough for production agents.
For pension operations, this is useful when the answer depends on documents: scheme rules, contribution policies, retirement options, transfer guidance, and internal SOPs. The agent does not guess; it retrieves the right evidence from Pinecone and passes it into OpenAI for a grounded response.
Prerequisites
- •Python 3.10+
- •An OpenAI API key
- •A Pinecone API key
- •A Pinecone index created in advance
- •A folder of pension-related documents or text chunks to embed
- •
pipinstalled
Install the SDKs:
pip install openai pinecone
Set environment variables:
export OPENAI_API_KEY="your-openai-key"
export PINECONE_API_KEY="your-pinecone-key"
Integration Steps
- •Initialize both clients and define your embedding model.
Use OpenAI embeddings for vector generation and Pinecone for storage and retrieval. Keep the embedding model fixed across indexing and querying.
import os
from openai import OpenAI
from pinecone import Pinecone
openai_client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
EMBED_MODEL = "text-embedding-3-small"
INDEX_NAME = "pension-fund-docs"
- •Create or connect to a Pinecone index.
For production, create the index once during deployment. For local validation, you can check whether it exists before using it.
from pinecone import ServerlessSpec
existing_indexes = [idx["name"] for idx in pc.list_indexes()]
if INDEX_NAME not in existing_indexes:
pc.create_index(
name=INDEX_NAME,
dimension=1536,
metric="cosine",
spec=ServerlessSpec(cloud="aws", region="us-east-1")
)
index = pc.Index(INDEX_NAME)
- •Embed pension content and upsert it into Pinecone.
Chunk your documents before embedding them. In this example, we use short policy snippets to keep the code focused on the integration path.
documents = [
{
"id": "doc-001",
"text": "Members may retire from age 55 subject to scheme rules and tax legislation.",
"metadata": {"source": "retirement-policy", "section": "eligibility"}
},
{
"id": "doc-002",
"text": "Transfers out require identity verification and a completed discharge form.",
"metadata": {"source": "transfer-policy", "section": "process"}
},
{
"id": "doc-003",
"text": "Contribution changes are processed monthly after payroll cutoff.",
"metadata": {"source": "contributions", "section": "operations"}
}
]
vectors = []
for doc in documents:
emb = openai_client.embeddings.create(
model=EMBED_MODEL,
input=doc["text"]
)
vectors.append({
"id": doc["id"],
"values": emb.data[0].embedding,
"metadata": {**doc["metadata"], "text": doc["text"]}
})
index.upsert(vectors=vectors)
- •Query Pinecone with an embedded user question.
Your agent should retrieve top matches before calling the chat model. That keeps the answer tied to indexed pension content instead of free-form speculation.
query_text = "At what age can a member retire?"
query_emb = openai_client.embeddings.create(
model=EMBED_MODEL,
input=query_text
)
results = index.query(
vector=query_emb.data[0].embedding,
top_k=3,
include_metadata=True
)
matches = results["matches"]
for match in matches:
print(match["id"], match["score"], match["metadata"]["text"])
- •Pass retrieved context into OpenAI for a grounded answer.
This is the core agent pattern: retrieve first, then generate. Keep the prompt strict so the model only answers from supplied context.
context = "\n".join(
f"- {m['metadata']['text']}"
for m in matches
)
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": (
"You are a pension operations assistant. "
"Answer only using the provided context. "
"If the context is insufficient, say so."
)
},
{
"role": "user",
"content": f"Question: {query_text}\n\nContext:\n{context}"
}
]
)
print(response.choices[0].message.content)
Testing the Integration
Run a simple end-to-end test that embeds a query, retrieves from Pinecone, and generates an answer from OpenAI.
test_question = "What happens when someone requests a transfer out?"
q_emb = openai_client.embeddings.create(
model=EMBED_MODEL,
input=test_question
)
search = index.query(
vector=q_emb.data[0].embedding,
top_k=2,
include_metadata=True
)
ctx = "\n".join([m["metadata"]["text"] for m in search["matches"]])
answer = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Answer only from context."},
{"role": "user", "content": f"{test_question}\n\nContext:\n{ctx}"}
]
)
print("Retrieved:", len(search["matches"]))
print("Answer:", answer.choices[0].message.content)
Expected output:
Retrieved: 2
Answer: Transfers out require identity verification and a completed discharge form.
Real-World Use Cases
- •Member service agents that answer pension policy questions using indexed scheme rules and internal procedures.
- •Operations copilots that help staff process transfers, contribution changes, retirement requests, and document checks.
- •Compliance assistants that retrieve approved policy text before generating responses for audit-sensitive workflows.
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