How to Integrate OpenAI for payments with Pinecone for RAG

By Cyprian AaronsUpdated 2026-04-21
openai-for-paymentspineconerag

Combining OpenAI for payments with Pinecone for RAG gives you a practical pattern for agent systems that need both transactional action and grounded retrieval. You use Pinecone to fetch the right policy, invoice, or customer context, then hand that context to OpenAI to decide whether a payment should be created, confirmed, or rejected.

That matters in finance and insurance because the agent is no longer guessing. It can retrieve the exact billing record, payment terms, or claim instruction before calling the payment workflow.

Prerequisites

  • Python 3.10+
  • openai SDK installed
  • pinecone SDK installed
  • API keys set in environment variables:
    • OPENAI_API_KEY
    • PINECONE_API_KEY
  • A Pinecone index already created with the same embedding dimension you plan to use
  • A payment backend or sandbox API exposed through your agent workflow
  • A document set ready for indexing:
    • invoices
    • payment policies
    • refund rules
    • KYC/AML notes

Install the libraries:

pip install openai pinecone

Integration Steps

1) Initialize OpenAI and Pinecone clients

Start by wiring both clients in one service. Keep them in the same application layer so your agent can retrieve context and act on it without bouncing between unrelated components.

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 = pc.Index("payments-rag")

2) Create embeddings for your payment knowledge base

For RAG, you need vectorized documents. Use OpenAI embeddings for invoice notes, payment policy docs, chargeback rules, or customer-specific instructions.

docs = [
    "Invoice #8821 is payable within 14 days. Late fees apply after day 15.",
    "Refunds above $500 require manager approval.",
    "ACH payments are preferred for enterprise customers.",
]

embeddings_response = openai_client.embeddings.create(
    model="text-embedding-3-small",
    input=docs,
)

vectors = []
for i, item in enumerate(embeddings_response.data):
    vectors.append({
        "id": f"doc-{i}",
        "values": item.embedding,
        "metadata": {"text": docs[i]}
    })

index.upsert(vectors=vectors)

A few points that matter in production:

  • Keep chunk sizes small enough to retrieve precise policy text.
  • Store metadata like customer_id, invoice_id, and document_type.
  • Use a stable embedding model across indexing and query time.

3) Retrieve relevant payment context from Pinecone

When a user asks about a payment, first embed the query, then search Pinecone for the closest policy or invoice records.

query = "Can I refund invoice 8821 without manager approval?"

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,
)

contexts = [match["metadata"]["text"] for match in results["matches"]]
print(contexts)

At this point you have grounded context. That context is what keeps your agent from inventing payment rules.

4) Ask OpenAI to reason over retrieved context and decide next action

Now pass the retrieved snippets into an OpenAI chat call. In an agent system, this is where the model decides whether to create a payment intent, request approval, or reject the action.

context_block = "\n".join(f"- {c}" for c in contexts)

messages = [
    {
        "role": "system",
        "content": (
            "You are a payments assistant. Use only the provided context "
            "to answer whether a refund can be processed."
        ),
    },
    {
        "role": "user",
        "content": f"Question: {query}\n\nContext:\n{context_block}",
    },
]

response = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
)

print(response.choices[0].message.content)

In a real system, don’t stop at text generation. Turn the response into an action:

  • approve_refund
  • request_manager_review
  • create_payment_intent
  • block_and_log

That action can then trigger your internal payments service or PSP integration.

5) Connect the decision to your payment workflow

This is where “OpenAI for payments” becomes concrete: use the model’s decision to call your internal billing API or PSP adapter.

def create_payment_intent(customer_id: str, amount_cents: int, currency: str):
    # Replace with Stripe/Adyen/internal ledger call
    return {
        "status": "created",
        "payment_intent_id": "pi_12345",
        "customer_id": customer_id,
        "amount_cents": amount_cents,
        "currency": currency,
    }

decision = response.choices[0].message.content.lower()

if "manager approval" in decision:
    action = {"status": "pending_review"}
else:
    action = create_payment_intent(
        customer_id="cust_1001",
        amount_cents=12500,
        currency="usd",
    )

print(action)

Testing the Integration

Run one end-to-end test with a question that should hit your indexed policy data.

test_query = "Can I issue a refund for invoice 8821 immediately?"

test_embedding = openai_client.embeddings.create(
    model="text-embedding-3-small",
    input=test_query,
).data[0].embedding

test_results = index.query(
    vector=test_embedding,
    top_k=1,
    include_metadata=True,
)

test_context = test_results["matches"][0]["metadata"]["text"]

test_response = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "Answer only using provided context."},
        {"role": "user", "content": f"Query: {test_query}\nContext: {test_context}"},
    ],
)

print("Retrieved:", test_context)
print("Model:", test_response.choices[0].message.content)

Expected output:

Retrieved: Refunds above $500 require manager approval.
Model: Refunds above $500 require manager approval, so this refund should go through manager review first.

If you get that result consistently, your retrieval layer and decision layer are wired correctly.

Real-World Use Cases

  • Payment support agents

    • Retrieve invoice history and refund rules from Pinecone.
    • Use OpenAI to draft customer responses and decide whether to escalate.
  • Claims payout workflows

    • Pull claim documents and payout thresholds from Pinecone.
    • Use OpenAI to determine if a payout can be initiated automatically.
  • Collections and dunning assistants

    • Retrieve delinquency policy and account notes.
    • Use OpenAI to generate next-best-action steps before triggering reminders or holds.

This pattern works because each tool does one job well. Pinecone gives you grounded memory; OpenAI turns that memory into decisions your payment system can safely execute.


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