Stop 16 · AI APIs & Cloud AI PlatformsTopics · 9/9

Serverless AI Functions

Part of the AI Engineer Roadmap

In short

Your AI feature gets 50 requests on Monday and 5,000 the night before exams. Serverless functions scale up and down by themselves, and you pay only when they run.

Deploying AI-powered logic as serverless functions (AWS Lambda, Vercel Functions) that scale automatically and only cost money when invoked — a common pattern for lightweight AI features.

The blob hops into a bright auto-rickshaw while a parked car sits covered in dust behind.

Think of it like this

Auto-rickshaw, not your own car

Owning a car means EMI, parking and insurance even when it sits idle. An auto is there when you need it, you pay per ride, and the first one sometimes takes a minute to arrive. Serverless functions are autos for your code.

Watch it happen

One request through a serverless function

Request
Cold start
Handler
LLM API
Response

1/5POST /summarise arrives at the function's URL.

The big ideas

What you need to know

Pay per run

You are billed for invocations and run time, not idle servers. Perfect for spiky, low-traffic AI features.

Cold starts

The first request after a quiet spell is slower. Keep imports light and create clients outside the handler.

Timeouts are tight

Functions have time limits, and LLM calls can be slow. Set the timeout higher than your slowest expected call.

Secrets in config

Put API keys in the platform's environment variables or secret manager, never in the code.

See it in code

Walk through the code

python
1import json2from openai import OpenAI3 4client = OpenAI()  # created once per container, reused on warm calls5 6def handler(event, context):7    body = json.loads(event.get("body") or "{}")8    text = body.get("text", "")[:4000]9    resp = client.responses.create(10        model="gpt-5-mini",11        input=f"Summarise in 2 lines:\n{text}",12    )13    return {"statusCode": 200,14            "body": json.dumps({"summary": resp.output_text})}

1/4The client is built outside the handler, so warm calls reuse it instead of rebuilding it.

An AWS Lambda-style Python handler behind an HTTP URL. Vercel and Cloud Run functions follow the same idea.

Save yourself the pain

Common mistakes

Oops

Keeping AWS Lambda's default 3-second timeout for an LLM call.

Do this instead

LLM replies often take longer. Raise the timeout and also set a timeout on the API client.

Oops

Running a big local model inside a serverless function.

Do this instead

Functions suit calling hosted APIs. Large models need GPUs and long-lived servers.

Your mission

Ship a summariser function

~40 min

Quick check

Why create the API client outside the handler function?

Go deeper

  1. 1Deploy a simple LLM-calling function on a serverless platform.
  2. 2Learn cold-start implications for latency-sensitive AI features.
  3. 3Understand when serverless is the right fit vs. when a persistent server is needed (e.g. long-running agents).

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge