Managing Cold Starts and Model Warm-Up in Serverless Agent Functions
Why agent functions on Cloud Run take so long to wake up, how to measure cold vs warm latency honestly, and the concrete levers — min instances, concurrency, connection pools, startup CPU — that keep first requests fast.
Published on • November 27, 2026
AI Assistant

The pattern is always the same: your agent feels instant in testing, then the first user after lunch stares at a spinner for four seconds. Serverless platforms bill by execution, which means idle services scale to zero — and every wake-up pays a tax your users experience as hesitation.
For agents the tax is worse than for a typical web request, because a turn is a chain of work: container start, process boot, framework import, model client construction, session lookup, then the model call itself. This article breaks down where the time goes, how to measure it, and which levers actually move it.
Anatomy of a cold start
trigger → schedule container → pull image (if needed) → start process
→ import Python modules → construct model clients
→ read secrets / warm connection pools
→ first request handled ← total: your cold start
Typical contributors for an ADK service:
| Phase | Rough cost | Scales with |
|---|---|---|
| Scheduling + image pull | 100 ms – 1.5 s | Image size, cold frequency |
| Process + interpreter boot | 50–300 ms | Runtime |
| Importing heavy modules (SDK, HTTP stack, ML deps) | 200 ms – 2 s | Dependency count |
| Building the Gen AI client / auth | 50–500 ms | Credential chain (metadata server is fast; a remote key fetch is not) |
| Session store connection | 50–800 ms | Pool creation, TLS, cold database |
| First model call | your normal TTFT | prompt size, region, queue |
Note that the last row isn’t a cold start in the platform’s sense, but users don’t distinguish them. A cold container that then does a full TTFT reads as one long stall.
Measure it before you tune it
You need cold and warm latency as separate series:
import time, os
COLD = os.environ.get('X_COLD_START') is None # set by a first-run marker
_started = time.perf_counter()
async def handle(request):
now = time.perf_counter()
is_cold = not globals().setdefault('_warm', False)
globals()['_warm'] = True
...
# tag metrics: phase="cold" on the first request this instance serves
Practical instrumentation notes:
- Tag the first request per instance (an instance ID in your logs lets you group afterwards).
- Percentiles per phase: p95 cold, p50 warm, and the share of requests that were cold — that share is the number that changes when you tune scaling.
- Reproduce it deliberately. Scale to zero, wait, then hit the endpoint with a scripted probe. Random production sampling makes cold starts look rarer than they are for the users who hit them.
- Separate deploy-time warmup from user traffic. The first request after a rollout is always cold; count it as an operational event, not a steady-state metric.
The levers, in order of payoff
1. Min instances for the interactive path
The blunt instrument, and usually the right one. Keeping one instance warm converts a 2-second cold start into a warm request for the cost of an idle container. Apply it to the user-facing service only — background workers can still scale to zero.
# cloud run (conceptual)
minScale: 1
maxScale: 50
2. Concurrency
If each instance handles one request at a time, you scale up aggressively and pay more cold starts under burst. Raising per-instance concurrency (ADK handlers are I/O-bound — most of the wait is on the model) means fewer instances and fewer wake-ups. Set it from measurement: too low wastes money, too high queues requests and hurts TTFT.
3. Image and import weight
- Slim base images, multi-stage builds, and a lockfile-driven install without build tooling.
- Defer heavy imports. Importing an ML library, a browser driver, or a vector client that the first turn doesn’t need costs startup time on every cold container. Lazy-load inside the tool that needs it.
- Cut dependency count. A transitive dependency you don’t use is still imported.
4. Client construction and connection pools
- Build model clients once at module scope or in a lifespan hook, not per request — and keep the reuse behaviour you want from your TTFT tuning.
- Pre-warm session DB pools during startup so the first turn doesn’t pay pool creation plus TLS.
- Fetch credentials from the ambient metadata server rather than remote key files; the metadata hop is fast and local.
5. Startup CPU and timeout tuning
Platforms that allow a CPU boost during startup (or provisioned concurrency-style warming) meaningfully cut import time. Pair it with a bounded startup timeout so a slow boot fails visibly rather than hanging into the user’s request budget.
6. Keep-alive and canary warmup
- Periodic canary requests (every 1–5 minutes) keep instances from scaling all the way down and double as health checks.
- For the model endpoint itself, a tiny ping after deploy avoids the first real user paying provider-side cold effects.
When serverless stops being the right shape
Sometimes the correct optimization is architectural:
- Steady interactive traffic → a long-running service (Cloud Run with a floor, GKE, or a plain VM) where boot cost is paid once per release, not per idle cycle.
- Very heavy agents (browser automation, in-process model runtimes) → warm pools with dedicated capacity, because startup is measured in tens of seconds.
- Bursty background work → keep serverless and accept cold starts; nobody is waiting on those requests.
Split the fleet: interactive path warm, batch path cheap. Trying to serve both with one scaling policy satisfies neither.
Takeaways
- A cold start for an agent is a chain — container, imports, client construction, pools, then model TTFT. Instrument the phases separately.
- Tag first-request-per-instance so cold and warm latency are distinct series, and reproduce cold starts on purpose.
- Payoff order: min instances for the interactive path → concurrency sizing → slim images/lazy imports → module-scope clients and pre-warmed pools → startup CPU boosts → canary keep-alive.
- Don’t import what the first turn doesn’t need; measure dependency weight as latency.
- Split interactive (warm) and batch (scale-to-zero) traffic, and go long-running when boot cost dominates.