Lesson 4 of 4 FM API Integration Patterns

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.

An asynchronous report API. The client's POST is accepted with 202 and a job ID instead of waiting 90 seconds, a worker generates the report from the queue, and the client polls GET /reports/{id} or receives a webhook. Clients treat 202 as success and back off on 429.
A webhook replaces polling when the client can receive callbacks.

29s

Default REST API integration timeout

30s

Maximum HTTP API integration timeout

Scenario questions

Generation that outlasts the API

Exam skills 2.5.1 · 2.4.1

Choose the API design for generations that may exceed API Gateway timeouts.

Log in to try this activity Interactive activities are free with an account.

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.

ProblemDesign choice
Generation exceeds integration timeoutStreaming, or asynchronous 202 plus job status
Inputs too large or costlyToken estimation, limits, truncation, history summarisation
One client exhausts quotaUsage plans and per-consumer token budgets
Transient failures and timeoutsSelective retries with backoff; lower maxTokens or faster model
Malformed requestsRequest validation with JSON Schema models
Sort into groups

Should the client retry?

Exam skills 2.4.3 · 2.5.1

Decide whether a client of an FM API should retry each response.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.