Designing Foundation Model APIs with API Gateway
This lesson describes API design for generative AI workloads, whose responses can exceed typical timeouts, whose inputs are measured in tokens and whose cost per request varies widely.
Learning outcomes
After completing this lesson, you will be able to:
- Handle long-running generation beyond integration timeouts.
- Manage token limits at the API layer.
- Retry only retryable failures.
Timeouts and long-running generation
API Gateway REST APIs have a default integration timeout of 29 seconds; for Regional and private REST APIs it can be raised (trading off account-level throttle quota), and HTTP APIs allow up to 30 seconds. Generations that may exceed these limits need a different shape: streaming (WebSocket APIs or REST response streaming) so data flows continuously, or an asynchronous API that returns 202 Accepted with a job ID and lets the client poll a status resource or receive a callback.
The following diagram shows the request and polling sequence of an asynchronous report API.

29s
Default REST API integration timeout
30s
Maximum HTTP API integration timeout
Generation that outlasts the API
Token limit management
Token budgets protect both the model's context window and the organisation's costs. The API layer estimates the number of input tokens in each request, or uses a token-counting API where available, and rejects or truncates inputs above a limit before they reach the model. It trims or summarises long conversation histories and sets maxTokens for each use case, so that a request cannot generate an unexpectedly long and expensive response.
In a shared service, budgets are also needed per consumer. Token usage for each client or team, tracked in DynamoDB or ElastiCache, lets the API throttle a client that exceeds its allowance, so that one runaway application cannot consume the shared model quota and cause throttling for everyone else. Returning token usage in response headers or bodies helps clients monitor and manage their own consumption.
Retry strategies for model timeouts
Clients should retry only retryable failures: HTTP 429 and 503 from API Gateway or the backend, and timeouts on idempotent operations, with exponential backoff and jitter, honouring any Retry-After header. Validation errors (HTTP 400) must not be retried. When a model times out on long outputs, the backend can retry with a lower maxTokens, a faster model or a request to continue from the partial output. Idempotency keys let clients retry safely without duplicating side effects.
Usage plans and API keys apply rate limits and quotas per client, and AWS WAF protects the API from abusive traffic. The following table summarises design choices.
| Problem | Design choice |
|---|---|
| Generation exceeds integration timeout | Streaming, or asynchronous 202 plus job status |
| Inputs too large or costly | Token estimation, limits, truncation, history summarisation |
| One client exhausts quota | Usage plans and per-consumer token budgets |
| Transient failures and timeouts | Selective retries with backoff; lower maxTokens or faster model |
| Malformed requests | Request validation with JSON Schema models |
Should the client retry?
Report generation API
A report-generation endpoint sometimes takes 90 seconds. Clients receive HTTP 504 errors and retry, multiplying load.
The synchronous design cannot meet the timeout. The API should accept the request, enqueue it and return 202 Accepted with a job ID; a worker generates the report; the client polls GET /reports/{id} or receives a webhook. Clients' retry logic should treat 202 as success and back off on 429.
Should an API retry a 400 error from the model?
Show answer
No. A 400 means the request is invalid. Retry 429, 503 and timeouts with backoff.
Exam Tips
Remember for the exam
- Default REST integration timeout is 29 seconds; stream or go asynchronous for long generations.
- Estimate and limit tokens at the API layer; set maxTokens per use case.
- Retry 429, 503 and timeouts with backoff; never retry 400 errors.
- Asynchronous APIs return 202 with a job ID.