How to Integrate OpenAI for lending with Pinecone for multi-agent systems
OpenAI for lending gives you the reasoning layer for credit workflows: document extraction, borrower Q&A, underwriting support, and policy-aware decisions. Pinecone gives you persistent semantic memory across agents, so your underwriting agent, compliance agent, and servicing agent can share context without stuffing everything into prompts.
Used together, they unlock multi-agent lending systems that can retrieve borrower history, loan policies, and case notes in milliseconds, then pass that context into OpenAI-powered agents for structured decisions and explanations.
Prerequisites
- •Python 3.10+
- •An OpenAI API key with access to the lending-capable model/workflow you’re using
- •A Pinecone API key and an index created in your Pinecone project
- •
pipinstalled - •Basic familiarity with:
- •embeddings
- •vector search
- •tool-calling or agent orchestration
Install the SDKs:
pip install openai pinecone
Set environment variables:
export OPENAI_API_KEY="your-openai-key"
export PINECONE_API_KEY="your-pinecone-key"
export PINECONE_INDEX_NAME="lending-agents"
Integration Steps
- •Initialize OpenAI and Pinecone clients
Use OpenAI for generating embeddings and running the lending agent logic. Use Pinecone as the shared retrieval layer for all agents in the system.
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"])
index_name = os.environ["PINECONE_INDEX_NAME"]
index = pc.Index(index_name)
- •Embed lending documents and store them in Pinecone
In a lending system, your corpus usually includes policy docs, underwriting rules, KYC notes, loan product terms, and prior case summaries. Generate embeddings with OpenAI and upsert them into Pinecone with metadata that helps agents filter by document type.
documents = [
{
"id": "policy-001",
"text": "Debt-to-income ratio must be below 43% for standard personal loans.",
"metadata": {"type": "policy", "product": "personal_loan"}
},
{
"id": "case-101",
"text": "Applicant has stable employment for 4 years and no missed payments in the last 24 months.",
"metadata": {"type": "case_note", "customer_id": "cust_101"}
}
]
texts = [doc["text"] for doc in documents]
embeddings_response = openai_client.embeddings.create(
model="text-embedding-3-small",
input=texts
)
vectors = []
for doc, item in zip(documents, embeddings_response.data):
vectors.append({
"id": doc["id"],
"values": item.embedding,
"metadata": {
**doc["metadata"],
"text": doc["text"]
}
})
index.upsert(vectors=vectors)
- •Retrieve relevant context for each agent
A multi-agent setup works best when each agent gets only the context it needs. Query Pinecone with a borrower question or case summary, then feed the top matches into your underwriting or compliance agent.
query = "Can this borrower qualify for a personal loan if their debt-to-income ratio is 41%?"
query_embedding = openai_client.embeddings.create(
model="text-embedding-3-small",
input=query
).data[0].embedding
results = index.query(
vector=query_embedding,
top_k=3,
include_metadata=True
)
matches = results["matches"]
for match in matches:
print(match["id"], match["score"], match["metadata"]["text"])
- •Call OpenAI to generate a lending decision draft
Now pass retrieved context into OpenAI. In production, this should produce a structured recommendation: approve, deny, or review manually.
context_text = "\n".join(
f"- {m['metadata']['text']}" for m in matches
)
prompt = f"""
You are a lending assistant.
Use the retrieved policy and case context to draft a decision recommendation.
Borrower question:
{query}
Context:
{context_text}
Return:
1. Decision
2. Reasoning
3. Any missing information needed
"""
response = openai_client.responses.create(
model="gpt-4o-mini",
input=prompt
)
print(response.output_text)
- •Wire both tools into a simple multi-agent loop
A practical pattern is:
- •retrieval agent: queries Pinecone
- •underwriting agent: reasons over retrieved context with OpenAI
- •compliance agent: checks policy alignment with the same retrieved evidence
def retrieve_context(question: str) -> str:
emb = openai_client.embeddings.create(
model="text-embedding-3-small",
input=question
).data[0].embedding
res = index.query(vector=emb, top_k=5, include_metadata=True)
return "\n".join([m["metadata"]["text"] for m in res["matches"]])
def underwriting_agent(question: str) -> str:
context = retrieve_context(question)
result = openai_client.responses.create(
model="gpt-4o-mini",
input=f"""
You are an underwriting agent.
Question: {question}
Evidence:
{context}
Produce a concise recommendation.
"""
)
return result.output_text
decision = underwriting_agent("Borrower has 41% DTI and stable income for 4 years.")
print(decision)
Testing the Integration
Run a smoke test that inserts one policy record, retrieves it, then asks OpenAI to summarize whether the policy applies.
test_query = "What is the maximum debt-to-income ratio allowed?"
emb = openai_client.embeddings.create(
model="text-embedding-3-small",
input=test_query
).data[0].embedding
res = index.query(vector=emb, top_k=1, include_metadata=True)
context = res["matches"][0]["metadata"]["text"]
answer = openai_client.responses.create(
model="gpt-4o-mini",
input=f"Question: {test_query}\nContext: {context}\nAnswer briefly."
)
print("Retrieved:", context)
print("Model answer:", answer.output_text)
Expected output:
Retrieved: Debt-to-income ratio must be below 43% for standard personal loans.
Model answer: The maximum allowed debt-to-income ratio is below 43% for standard personal loans.
Real-World Use Cases
- •
Underwriting copilots
- •Retrieve borrower history, policy clauses, and prior decisions from Pinecone.
- •Use OpenAI to draft an approval recommendation with supporting rationale.
- •
Compliance review agents
- •One agent retrieves regulatory or internal policy references.
- •Another agent checks whether the proposed loan action violates any stored rule.
- •
Servicing and collections assistants
- •Store customer interaction history in Pinecone.
- •Let OpenAI generate next-best-action responses based on account context and repayment policies.
The pattern is simple: Pinecone holds durable shared memory, while OpenAI handles reasoning and generation. For lending workflows with multiple agents, that separation keeps your system faster to debug and easier to control.
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