LLM APIs

6. LLM APIs#

Production AI features talk to models through APIs, and those calls fail, slow down, and cost money. This chapter teaches you to call model APIs the way an IS professional should: understanding the request format, building in reliability, and controlling cost and latency.

  • API Basics: Real request and response shapes from two provider routes; stop reasons, usage fields, status codes, and error bodies; an acceptance matrix.

  • Reliability: One retry policy, backoff with a Retry-After floor and a deadline, rate-limit headers and token buckets, fallbacks through an adapter, and concurrency under a limit.

  • Cost and Latency: Reservations and reconciliation, cost from a price table with cached tokens, scoped response caches, streams and time to first token, batch APIs, and the assembled case client.

Chapter Video

This tutorial sets up an API key and makes a first model API call from code, the starting point for the request-and-response patterns in this chapter.

Claude API Crash Course #1 - Introduction & Setup (9:11) by Net Ninja

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Construct model API requests with messages and parameters, and read the response fields.

  2. Handle API errors with timeouts, retries, and exponential backoff.

  3. Respect rate limits and design for provider outages.

  4. Estimate and control API cost with token budgets and caching.

  5. Build a reusable Python model client with retries and usage logging.

Chapter Flow

Glossary

#

Term

Explanation

1

Endpoint

A URL where an API accepts requests.

2

API key

A secret credential that identifies and authorizes the caller of an API.

3

Software development kit (SDK)

A library that wraps an API in functions for a programming language.

4

Request parameter

A setting sent with a request, such as the model name or maximum output tokens.

5

HTTP status code

A number in an API response that reports success or the type of failure.

6

Timeout

The longest a client waits for a response before giving up.

7

Retry

Sending a failed request again.

8

Exponential backoff

Waiting longer after each failed retry to avoid overloading a service.

9

Rate limit

The maximum number of requests or tokens a provider accepts in a period.

10

Usage metadata

Token counts and other details the API returns with each response.

11

Caching

Storing and reusing earlier results so repeated requests cost less.

12

Streaming

Receiving output in pieces as it is generated instead of all at once.

13

Response adapter

Code mapping an external response into an application result.

14

Deadline

A limit on the complete operation including waits and retries.

15

Jitter

Random variation in retry delay.

16

Idempotency

Repeating an operation without additional unintended effect.

17

Graceful degradation

Preserving useful service with reduced functionality.

18

Circuit breaker

A policy suppressing calls temporarily after failures.

19

Bulkhead

Capacity isolation between workloads.

20

Reservation

Capacity held before work starts.

21

Reconciliation

Replacing reserved capacity with observed usage.

22

Cache key

An identity for equivalent authorized reusable results.

23

Usage record

A record of counted resource consumption for a call.

24

Acceptance check

A check that a request/result interaction satisfies its application contract.

25

Operation identity

A value correlating a result with its intended request.

26

Unknown usage

Resource consumption not yet established by available evidence.

27

Stop reason

The provider’s statement of why generation ended, such as a natural end, an output limit, a stop sequence, or a refusal.

28

Spend cap

An account-level limit on monthly spending; a request refused for it cannot succeed on retry.

29

Retry-After

A response header giving the minimum wait before a request may be repeated.

30

Token bucket

A rate-limit model in which capacity refills continuously up to a maximum.

31

Rate-limit header

A response header reporting the limit, the remaining allowance, and the reset time.

32

Gateway

A service that holds provider keys, routes requests, applies limits, and logs usage for every feature.

33

Time to first token

The time from sending a request until the first piece of output arrives.

34

Server-sent events

A streaming format in which a response arrives as a sequence of named events.

35

Batch API

A provider endpoint that processes many requests asynchronously at a lower price.

36

Cached tokens

Prompt tokens served from a provider’s prompt cache and billed at a reduced rate.