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

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

Azure OpenAI for healthcare gives you clinical-grade language capabilities for summarization, triage support, and structured extraction. CosmosDB gives your agent system durable memory, fast retrieval, and a place to coordinate state across multiple agents. Put them together and you can build healthcare workflows where one agent extracts patient context, another checks policy or care pathways, and a coordinator agent persists decisions and handoffs.

Prerequisites

  • Python 3.10+
  • An Azure subscription with:
    • Azure OpenAI resource
    • Azure Cosmos DB account
  • Access to an Azure OpenAI model deployment
  • A Cosmos DB database and container ready for agent state
  • Environment variables set:
    • AZURE_OPENAI_ENDPOINT
    • AZURE_OPENAI_API_KEY
    • AZURE_OPENAI_DEPLOYMENT
    • COSMOS_ENDPOINT
    • COSMOS_KEY
    • COSMOS_DATABASE
    • COSMOS_CONTAINER
  • Installed packages:
    • openai
    • azure-cosmos
    • python-dotenv
pip install openai azure-cosmos python-dotenv

Integration Steps

  1. Initialize Azure OpenAI for healthcare and CosmosDB clients

    Keep the clients in one module. In production, this is where you also wire retry policies, telemetry, and secret loading.

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

load_dotenv()

aoai_client = AzureOpenAI(
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    api_version="2024-02-15-preview",
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
)

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"])
deployment_name = os.environ["AZURE_OPENAI_DEPLOYMENT"]
  1. Create a shared agent-memory schema in CosmosDB

    Multi-agent systems need explicit state. Store task metadata, agent outputs, clinical notes, and routing decisions as JSON documents.

from datetime import datetime, timezone
import uuid

def create_agent_state(patient_id: str, task_type: str) -> dict:
    return {
        "id": str(uuid.uuid4()),
        "patient_id": patient_id,
        "task_type": task_type,
        "status": "created",
        "created_at": datetime.now(timezone.utc).isoformat(),
        "messages": [],
        "agent_outputs": {},
    }

state = create_agent_state("patient-123", "clinical_summarization")
container.upsert_item(state)
print("Saved state:", state["id"])
  1. Call Azure OpenAI to extract structured healthcare output

    For healthcare workflows, force structured output so downstream agents do not parse free text blindly. Use the chat completions API with a strict instruction set.

clinical_note = """
Patient reports chest discomfort for 2 days.
No shortness of breath.
History of hypertension.
Current meds: lisinopril.
"""

response = aoai_client.chat.completions.create(
    model=deployment_name,
    messages=[
        {
            "role": "system",
            "content": (
                "You are a healthcare assistant. Extract concise structured data "
                "for downstream clinical workflow. Return JSON only."
            ),
        },
        {"role": "user", "content": clinical_note},
    ],
    temperature=0,
)

summary_text = response.choices[0].message.content
print(summary_text)
  1. Persist the model output back into CosmosDB for other agents

    This is the coordination layer. One agent writes the result; another agent can read it later to decide next actions.

item = container.read_item(item=state["id"], partition_key=state["patient_id"])

item["status"] = "summarized"
item["agent_outputs"]["azure_openai"] = {
    "summary": summary_text,
    "model": deployment_name,
}

container.upsert_item(item)
print("Updated CosmosDB document")
  1. Build a second agent that reads from CosmosDB and acts on the result

    In multi-agent systems, one agent should not own everything. A routing or escalation agent can read stored state and decide whether to escalate to human review.

def route_next_action(patient_id: str) -> str:
    query = """
    SELECT * FROM c
    WHERE c.patient_id = @patient_id
    ORDER BY c.created_at DESC
    """
    items = list(container.query_items(
        query=query,
        parameters=[{"name": "@patient_id", "value": patient_id}],
        enable_cross_partition_query=True,
    ))

    if not items:
        return "no_state_found"

    latest = items[0]
    summary = latest.get("agent_outputs", {}).get("azure_openai", {}).get("summary", "")

    if "chest discomfort" in summary.lower():
        return "escalate_to_clinical_review"

    return "continue_automation"

print(route_next_action("patient-123"))

Testing the Integration

Run an end-to-end smoke test: write state, generate output with Azure OpenAI, persist it to CosmosDB, then read it back.

test_patient_id = "patient-test-001"
test_state = create_agent_state(test_patient_id, "triage_support")
container.upsert_item(test_state)

test_response = aoai_client.chat.completions.create(
    model=deployment_name,
    messages=[
        {"role": "system", "content": "Return JSON only."},
        {"role": "user", "content": "Patient has fever and sore throat for 3 days."},
    ],
    temperature=0,
)

test_summary = test_response.choices[0].message.content

test_state["status"] = "completed"
test_state["agent_outputs"]["azure_openai"] = {"summary": test_summary}
container.upsert_item(test_state)

saved = container.read_item(item=test_state["id"], partition_key=test_patient_id)
print(saved["status"])
print(saved["agent_outputs"]["azure_openai"]["summary"])

Expected output:

completed
{"...structured summary from Azure OpenAI..."}

If you get a document back with the generated content stored under agent_outputs.azure_openai, the integration is working.

Real-World Use Cases

  • Clinical intake orchestration

    • One agent extracts symptoms from intake text.
    • Another agent checks prior context from CosmosDB.
    • A coordinator routes high-risk cases to human staff.
  • Care-plan summarization across multiple agents

    • A summarizer agent creates concise patient updates.
    • A reviewer agent compares updates against stored history in CosmosDB.
    • A supervisor agent decides what needs escalation or follow-up.
  • Prior authorization support

    • An extraction agent pulls procedure details from unstructured notes.
    • A policy agent reads coverage rules stored in CosmosDB.
    • A decision agent generates a traceable recommendation for staff review.

This pattern works because each system does one job well. Azure OpenAI handles reasoning and extraction; CosmosDB handles durable shared memory for the agent graph.


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