Claude API Kubernetes Autoscaling Setup Guide
Why Claude API workloads need a different autoscaling approach
If you're running a service that calls the Claude API from inside Kubernetes, the default CPU-based Horizontal Pod Autoscaler (HPA) almost never gives you the behavior you want. Pods that call an external LLM API spend most of their time waiting on network I/O, not burning CPU. A pod can hold 50 concurrent in-flight requests to Claude while sitting at 5% CPU utilization — which means CPU-based HPA will never scale you up when you actually need more capacity, and it will scale you down while requests are still queued.
The fix is to autoscale on the metric that actually represents load for an LLM proxy: queue depth, concurrent in-flight requests, or requests-per-second, not CPU or memory. This article walks through a practical setup using HPA with custom metrics, KEDA for queue-driven scaling, and the request-handling patterns that keep you inside Anthropic's rate limits while scaling.
Baseline architecture
Before touching autoscaling config, get the shape of the deployment right:
- Stateless worker pods that accept jobs (HTTP requests or queue messages) and call the Claude API.
- A queue or buffer (Redis, SQS, RabbitMQ, or an in-memory concurrency limiter) between your ingress and the workers, so bursts don't translate directly into a wall of simultaneous outbound calls.
- A shared rate limiter (token bucket, keyed per API key) so pods don't collectively blow past your Anthropic rate limit when they scale out.
Without the queue, autoscaling just adds more pods hammering the same rate-limited endpoint, which gets you more 429s, not more throughput.
Option 1: HPA with custom metrics
If your workers expose a Prometheus metric for in-flight requests or queue depth, you can drive HPA off that instead of CPU:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: claude-worker-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: claude-worker
minReplicas: 2
maxReplicas: 30
metrics:
- type: Pods
pods:
metric:
name: claude_inflight_requests
target:
type: AverageValue
averageValue: "8"
This requires the Prometheus Adapter (or a similar custom metrics API server) wired into your cluster so Kubernetes can read claude_inflight_requests from each pod. Set the target averageValue close to the concurrency level each pod can comfortably sustain without timing out on Claude responses — if a pod handles streaming responses, factor in that each connection stays open for the duration of the stream, not just the time to first token.
Option 2: KEDA for queue-driven scaling
If requests to Claude go through a queue (recommended for anything beyond a toy setup), KEDA is a better fit than raw HPA because it scales based on queue length and can scale to zero when idle.
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: claude-worker-scaledobject
spec:
scaleTargetRef:
name: claude-worker
minReplicaCount: 0
maxReplicaCount: 40
cooldownPeriod: 60
triggers:
- type: redis
metadata:
address: redis.default.svc:6379
listName: claude-jobs
listLength: "10"
Each pod picks up a batch of jobs from claude-jobs and calls the Claude API, respecting a per-pod concurrency cap. KEDA adds or removes workers based on how fast the queue is growing relative to listLength, which correlates with actual demand far better than CPU does.
Respecting rate limits while scaling
Scaling pod count without a shared rate limiter is the most common mistake. Anthropic enforces per-key rate limits, and more pods just means more simultaneous callers against the same limit. Two practical approaches:
- Centralized token bucket: store a rate-limit counter in Redis and have every pod check/decrement it before calling the API. Simple, but adds a Redis round trip per request.
- Static per-pod budget: divide your known rate limit by
maxReplicasand cap each pod's internal concurrency accordingly. Cheaper, but wastes headroom when replica count is below max.
Either way, your workers need solid retry/backoff logic for 429 and 529 responses — exponential backoff with jitter, capped retries, and a dead-letter path for requests that exhaust retries.
Readiness and liveness probes matter more here than usual
LLM calls can take many seconds, especially with longer context or tool use loops. Make sure:
- Liveness probes don't kill pods mid-request because of a slow health check timeout.
- Readiness probes check actual capacity (e.g., "below max concurrency") rather than just "process is up," so the Kubernetes service doesn't route new traffic to a pod that's already saturated.
- Termination grace periods are long enough that scale-down doesn't cut off in-flight streaming responses. 30-60 seconds is a reasonable starting point for most workloads.
Simplifying the stack with SubToAPI
A lot of this complexity — rate limiting, retries, key rotation, usage tracking per team or customer — exists because you're managing raw Claude API access directly. If you route calls through SubToAPI, your Kubernetes workers call a single HTTPS endpoint with a sub_live_... key instead of juggling Anthropic rate limits and backoff logic themselves. That doesn't remove the need for autoscaling — you still want your pod count to track load — but it does remove one entire class of failure (rate-limit coordination across pods) from your cluster design. See the quickstart and streaming docs for how requests and responses look in practice, and pricing for plan details if you want usage metadata and team seats alongside the API itself.
A minimal checklist before you ship this
- Workers expose a custom metric (in-flight requests or queue depth), not just default resource metrics.
- A queue sits between ingress and workers to absorb bursts.
- A shared rate limiter prevents the scaled-out fleet from exceeding your Claude API rate limit.
- Readiness probes reflect actual capacity, not just liveness.
- Termination grace periods accommodate long-running streamed responses.
- Retry/backoff logic handles 429/529 without amplifying load during incidents.
Questions
Does CPU-based HPA work at all for Claude API workloads? It works poorly on its own because LLM calls are I/O-bound, not CPU-bound. Use it only as a secondary safety metric alongside a custom metric like in-flight requests or queue depth.
Should I scale to zero when there's no traffic? Yes if you're using KEDA with a queue trigger — it's efficient for bursty or low-traffic services. Avoid scale-to-zero for latency-sensitive synchronous endpoints, since cold-start pods add noticeable delay to the first request.
How do I prevent scaled-out pods from exceeding Anthropic's rate limits? Use a shared, centralized rate limiter (Redis token bucket is common) that every pod checks before calling the API, rather than relying on each pod enforcing its own independent limit.