How to Integrate OpenAI for payments with Pinecone for startups
If you’re building an AI agent for a startup, the useful pattern is simple: use OpenAI to generate the decisioning or payment-related reasoning, and Pinecone to store and retrieve the context that makes those decisions reliable. That gives you an agent that can answer billing questions, match customers to invoices, and pull the right transaction history before it takes action.
The practical win is state. OpenAI handles the language and workflow logic; Pinecone gives you fast semantic retrieval over receipts, invoices, policy docs, payment notes, and support history.
Prerequisites
- •Python 3.10+
- •An OpenAI API key
- •A Pinecone API key
- •A Pinecone index created ahead of time
- •
pipinstalled - •Basic familiarity with async Python
- •Installed packages:
- •
openai - •
pinecone - •
python-dotenv
- •
pip install openai pinecone python-dotenv
Integration Steps
- •Set your environment variables.
Use a .env file so you don’t hardcode credentials.
from dotenv import load_dotenv
import os
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
PINECONE_INDEX_NAME = os.getenv("PINECONE_INDEX_NAME", "startup-payments")
- •Initialize both clients.
This uses the current OpenAI Python SDK and Pinecone client.
from openai import OpenAI
from pinecone import Pinecone
openai_client = OpenAI(api_key=OPENAI_API_KEY)
pc = Pinecone(api_key=PINECONE_API_KEY)
index = pc.Index(PINECONE_INDEX_NAME)
- •Create embeddings for payment-related records and store them in Pinecone.
For startups, this is usually invoices, subscription notes, refund reasons, chargeback evidence, or payment support tickets.
payment_docs = [
{
"id": "inv_1001",
"text": "Invoice 1001 paid by customer Acme Corp on 2024-11-02. Amount $240.00. Status: settled."
},
{
"id": "refund_2007",
"text": "Refund request from customer Beta Labs for duplicate charge on card ending 4242."
},
]
texts = [doc["text"] for doc in payment_docs]
embeddings_response = openai_client.embeddings.create(
model="text-embedding-3-small",
input=texts,
)
vectors = []
for doc, emb in zip(payment_docs, embeddings_response.data):
vectors.append({
"id": doc["id"],
"values": emb.embedding,
"metadata": {"text": doc["text"]}
})
index.upsert(vectors=vectors)
- •Query Pinecone with a user’s payment question, then pass the retrieved context to OpenAI.
This is the core pattern: retrieve first, then generate a grounded response.
query = "Did Acme Corp pay invoice 1001?"
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,
)
context_chunks = [
match["metadata"]["text"]
for match in results["matches"]
]
prompt = f"""
You are a payments assistant for a startup.
Answer using only the context below.
Context:
{chr(10).join(context_chunks)}
Question: {query}
"""
response = openai_client.responses.create(
model="gpt-4.1-mini",
input=prompt,
)
print(response.output_text)
- •Add a payment action layer if your agent needs to trigger workflows.
If your product also processes payments through Stripe or another processor, keep OpenAI as the reasoning layer and call your payment provider separately after retrieval confirms intent. Don’t let the model directly “guess” transaction state.
def get_payment_context(question: str):
q_emb = openai_client.embeddings.create(
model="text-embedding-3-small",
input=[question],
).data[0].embedding
res = index.query(vector=q_emb, top_k=2, include_metadata=True)
return [m["metadata"]["text"] for m in res["matches"]]
def build_answer(question: str):
context = get_payment_context(question)
prompt = f"Context:\n{chr(10).join(context)}\n\nQuestion: {question}"
result = openai_client.responses.create(
model="gpt-4.1-mini",
input=prompt,
)
return result.output_text
print(build_answer("Was invoice 1001 settled?"))
Testing the Integration
Run a simple retrieval-plus-generation test against a known record.
test_question = "What is the status of invoice 1001?"
answer = build_answer(test_question)
print("QUESTION:", test_question)
print("ANSWER:", answer)
Expected output:
QUESTION: What is the status of invoice 1001?
ANSWER: Invoice 1001 was paid by Acme Corp on 2024-11-02 and is marked as settled.
If you get a vague answer, check these first:
- •Your Pinecone index contains the expected vectors
- •The embedding model matches what you used during upsert/query
- •The retrieved metadata actually includes the payment text
- •Your prompt tells OpenAI to answer only from retrieved context
Real-World Use Cases
- •Payment support agent
- •Retrieve invoice history, failed charge notes, and refund records before generating a response to customers.
- •Billing reconciliation assistant
- •Match internal ledger notes against customer-facing records using semantic search over transaction descriptions.
- •Dispute triage workflow
- •Pull relevant evidence from stored receipts, delivery confirmations, and support logs before drafting dispute responses.
The production pattern here is stable: embed your payment knowledge into Pinecone, retrieve only what matters, then let OpenAI turn that context into an action or answer. For startups, that’s enough to ship a useful payments agent without building a brittle rules engine first.
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