AWS Lambda is the natural choice for AI endpoints — you pay per invocation, it scales to zero when idle, and API Gateway gives you a production-ready URL in minutes. But Lambda has a hidden tension with LLM calls: you are paying for Lambda execution time while waiting on an external API that you have no control over. A slow OpenAI response means a slow Lambda invocation means higher costs. And if OpenAI is down, your Lambda retries, times out, and you pay for the failed attempts too.
Antbase optimizes both sides of this equation. On the latency side, simple requests get routed to fast models that respond quickly — shorter Lambda execution time, lower costs. On the reliability side, Antbase handles retries and failover across providers, so your Lambda gets a successful response even during provider issues. You stop paying for failed invocations and timeout retries.
The serverless cost model makes Antbase routing even more valuable than in traditional deployments. Every millisecond of Lambda execution costs money, and every unnecessary premium model call is a double expense — you pay more for the AI tokens AND more for the longer Lambda execution time. By routing simple tasks to fast free models, Antbase reduces both your AI costs and your compute costs simultaneously.
AWS Lambda lets you run functions without managing servers. Combined with Antbase, you get serverless AI endpoints that scale automatically — pay only for what you use on both the compute and AI sides.
Lambda Handler
# handler.py
import json
import os
from openai import OpenAI
client = OpenAI(
base_url="https://antbase.ai/v1",
api_key=os.environ["ANTBASE_API_KEY"],
)
def handler(event, context):
body = json.loads(event.get("body", "{}"))
message = body.get("message", "Hello")
response = client.chat.completions.create(
model="auto",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": message},
],
)
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({
"reply": response.choices[0].message.content,
}),
}Deploy with SAM
# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
AIFunction:
Type: AWS::Serverless::Function
Properties:
Handler: handler.handler
Runtime: python3.12
Timeout: 60
MemorySize: 256
Environment:
Variables:
ANTBASE_API_KEY: !Ref AntbaseApiKey
Events:
Api:
Type: Api
Properties:
Path: /ask
Method: post
Parameters:
AntbaseApiKey:
Type: String
NoEcho: trueTips
- •Set Lambda timeout to 60 seconds. Antbase responds quickly, but complex routing to premium models can take 10-20 seconds.
- •The OpenAI client is initialized outside the handler (at module scope) to reuse the connection across invocations.
- •Store the API key in AWS Systems Manager Parameter Store or Secrets Manager instead of hardcoding in the template.
- •For streaming responses from Lambda, use Lambda Function URLs with response streaming enabled (Node.js runtime).



