Foundations to Frontier
Tests

LLM provider APIs

Most LLM applications call hosted models through provider APIs from companies such as Anthropic, OpenAI and Google, or through OpenAI-compatible servers such as vLLM. The APIs share a core: a list of messages and a system prompt in, generated content out. Around that core they add streaming, tool calling, structured outputs, prompt caching, batch processing and token-based billing. Rate limits and transient errors are part of normal operation. This page covers request structure, parameters, streaming, tools, structured outputs, caching and batching, costs, rate limits and retries, and production concerns such as timeouts, idempotency, logging and data handling. In the exercises you compute backoff waits and costs and parse tool calls. You then simulate 50 clients sending 1000 requests to an API limited to 10 requests per second. Immediate retries had 98% of attempts rejected. Exponential backoff cut rejections to 38% but stretched the finish time to 120 s, against an ideal of 100 s. Pacing requests on the client side to the known limit gave no rejections and the shortest tail.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Talking to a model over the internet

Hosted models are used by sending a request: some instructions, the conversation so far, and settings such as how long the answer may be. The answer comes back in pieces as it is written, so users see text quickly. Providers limit how many requests you can send per minute; if you go over, they say ‘slow down’, and a well-behaved program waits a little longer after each refusal. The demo shows how waits add up.

🎮 Tooling lab
5 retries with waits 1·2^k s (capped at 60 s) take 31.0 s in total. Add random jitter so many clients do not retry in lockstep.
Why engineers care

A flight-operations assistant must handle rate limits and outages gracefully, with timeouts and fallbacks, because the API is a dependency like any other external service.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A chat API request contains…
  2. 2. Streaming lets users…
  3. 3. After a rate-limit refusal a client should…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Request: model, system prompt, messages (user / assistant turns, images, documents), max tokens, temperature, stop sequences; response with content blocks, stop reason and token usage.
  • Streaming: server-sent events; time to first token matters for user experience.
  • Tools: JSON-schema tool definitions; the model returns tool calls; your code executes them and returns results (F11-1).
  • Structured outputs: JSON mode or schema-constrained decoding; still validate.
  • Prompt caching: reuse of a long shared prefix at a large discount and lower latency; order content stable-first.
  • Batch APIs: asynchronous jobs at about half price for offline workloads.
  • Limits and errors: requests and tokens per minute; 429 (rate limit), 5xx and overloaded errors; retry with exponential backoff and jitter, respect retry-after headers, cap retries, use idempotency.
  • Production: timeouts, fallbacks to other models, logging of prompts and usage, cost dashboards, data-retention and privacy settings, keys in a secrets manager.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Prompt caching works best when the shared content is…
  2. 2. Batch APIs suit…
  3. 3. HTTP 429 means…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Calling APIs robustly

Exercise 1.

  • Exponential backoff with jitter.
  • Token cost with caching and batching.
  • Parsing a tool call.

Practice.

  • Use the official SDKs: they handle retries, timeouts and streaming; set max_retries and timeout explicitly.
  • Pace requests with a client-side limiter when you know your limits; back off with jitter on 429 and 5xx.
  • Put system prompt, tools and long documents first and mark them cacheable; measure cache hit rates.
  • Log token usage per feature; set budgets and alerts.
  • Keep API keys out of code; use environment variables or a secrets manager.
import anthropic
client = anthropic.Anthropic(max_retries=5, timeout=60)        # SDK retries 429/5xx with backoff
manual = open("maintenance_manual.txt").read()
resp = client.messages.create(
    model="claude-sonnet-5-5", max_tokens=500,
    system=[dict(type="text", text="Answer from the manual only. Cite section numbers."),
            dict(type="text", text=manual, cache_control=dict(type="ephemeral"))],
    messages=[dict(role="user", content="Torque for the M8 wing-root bolt?")])
print(resp.content[0].text, resp.usage)
🐍 Exercise 1: backoff, cost and tool calls
Compute backoff waits with jitter, estimate call costs with caching and batching, and parse a tool call.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Official SDKs handle…
  2. 2. API keys belong in…
  3. 3. Track cost by logging…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Rate limits as queues. A token bucket admits at most r requests per second on average. When clients want more, the excess must wait somewhere: in the client (pacing) or in repeated failed attempts (retries). Retries do not raise throughput above r; they only change how much work is wasted and how waiting is distributed among clients.

Backoff. Exponential backoff reduces the retry rate geometrically, so wasted attempts fall; but some unlucky clients reach long waits and finish late. Jitter randomises waits so clients do not retry in synchronised waves.

Cost model. Cost = (uncached input × p_in + cached input × p_in × (1 − d) + output × p_out) / 10⁶, halved for batch jobs. Output tokens usually cost several times input tokens, so verbose outputs dominate bills.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Retries can raise throughput above the limit?
  2. 2. Exponential backoff mainly reduces…
  3. 3. Bills are often dominated by…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How do retry strategies behave against a rate-limited API, and what does client-side pacing gain?

History.

  • Exponential backoff in Ethernet (Metcalfe and Boggs, 1976).
  • M. Brooker, “Exponential backoff and jitter” (AWS Architecture Blog, 2015).
  • Token-bucket rate limiting (Turner, 1986).

Exercise 2. A simulated API limited to 10 requests per second.

  • Server: token bucket of size 10; responses take 0.5–1.5 s.
  • Clients: 50, each sending 20 requests one after another (1000 in total), so the ideal finish time is 100 s.
  • Strategies after a 429: retry after 0.05 s; after 1 s; exponential 0.5·2^k s (cap 30 s); exponential with full jitter; client-side pacing (wait for the next token without sending).
strategy rejected share of attempts finish time (s) 95th-percentile request time (s)
immediate retry 0.981 104.1 20.0
fixed 1 s 0.747 105.8 14.8
exponential 0.376 120.3 8.1
exponential + jitter 0.569 118.7 14.8
client-side pacing 0.0 105.6 3.1
  • No strategy beat the limit: work finished in 104–120 s in every case.
  • Immediate retries wasted 98% of attempts (each counts against quotas and load).
  • Exponential backoff wasted the fewest attempts among retry strategies but left some clients waiting up to 30 s.
  • Jitter shortened the finish time relative to plain exponential on all seeds (105.3 vs 141.1 and 118.6 vs 142.0 on seeds 1 and 2) at the cost of more rejections.
  • Pacing removed rejections and long tails.
  • Caveat: pacing here knows the server’s bucket exactly; real clients estimate limits from headers.
🐍 Exercise 2 (research): how do retry strategies behave against a rate-limited API, and what does client-side pacing gain?
Simulate many clients against a rate-limited API with different retry strategies and client-side pacing; measure rejected attempts, finish time and tail latency.

Provider APIs in AI, robotics and aerospace

  • Operations tools: assistants for planners and engineers built on APIs need fallbacks for outages and clear data-handling agreements.
  • Robotics: cloud LLM calls for task planning must tolerate latency and failure; on-board fallbacks keep robots safe.
  • Data sensitivity: export-controlled or personal data may require private deployments or zero-retention settings.

References

  1. R. Metcalfe and D. Boggs (1976). “Ethernet: distributed packet switching for local computer networks.” CACM.
  2. M. Brooker (2015). “Exponential backoff and jitter.” AWS Architecture Blog.
  3. Anthropic (2025). Messages API, prompt caching and batch processing, documentation.
  4. OpenAI (2025). Rate limits and error codes, documentation.