Production-grade C# / .NET 10 patterns for building with Azure OpenAI. Maintained by Safkat Nirjash — AI Engineering Lead.
Most Azure OpenAI samples online are toy notebooks. This cookbook is the opposite: each recipe is a small, runnable C# snippet that mirrors something I've actually shipped in production — with the gotchas, the cost notes, and the failure modes called out.
| # | Recipe | What it covers |
|---|---|---|
| 01 | Basic chat completion | The Azure.AI.OpenAI client, auth, deployment naming, retries |
| 02 | Streaming responses | Server-sent events, cancellation, partial-token handling |
| 03 | Function calling | Tool definitions, argument parsing, multi-turn loops |
| 04 | Embeddings + cosine similarity | Generating, storing, and searching embeddings |
| 05 | RAG over a document set | Chunking, retrieval, prompt construction, citations |
| 06 | Structured outputs (JSON mode) | Schema enforcement, validation, error recovery |
| 07 | Token counting & cost control | Tiktoken in .NET, budget guards, per-tenant caps |
| 08 | Eval harness | Scoring outputs against a golden set with rubrics |
(More added as I ship them. Star the repo to follow.)
I'm currently building an AI-integrated analytics platform on .NET 10 + Azure OpenAI. Every recipe here is something I needed, couldn't find a good answer for, and reverse-engineered from production traffic. Putting them out so the next engineer doesn't have to.
Each recipe is self-contained under snippets/. To run:
cd snippets/01-basic-chat
dotnet user-secrets set "AzureOpenAI:Endpoint" "https://YOUR-RESOURCE.openai.azure.com/"
dotnet user-secrets set "AzureOpenAI:ApiKey" "YOUR-KEY"
dotnet user-secrets set "AzureOpenAI:DeploymentName" "gpt-4o"
dotnet runUse user-secrets, environment variables, or Managed Identity in production. Never commit keys — the .gitignore is configured to keep appsettings.Local.json out.
- .NET 10 (LTS) target framework, nullable enabled,
LangVersion=latest - One project per recipe, kept small enough to read in one sitting
- Cost notes at the top of every README (
# ~$0.001 per call) - Failure modes documented inline as
// FAILS WHEN: ...
MIT — use freely in your own projects.
Safkat Nirjash — Senior .NET / Azure Architect, currently AI Engineering Lead at Inument/Gifty. 13+ years in production .NET, AZ-305 + AI-102 + AWS SAP certified. nirjash.com · LinkedIn