Azure Functions with OpenAI

Azure FunctionsAzure OpenAIServerless.NET

You do not leave every light in your house on 24 hours a day. You flip a switch when you enter a room. Azure Functions works the same way for code — it runs only when something happens, like a user sending a chat message or a file landing in storage.

Now add Azure OpenAI to that picture: a smart brain that wakes up on demand, answers the question, and goes quiet again. No server babysitting. That combo is one of the simplest ways to ship AI features — and this guide explains how it fits together.

What is Azure Functions with OpenAI?

Azure Functions is Microsoft's serverless compute service. You write a small piece of code (a function), choose what triggers it, and Azure handles the rest — scaling, patching, and shutting down idle capacity.

Azure OpenAI hosts language models like GPT inside your Azure subscription. Pairing the two means your function receives a request, calls the model, and returns the result — all without you managing a virtual machine.

A trigger is the event that starts your function. Common triggers include HTTP requests (someone hits your API), blob uploads (a PDF arrives), or queue messages (a background job waits).

Why use this combo?

AI traffic is often spiky. Your app might get ten requests at midnight and ten thousand at lunch. Running a full server all day to handle lunch peak wastes money at midnight.

Serverless Functions help because they:

  • Scale automatically with demand.
  • Charge per execution, not per idle hour.
  • Let you ship a single HTTP endpoint for chat or summarization fast.
  • Integrate with other Azure services (Storage, Service Bus, Event Grid) naturally.

Think of it like ride-sharing. You do not buy a bus because rush hour exists — you summon capacity when riders appear.

How does it work?

You create a Function App in Azure, write a .NET function with an HTTP trigger, and use the Azure OpenAI SDK inside it. When a POST request arrives with a user message, the function calls the chat model and returns JSON.

Authentication should use managed identity — an automatic ID Azure gives your Function App so it can call OpenAI without API keys in config files. It is like the function wearing a company badge instead of carrying a written password in its pocket.

Mobile app sends POST /api/chat
        ↓
   Azure Functions (HTTP trigger)
        ↓
   Managed identity → Azure OpenAI
        ↓
   GPT generates reply
        ↓
   JSON response back to app
        ↓
   Function scales down when idle

For background work — summarizing every PDF uploaded to a folder — use a blob trigger instead. The function wakes when a file appears, reads it, calls OpenAI, and saves the summary. Same serverless idea, different doorbell.

Note User-facing chat APIs may hit cold starts on the Consumption plan. Consider the Premium plan or minimum instances if the first request must be fast.

Real-world example

A logistics company adds a WhatsApp-style status bot for drivers. Drivers send short voice-to-text questions: "Any delays on Route 7?"

An HTTP-triggered Function receives the text, calls Azure OpenAI with a prompt that includes live route data from a database, and replies in plain language. At 3 AM, zero drivers are asking questions — and the function is not burning compute. At 6 AM rush, Azure spins up more instances automatically.

Same pattern as food delivery apps pinging the kitchen only when you place an order — not keeping every burner on all day.

Step-by-step: build your first AI function

Step 1: Create an Azure OpenAI resource and deploy a model (for example gpt-4o-mini).

Step 2: Create a Function App with the .NET isolated worker runtime.

Step 3: Enable system-assigned managed identity on the Function App.

Step 4: Grant that identity permission to call Azure OpenAI (Cognitive Services OpenAI User role).

Step 5: Add the Azure.AI.OpenAI NuGet package and write an HTTP-triggered function.

Step 6: Test locally with Azure Functions Core Tools, then deploy via GitHub Actions or Azure Portal.

[Function("Chat")]
public async Task<HttpResponseData> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req)
{
    string body = await new StreamReader(req.Body).ReadToEndAsync();
    var chat = openAIClient.GetChatClient("gpt-4o-mini");

    var completion = await chat.CompleteChatAsync(
        new SystemChatMessage("You are a helpful assistant."),
        new UserChatMessage(body));

    var response = req.CreateResponse(HttpStatusCode.OK);
    await response.WriteAsJsonAsync(new { reply = completion.Value.Content[0].Text });
    return response;
}

Common misconceptions

"Serverless means no servers exist." Servers exist — you just do not manage them. Azure runs the infrastructure; you run the logic.

"Functions are only for tiny scripts." Production AI APIs run on Functions daily. The key is keeping each function focused and watching cold starts.

"I should put API keys in application settings." Prefer managed identity. Keys in settings leak through logs, backups, and screenshots.

Trigger typeUse caseGood fit for
HTTPReal-time API callsChat bots, mobile app backends
BlobFile uploaded to storagePDF summarization, image captioning
QueueMessage in a queueDecoupled batch processing
TimerScheduled cron jobNightly report generation

Quick recap

  • Azure Functions runs code on demand — pay for use, not idle time.
  • HTTP triggers suit live chat APIs; blob and queue triggers suit background AI jobs.
  • Managed identity connects Functions to Azure OpenAI without API keys.
  • Watch cold starts and token costs — serverless saves compute, not model usage.

Summary

Azure Functions plus Azure OpenAI is the motion-sensor light of AI architecture — power when needed, quiet when not. You get a scalable HTTP endpoint or background processor without patching servers at midnight.

Start with one HTTP function, managed identity, and a small model. Measure latency and token spend. Once that works, add queues for heavy files and monitoring for production confidence.

Frequently Asked Questions

Azure Functions is a serverless compute service. Your code runs only when triggered by an event — like an HTTP request or a new file in storage — and you pay for execution time, not idle servers.

AI workloads are often spiky. Functions scale out during busy periods and scale to zero when quiet, which fits chat APIs, summarization jobs, and webhooks.

Serverless means you focus on code, not servers. The cloud platform starts, runs, and stops compute automatically based on demand.

Managed identity gives your Function an automatic Azure AD identity so it can call Azure OpenAI without storing API keys in configuration files.

HTTP triggers respond to web requests instantly — good for chat APIs. Queue triggers process messages in the background — good for batch summarization of uploaded files.

Cold start is the extra delay when a function wakes up after being idle. Premium plans and keeping instances warm reduce it for user-facing APIs.

Yes. The isolated worker model runs modern .NET versions and works well with the Azure OpenAI SDK and dependency injection.

Key Takeaways

  • Azure Functions runs AI code on demand — ideal for spiky chat and batch workloads.
  • Use managed identity instead of API keys for Azure OpenAI access.
  • Pick the right trigger: HTTP for live APIs, blob/queue for background processing.
  • Plan for cold starts and token costs when moving from demo to production.

Suggested Next Reads

Share: LinkedIn Facebook X

Need help implementing this in your organization?

Contact Emerrank Consultancy