DEV Community

shashank ms
shashank ms

Posted on

Deploying LLM Models on Cloud Functions: Best Practices and Strategies

Serverless platforms like AWS Lambda, Google Cloud Functions, and Azure Functions are attractive for LLM applications because they scale to zero and charge only for compute time used. However, deploying billion-parameter models directly inside a Cloud Function is usually impractical. Memory limits, execution timeouts, and cold-start latency make on-premise inference inside a function container a fragile pattern. The dominant best practice is to treat Cloud Functions as an orchestration and API normalization layer while offloading inference to a dedicated platform. Oxlo.ai fits this architecture precisely: it offers 45+ open-source and proprietary models with no cold starts on popular models, full OpenAI SDK compatibility, and request-based pricing that removes the cost uncertainty of token-based billing.

Separating Inference from Serverless Compute

Cloud Functions excel at short-lived, stateless tasks. A typical AWS Lambda deployment offers up to 10 GB of memory and a 15-minute timeout. While this is sufficient for application logic, it is inadequate for serving a 70B parameter model such as Llama 3.3 70B or DeepSeek R1 671B MoE. Loading these weights into a serverless container would trigger multi-minute cold starts on every scale-out event, and inference latency would be unpredictable.

The recommended architecture keeps models on dedicated GPU infrastructure and uses Cloud Functions for pre-processing, authentication, RAG retrieval, and response formatting. Oxlo.ai provides fully OpenAI API compatible endpoints at https://api.oxlo.ai/v1, so you can replace your OpenAI client configuration without rewriting application code. Because Oxlo.ai has no cold starts on popular models, the time-to-first-token remains consistent even when your serverless tier scales from zero.

Connection Pooling and Client Reuse

One of the most common mistakes in serverless LLM apps is initializing a new HTTP client inside every function invocation. This adds TLS handshake overhead and DNS latency on every request. Instead, initialize your client outside the handler so it persists across warm invocations.

import os
from openai import OpenAI

# Initialize once, reuse across invocations
client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key=os.environ.get("OXLO_API_KEY")
)

def lambda_handler(event, context):
    messages = event.get("messages", [])
    
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=messages,
        stream=False
    )
    
    return {
        "statusCode": 200,
        "body": response.choices[0].message.content
    }

This pattern works identically in Google Cloud Functions,

Top comments (0)