Lesson 68.5 · 5 of 5 · 4 min read · Reviewed 24 Nov 2026
Worked example: batch APIs and streaming in production
By the end, you can
- Use Claude's Message Batches API for offline jobs.
- Handle streaming events and errors in production.
Message Batches
You send a list of requests, each with your own custom_id and normal Messages APIMessages API: The chat API endpoint that takes a list of messages and returns the next assistant message. parameters:
{"requests": [
{"custom_id": "ticket-1001", "params": {"model": "...", "max_tokens": 200,
"messages": [{"role": "user", "content": "Label this ticket: ..."}]}},
{"custom_id": "ticket-1002", "params": {"...": "..."}}
]}
Then you poll the batch status until processing ends and download the results. Each result line has the custom_id and either a message or an error. Batches cost less than standard calls and most finish well within a day. The batch docs list current size limits, the discount and how long results are kept.
Prompt cachingPrompt caching: Reusing the processed form of a repeated prompt prefix across requests, which cuts cost and latency. also works inside batches, so a long shared system promptSystem prompt: Instructions passed in the top-level system field that set the model’s role, rules and context for the whole conversation. is worth marking for caching.
Streaming in production
A streamedStreaming: Receiving a response as a series of server-sent events while it is generated, instead of waiting for the whole reply. response is a series of events: message_start, then content blockContent block: One typed piece of a message, such as text, image, document, tool_use or tool_result. start, delta and stop events, then message_delta (with the stop reasonStop reason: The field in a response that says why generation stopped: end_turn, max_tokens, stop_sequence, tool_use and others. and final usage) and message_stop. Points to handle:
- Errors can arrive mid-stream, for example an overload error after some text. Decide whether to retry from scratch or show what arrived with a notice.
- Proxies and load balancers must not buffer the stream, or users see nothing until the end.
- Client disconnects: stop reading and close the request so you don’t pay for tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. nobody sees.
- Record final usage from the closing events for cost tracking.
Key points
- The Message Batches API processes many requests asynchronously at a discount compared with standard calls.
- Each request in a batch has a custom_id; results come back per request, with success or error.
- Streaming sends server-sent events; handle errors mid-stream and keep partial text if useful.
Run a Message Batch and a stream
Submit a small Message Batch and stream a reply with the real Claude API from your own machine.
You will have: A local script that submits 5 requests as a batch, saves results by custom_id, and streams one reply with error handling.
Steps
- Install the official Python SDK on your machine and set ANTHROPIC_API_KEY as an environment variable; never paste a key into this site or into code.
- Create a batch of 5 short classification requests, each with its own custom_id.
- Poll the batch until processing ends, then read the results and store them in a dict keyed by custom_id, keeping errors separate.
- Stream one reply with the SDK's streaming helper and time the first text delta.
- Wrap the stream in error handling that keeps partial text and logs the request id.
Done when
- All 5 results are stored by custom_id, with any errors listed separately.
- The streamed reply prints its time to first text and final usage.
- No API key appears in your code or output.
Docs: Batch processing (Claude docs) · Streaming messages (Claude docs)
Do it Handle a stream that fails halfway in the Playground. No API key needed.
Finished the chapter? Take the chapter check.