Skip to content
AI Engineering
Building with the API

API features: streaming, limits and batches

Beyond a plain call, provider APIs offer a few features that change cost, speed and reliability.

Chapter 7: Building with the APILesson 4 of 11Level: StarterAbout 12 min
Words on this page (11)
token
: A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
tool use
: The model asks your program to run a function and reads the result.
tool
: A function the model can ask your program to run, described by a name, description and argument schema.
backoff
: Waiting longer after each failed attempt before retrying, for example 1, 2, then 4 seconds.
batch
: A group of examples processed together in one step.
prompt
: The input text you give a model, including instructions, examples and data.
API
: Application programming interface: a way for one program to call another, usually over HTTP.
jitter
: A little randomness added to retry waits so many clients do not retry at the same moment.
JSON
: A text format for structured data: keys and values in braces, like {"name": "Ada"}.
system prompt
: Standing instructions placed at the start of every call that set the model's role and rules.
logarithm
: The power you raise a base to in order to get a number. log base 2 of 8 is 3 because 2 to the power 3 is 8.

Streaming

The server sends as they are generated, so the user sees the answer start in a fraction of a second instead of waiting for the whole thing. It is built on server-sent events. Handle partial and errors that arrive mid-stream.

Limits

Each model has a maximum context size and a maximum output size. Counting tokens before a call prevents errors. Rate limits cap requests and tokens per minute: handle them with retries, and queues. Set timeouts, since a long generation can run for minutes.

Batch and caching

APIs run many non-urgent requests at a discount, with results returned within hours. caching charges less for an unchanged prefix. Use both for bulk work such as classifying a large dataset.

Two API styles

Chat-style APIs take a list of messages and return the next message. Newer response-style APIs add built-in , state kept on the server and richer output items. Many providers and routers, such as OpenRouter, expose an OpenAI-compatible endpoint so the same client code can call many models by changing the base URL and model name.

A worked example: handling limits

Your app sends 300 requests a minute and the provider allows 200. The extra 100 get a 429 error. A good client queues them and retries after 1, 2, then 4 seconds with a little random , so a hundred clients do not retry in lockstep. For a non-urgent job, such as labeling 100,000 reviews overnight, use a batch : results arrive within hours at a lower price.

Streaming gotchas

Text arrives in small pieces, so append, do not replace. The connection can drop halfway: keep what you have and offer a retry. Let the user press Stop.

How streaming works

The server keeps the connection open and sends small events as tokens are produced. Your code reads each event and appends its text to the screen. Usage counts arrive in the stream's closing events. Some providers need an option switched on to send them.

Two times matter. Time to first token is the wait before anything appears. Total time is how long the whole answer takes. Streaming improves the first and leaves the second unchanged.

Trade-offs and when not to stream

Skip streaming when code, not a person, consumes the answer. A reply cannot be parsed until it is complete. Skip it when you must check the full answer before anyone sees it, such as a safety filter.

Batch jobs give a discount but you wait, so they suit work nobody is waiting on. Prompt caching only helps when the start of the prompt is identical between calls. Anything that changes early, such as a timestamp at the top of the , breaks the match.

What goes wrong

Errors arrive in waves. Many clients hit the limit together and retry at the same moment. Add random jitter, then retry times to see the waves spread out.

A worker freezes. No timeout (a maximum wait you set) was configured. Look for requests that have been open for minutes.

Costs do not drop after you enable caching. Check the usage fields your provider returns for cached tokens, and look at what changes at the top of your prompt.

An answer ends halfway. The connection dropped. Keep the partial text and offer a retry button.

Try it yourself

Stream a request that asks for a long answer. Print the time when the first piece arrives and the time when the stream ends. Typically the first comes within a second or two and the second several seconds later. Then run it without streaming and confirm the total time is about the same.

Common mistake

Setting no timeout. A stuck request that never returns can freeze a worker forever.

The code

Streaming prints words as they arrive instead of waiting for the full answer.

with client.messages.stream(
    model="claude-sonnet-5-5",
    max_tokens=300,
    messages=[{"role": "user", "content": "Explain tokens in two sentences."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Check yourself

Why stream tokens to the user?

How should you handle rate limits?

You must label 100,000 reviews overnight, and speed does not matter. What fits best?

Go deeper

Spotted a mistake or something unclear? Report it on GitHub.

On this page