By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
An internal support assistant called the API once per ticket and waited for the whole reply. The first week was fine. Then the handbook grew, the answers got longer, and one ticket in twenty died with a gateway timeout.
The model had finished the work. The user never saw it. The team raised the client timeout, then the gateway timeout, then bought a larger instance. None of it touched the cause: nothing was sent until everything was finished.
Every call sends a model id, a system prompt, a list of messages and generation limits. Messages alternate between the user and assistant roles. The model never sees anything you did not send.
A message is not a string. It is a list of content blocks, and the block type tells you which turn it belongs to.
| Block type | What it carries | Which turn |
|---|---|---|
text | Prose the model reads or writes | Either |
image | A base64 or URL image source | User |
tool_use | A tool name, an id and an input object | Assistant |
tool_result | The output for one tool_use id | User |
thinking | Reasoning content and its signature | Assistant |
The assistant turn goes back into the conversation unchanged before you append results. Drop a block you do not recognise and the next request can fail, because the pairing is broken.
With streaming enabled, the response becomes a series of server-sent events. Text arrives as deltas you can render immediately. Tool input arrives as input_json_delta fragments, and those fragments are not valid JSON until the block closes.
This is where ordinary async discipline pays off. Read until the stream ends, and keep a total-time budget apart from the idle timeout. Cancel when the caller disconnects, because a read that blocks forever is a leak with a bill attached.
import json
EVENTS = [
{"type": "content_block_start", "index": 0, "block": {"type": "text"}},
{"type": "content_block_delta", "index": 0,
"delta": {"type": "text_delta", "text": "Checking "}},
{"type": "content_block_delta", "index": 0,
"delta": {"type": "text_delta", "text": "order A17."}},
{"type": "content_block_stop", "index": 0},
{"type": "content_block_start", "index": 1,
"block": {"type": "tool_use", "id": "tu_1", "name": "lookup_order"}},
{"type": "content_block_delta", "index": 1,
"delta": {"type": "input_json_delta", "partial_json": '{"id": "A1'}},
{"type": "content_block_delta", "index": 1,
"delta": {"type": "input_json_delta", "partial_json": '7"}'}},
{"type": "content_block_stop", "index": 1},
]
def read_stream(events):
text = []
tool_name = None
tool_json = ""
for event in events:
if event["type"] == "content_block_start":
block = event["block"]
if block["type"] == "tool_use":
tool_name = block["name"]
tool_json = ""
elif event["type"] == "content_block_delta":
delta = event["delta"]
if delta["type"] == "text_delta":
text.append(delta["text"])
elif delta["type"] == "input_json_delta":
tool_json += delta["partial_json"]
return "".join(text), tool_name, tool_json
text, tool_name, partial = read_stream(EVENTS)
print(text)
print("tool:", tool_name)
print("input:", json.loads(partial))Streaming keeps a connection alive while the model works. It does not make the model finish sooner, and it does not shorten a forty-thousand-document job. If the problem is volume rather than idle time, the answer is a different mode.
tool_use stop reason means your code must run something and return a result.Many teams reach the API through a gateway, a proxy or a vendor's orchestration product. That layer sees and can rewrite the request, so check what survives the trip.
cache_control markers arrive, or you pay for the prefix on every call.thinking blocks survive a tool loop, or the model loses its reasoning between steps.Both modes use the same messages, the same schema and the same prompt, so one evaluation set covers both. What changes is who is waiting.
| Workload | Mode | Why |
|---|---|---|
| Live chat reply to a customer | Realtime, streamed | A person is waiting, so time to first token is the experience |
| Nightly re-classification of 40,000 archived documents | Batch | Nobody waits, and cost per document dominates |
| The ticket backlog after an outage | Batch | A volume spike where minutes matter, not seconds |
| One contract review before a signature call | Realtime | A person is blocked, so latency is the product |
Batch is asynchronous. You submit work, it is processed, and you collect results later. That trade buys cost and removes rate-limit pressure. It costs you interactivity and a same-second retry. Check the current turnaround and pricing in the API documentation before you promise a deadline.
Pick the assistant you designed in the previous step.
input_json_delta fragment to the event list above so the tool input arrives in three pieces. Predict the output first.Key takeaways
A request is a model id, a system prompt, messages of content blocks and generation limits.
Streaming shortens time to first byte; it does not shorten the work or remove the need for a budget.
Tool input arrives as fragments: accumulate, then parse at block stop.
Put the stable prefix first and the per-request text last so caching can hit.
Choose batch when nobody waits, and check the current turnaround before promising one.
A gateway can strip caching, thinking or the model pin; verify what reaches the API.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.