{"slug": "how-to-deploy-deepseek-r1-qwen-on-azure-with-scale-to-zero-0-idle-bill", "title": "How to Deploy DeepSeek-R1 & Qwen on Azure with Scale-to-Zero ($0 Idle Bill)", "summary": "A developer built an open-source FastAPI gateway that serves DeepSeek-R1 and Qwen models through an OpenAI-compatible API on Azure Container Apps, configured with min_replicas set to 0 so idle deployments scale to zero and incur no compute charges. The gateway exposes /v1/chat/completions and /v1/models with Server-Sent Events streaming via TextIteratorStreamer, letting existing LangChain, LlamaIndex or OpenAI SDK clients switch over by changing only their base_url. The code is published on GitHub as azure-model-deployer, with a paid boilerplate offering CUDA 12.1 GPU Dockerfiles and one-command Azure CLI deployment scripts.", "body_md": "If you are building AI applications, you quickly hit a fork in the road:\n\nIn this guide, I'll walk through how we solved this by building a lightweight FastAPI gateway deployed to **Azure Container Apps (ACA)** configured with automated **scale-to-zero**.\n\nThe gateway sits between client applications and the underlying Hugging Face Transformers model:\n\n`/v1/chat/completions` and `/v1/models`. Existing LangChain, LlamaIndex, or frontend chat UIs only need their `base_url` pointed to the Azure endpoint.`TextIteratorStreamer` for full typewriter streaming in client UIs.\nHere is the simplified implementation of the OpenAI-compatible streaming endpoint:\n\n``` python\nimport json\nimport asyncio\nfrom fastapi import FastAPI, Depends, HTTPException, Security\nfrom fastapi.responses import StreamingResponse\nfrom fastapi.security import HTTPBearer, HTTPAuthorizationCredentials\nfrom pydantic import BaseModel\nfrom typing import List, Optional\n\napp = FastAPI(title=\"Private Azure AI Gateway\")\nsecurity = HTTPBearer()\n\nclass ChatMessage(BaseModel):\n    role: str\n    content: str\n\nclass ChatCompletionRequest(BaseModel):\n    model: str\n    messages: List[ChatMessage]\n    stream: Optional[bool] = False\n    temperature: Optional[float] = 0.7\n    max_tokens: Optional[int] = 512\n\n@app.post(\"/v1/chat/completions\")\nasync def chat_completions(\n    req: ChatCompletionRequest,\n    auth: HTTPAuthorizationCredentials = Security(security)\n):\n    if req.stream:\n        async def event_generator():\n            # Stream tokens using Server-Sent Events (SSE)\n            tokens = [\"Hello\", \" from\", \" self-hosted\", \" DeepSeek!\"]\n            for token in tokens:\n                chunk = {\n                    \"id\": \"chatcmpl-123\",\n                    \"object\": \"chat.completion.chunk\",\n                    \"choices\": [{\"delta\": {\"content\": token}, \"finish_reason\": None}]\n                }\n                yield f\"data: {json.dumps(chunk)}\\n\\n\"\n                await asyncio.sleep(0.05)\n            yield \"data: [DONE]\\n\\n\"\n\n        return StreamingResponse(event_generator(), media_type=\"text/event-stream\")\n\n    return {\n        \"id\": \"chatcmpl-123\",\n        \"object\": \"chat.completion\",\n        \"choices\": [{\n            \"message\": {\"role\": \"assistant\", \"content\": \"Sample response\"},\n            \"finish_reason\": \"stop\"\n        }]\n    }\n```\n\nIn the deployment configuration, the secret sauce is setting `min_replicas = 0`:\n\n```\n{\n  \"min_replicas\": 0,\n  \"max_replicas\": 3,\n  \"scale_to_zero\": true,\n  \"idle_timeout_seconds\": 300\n}\n```\n\nWhen deployed via Azure CLI:\n\n```\naz containerapp create \\\n  --name deepseek-gateway \\\n  --resource-group my-ai-rg \\\n  --environment my-aca-env \\\n  --image myacr.azurecr.io/deepseek-gateway:latest \\\n  --target-port 8080 \\\n  --ingress external \\\n  --min-replicas 0 \\\n  --max-replicas 3 \\\n  --scale-rule-name http-rule \\\n  --scale-rule-type http \\\n  --scale-rule-http-concurrency 10\n```\n\nIn your existing Python application, you do not need to install custom Hugging Face libraries. Just point the official OpenAI SDK to your Azure endpoint:\n\n``` python\nfrom openai import OpenAI\n\nclient = OpenAI(\n    base_url=\"https://deepseek-gateway.azurecontainerapps.io/v1\",\n    api_key=\"your-secure-bearer-key\"\n)\n\nresponse = client.chat.completions.create(\n    model=\"deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B\",\n    messages=[{\"role\": \"user\", \"content\": \"Write a Python script to sort a list of dicts.\"}],\n    stream=True\n)\n\nfor chunk in response:\n    print(chunk.choices[0].delta.content or \"\", end=\"\")\n```\n\nThe complete open-source repository is available on GitHub:\n\n👉 [GitHub: faizalbagwan786/azure-model-deployer](https://github.com/faizalbagwan786/azure-model-deployer)\n\nIf you want the production-ready boilerplate with pre-configured NVIDIA CUDA 12.1 GPU Dockerfiles, vision support (SmolVLM), test suite, and 1-command Azure CLI automated deployment scripts, check out the [Gumroad Release ($10)](https://faizalbagwan.gumroad.com/l/hhtxdv).\n\n*If you are working on deploying custom private models on Azure or optimizing cloud AI costs, feel free to connect or ask questions in the comments below!*", "url": "https://wpnews.pro/news/how-to-deploy-deepseek-r1-qwen-on-azure-with-scale-to-zero-0-idle-bill", "canonical_source": "https://dev.to/faizalbagwan786/how-to-deploy-deepseek-r1-qwen-on-azure-with-scale-to-zero-0-idle-bill-108b", "published_at": "2026-11-14 12:15:28+00:00", "updated_at": "2026-11-14 12:20:45.881951+00:00", "lang": "en", "topics": ["ai-infrastructure", "large-language-models", "ai-tools", "mlops", "developer-tools"], "entities": ["Azure Container Apps", "DeepSeek-R1", "Qwen", "FastAPI", "Hugging Face", "OpenAI", "GitHub", "LangChain"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/how-to-deploy-deepseek-r1-qwen-on-azure-with-scale-to-zero-0-idle-bill", "markdown": "https://wpnews.pro/news/how-to-deploy-deepseek-r1-qwen-on-azure-with-scale-to-zero-0-idle-bill.md", "text": "https://wpnews.pro/news/how-to-deploy-deepseek-r1-qwen-on-azure-with-scale-to-zero-0-idle-bill.txt", "jsonld": "https://wpnews.pro/news/how-to-deploy-deepseek-r1-qwen-on-azure-with-scale-to-zero-0-idle-bill.jsonld"}}