How to Integrate Azure OpenAI for lending with CosmosDB for multi-agent systems

By Cyprian AaronsUpdated 2026-04-21
azure-openai-for-lendingcosmosdbmulti-agent-systems

Combining Azure OpenAI for lending with CosmosDB gives you a clean pattern for agentic credit workflows: one model can reason over borrower data, summarize documents, and draft decisions, while CosmosDB stores conversation state, underwriting artifacts, and agent-to-agent handoffs. In practice, this is what lets you build multi-agent lending systems that keep context across credit review, fraud checks, document extraction, and final approval.

Prerequisites

  • Python 3.10+
  • An Azure OpenAI resource deployed with a chat model
  • A Cosmos DB account with a SQL API database and container
  • Azure credentials or API keys for both services
  • pip access to install SDKs
  • A lending use case already defined:
    • loan pre-screening
    • document summarization
    • income verification
    • adverse action explanation

Install the SDKs:

pip install openai azure-cosmos python-dotenv

Set environment variables:

export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"
export AZURE_OPENAI_API_KEY=" თქვენი-key "
export AZURE_OPENAI_DEPLOYMENT="gpt-4o-mini"
export COSMOS_ENDPOINT="https://your-account.documents.azure.com:443/"
export COSMOS_KEY="your-cosmos-key"
export COSMOS_DATABASE="lending_agents"
export COSMOS_CONTAINER="agent_state"

Integration Steps

1. Initialize Azure OpenAI and CosmosDB clients

Use Azure OpenAI for the reasoning layer and CosmosDB for persistent state. For multi-agent systems, every agent should write its own state so other agents can pick it up later.

import os
from openai import AzureOpenAI
from azure.cosmos import CosmosClient

azure_openai_client = AzureOpenAI(
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    api_version="2024-06-01",
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
)

cosmos_client = CosmosClient(
    url=os.environ["COSMOS_ENDPOINT"],
    credential=os.environ["COSMOS_KEY"]
)

database = cosmos_client.create_database_if_not_exists(
    id=os.environ["COSMOS_DATABASE"]
)

container = database.create_container_if_not_exists(
    id=os.environ["COSMOS_CONTAINER"],
    partition_key="/session_id"
)

2. Store borrower context in CosmosDB

In lending workflows, each application needs a durable record. Store the raw inputs plus agent outputs so underwriting, compliance, and customer service agents can all read the same session.

from datetime import datetime

borrower_case = {
    "id": "app_10001",
    "session_id": "loan_session_10001",
    "borrower_name": "Amina Patel",
    "requested_amount": 25000,
    "annual_income": 82000,
    "credit_score": 712,
    "documents": ["bank_statement.pdf", "paystub.pdf"],
    "status": "received",
    "created_at": datetime.utcnow().isoformat()
}

container.upsert_item(borrower_case)
print("Saved borrower case to CosmosDB")

3. Call Azure OpenAI to generate a lending assessment

The model should not make unsupported decisions. Use it to summarize risk factors and produce structured output that your policy engine can validate.

prompt = f"""
You are a lending assistant.
Review this application and return JSON with fields:
risk_level, key_factors, recommended_next_step.

Application:
Name: {borrower_case['borrower_name']}
Requested Amount: {borrower_case['requested_amount']}
Income: {borrower_case['annual_income']}
Credit Score: {borrower_case['credit_score']}
Documents: {', '.join(borrower_case['documents'])}
"""

response = azure_openai_client.chat.completions.create(
    model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
    messages=[
        {"role": "system", "content": "You assist with lending workflow analysis."},
        {"role": "user", "content": prompt}
    ],
    temperature=0.2,
)

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

4. Write the model output back to CosmosDB for other agents

This is where multi-agent coordination becomes practical. The fraud agent, document agent, and compliance agent can all read the same record and append their results without losing history.

borrower_case["status"] = "assessed"
borrower_case["underwriting_assessment"] = assessment_text
borrower_case["updated_at"] = datetime.utcnow().isoformat()

container.upsert_item(borrower_case)

print("Updated case with underwriting assessment")

5. Query CosmosDB to route work between agents

A multi-agent system needs simple routing logic. One agent can query pending cases, then hand off to specialized agents based on status or risk level.

query = """
SELECT * FROM c
WHERE c.session_id = @session_id
"""

items = list(container.query_items(
    query=query,
    parameters=[{"name": "@session_id", "value": "loan_session_10001"}],
    enable_cross_partition_query=True
))

for item in items:
    print(item["id"], item["status"])

Testing the Integration

Run a full round trip: save the case, call Azure OpenAI, persist the result, then read it back from CosmosDB.

item = container.read_item(item="app_10001", partition_key="loan_session_10001")
print("Status:", item["status"])
print("Assessment:", item["underwriting_assessment"])

Expected output:

Status: assessed
Assessment: {
  "risk_level": "medium",
  "key_factors": [
    "Stable income relative to loan size",
    "Credit score above typical threshold",
    "Supporting documents provided"
  ],
  "recommended_next_step": "Proceed to compliance review"
}

Real-World Use Cases

  • Loan pre-screening agent

    • Uses Azure OpenAI to summarize applicant data.
    • Stores decisions and reasons in CosmosDB for auditability.
  • Document verification pipeline

    • One agent extracts data from pay stubs and bank statements.
    • Another agent compares extracted fields against application data.
    • CosmosDB keeps both outputs tied to the same case.
  • Multi-agent credit decisioning

    • A risk agent evaluates affordability.
    • A fraud agent checks anomalies.
    • A compliance agent drafts adverse action language if needed.

This setup works because Azure OpenAI handles reasoning and language generation, while CosmosDB gives you durable shared memory across agents. For lending systems, that separation is what keeps your workflow observable, auditable, and production-ready.


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