How to Integrate Azure OpenAI for investment banking with CosmosDB for production AI

By Cyprian AaronsUpdated 2026-04-21
azure-openai-for-investment-bankingcosmosdbproduction-ai

Combining Azure OpenAI for investment banking with CosmosDB gives you a practical pattern for production-grade agent systems: the model handles language, reasoning, and summarization, while CosmosDB stores client context, deal notes, compliance state, and conversation memory. That means you can build assistants that answer banker questions with grounded firm data instead of relying on stateless prompts.

For investment banking workflows, this is the difference between a demo chatbot and an internal tool that can support pitch books, due diligence, KYC review, and mandate tracking.

Prerequisites

  • Python 3.10+
  • An Azure OpenAI resource deployed in your Azure subscription
  • A deployed Azure OpenAI model name, such as gpt-4o-mini or gpt-4.1
  • An Azure Cosmos DB account with a database and container created
  • Azure credentials available via environment variables or managed identity
  • Installed packages:
    • openai
    • azure-cosmos
    • python-dotenv

Install dependencies:

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="your-api-key"
export AZURE_OPENAI_API_VERSION="2024-06-01"

export COSMOS_ENDPOINT="https://your-account.documents.azure.com:443/"
export COSMOS_KEY="your-cosmos-key"
export COSMOS_DATABASE="banking_ai"
export COSMOS_CONTAINER="client_memory"

Integration Steps

  1. Initialize Azure OpenAI and CosmosDB clients

    Start by wiring both SDKs in the same service. In production, keep the clients singleton-scoped so you do not recreate them per request.

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

azure_openai_client = AzureOpenAI(
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_version=os.environ["AZURE_OPENAI_API_VERSION"],
)

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

database = cosmos_client.get_database_client(os.environ["COSMOS_DATABASE"])
container = database.get_container_client(os.environ["COSMOS_CONTAINER"])
  1. Store investment banking context in CosmosDB

    Use CosmosDB to persist client metadata, meeting notes, deal status, or prior responses. This gives your agent memory across sessions.

from datetime import datetime

client_profile = {
    "id": "client-acme-capital",
    "pk": "client-acme-capital",
    "client_name": "ACME Capital Partners",
    "sector": "Financial Services",
    "deal_stage": "mandate_review",
    "last_meeting_summary": "Discussed debt financing options for Q3 acquisition.",
    "updated_at": datetime.utcnow().isoformat()
}

container.upsert_item(client_profile)
print("Saved client profile to CosmosDB")
  1. Retrieve context from CosmosDB before calling Azure OpenAI

    Your agent should ground responses in stored facts. Query CosmosDB by partition key or id before generating output.

client_id = "client-acme-capital"

query = """
SELECT * FROM c
WHERE c.id = @id AND c.pk = @pk
"""

params = [
    {"name": "@id", "value": client_id},
    {"name": "@pk", "value": client_id},
]

items = list(container.query_items(
    query=query,
    parameters=params,
    enable_cross_partition_query=False,
))

context = items[0] if items else {}
print(context)
  1. Generate an investment banking response with Azure OpenAI

    Pass the retrieved CosmosDB context into the prompt so the model answers using firm-specific memory.

model_name = "gpt-4o-mini"

messages = [
    {
        "role": "system",
        "content": (
            "You are an internal investment banking assistant. "
            "Use only the provided client context and user request. "
            "If data is missing, say what is missing."
        ),
    },
    {
        "role": "user",
        "content": f"""
Client context:
{context}

Request:
Draft a concise follow-up email summarizing next steps after the mandate review.
""",
    },
]

response = azure_openai_client.chat.completions.create(
    model=model_name,
    messages=messages,
    temperature=0.2,
)

draft_email = response.choices[0].message.content
print(draft_email)
  1. Write the generated output back to CosmosDB

    Persist outputs so reviewers can audit what the agent produced and reuse it later in workflow automation.

result_doc = {
    "id": f"{client_id}-followup-email-001",
    "pk": client_id,
    "type": "generated_artifact",
    "artifact_name": "followup_email",
    "source_client_id": client_id,
    "content": draft_email,
}

container.upsert_item(result_doc)
print("Saved generated email to CosmosDB")

Testing the Integration

Run a single end-to-end check: write context, fetch it back, generate a response, then store the result.

test_client_id = "client-acme-capital"

# Fetch stored context
stored = list(container.query_items(
    query="SELECT * FROM c WHERE c.id = @id AND c.pk = @pk",
    parameters=[
        {"name": "@id", "value": test_client_id},
        {"name": "@pk", "value": test_client_id},
    ],
))

assert stored, "No client context found in CosmosDB"

# Generate a short summary
reply = azure_openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "Summarize banking notes clearly."},
        {"role": "user", "content": f"Summarize this record: {stored[0]}"},
    ],
)

text = reply.choices[0].message.content
print(text)

Expected output:

Client ACME Capital Partners is in mandate review for a debt financing transaction tied to a Q3 acquisition. Next step is to confirm financing structure and circulate follow-up materials.

Real-World Use Cases

  • Deal team copilot

    • Pulls client history from CosmosDB and drafts banker-ready updates with Azure OpenAI.
    • Useful for meeting prep, follow-up emails, and pitch book summaries.
  • Due diligence assistant

    • Stores diligence findings, red flags, and open questions in CosmosDB.
    • Uses Azure OpenAI to summarize issues by workstream or generate management Q&A.
  • Compliance-aware workflow agent

    • Persists approvals, disclaimers, and audit trails in CosmosDB.
    • Uses Azure OpenAI to draft responses while keeping a traceable record of every output.

If you want this production-ready, add retry logic around both SDK calls, enforce partition-key design up front, and keep all prompts versioned alongside stored artifacts in CosmosDB. That gives you an agent system that is auditable enough for banking environments and stable enough for real internal use.


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