APIAdvancedClaude API
Streaming: a stream that stops is not a complete message
A turn built from a cut-off stream can leave a truncated tool_use in the history and break the next request.
A tool_use's arguments arrive as partial JSON across several deltas: they can only be parsed at content_block_stop. Save the turn to the history only after message_stop. If the stream is interrupted, discard the partial turn and retry; and look at stop_reason before continuing the loop.
Example
if event.type == "content_block_stop":
parse_block_input() # only here is it valid JSON
if event.type == "message_stop":
history.append(turn) # complete turn
# stream cut before that → discard the partial turn and retry