Reading the response — content blocks, stop_reason, usage — step 8 of 9
Write parse_response(response) that takes a response dict (from
Anthropic's messages.create) and returns a structured dict:
text— string, joined text from alltext-type blocks (joined with one space; empty string if none).tool_uses— list of{"id": ..., "name": ..., "input": ...}dicts, one pertool_use-type block.stop_reason— copyresponse["stop_reason"]through.tokens— int, sum ofusage.input_tokens + usage.output_tokens.
Skip any block whose type is neither text nor tool_use (e.g.,
thinking blocks).
Three responses run for you. Expected output:
text='Paris.' tool_uses=[] stop=end_turn tokens=37
text='Let me search.' tool_uses=[{'id': 't1', 'name': 'search', 'input': {'q': 'ramen'}}] stop=tool_use tokens=58
text='' tool_uses=[] stop=max_tokens tokens=1024
⌘↵ runs the editor.read, then continue.
Write parse_response(response) that takes a response dict (from
Anthropic's messages.create) and returns a structured dict:
text— string, joined text from alltext-type blocks (joined with one space; empty string if none).tool_uses— list of{"id": ..., "name": ..., "input": ...}dicts, one pertool_use-type block.stop_reason— copyresponse["stop_reason"]through.tokens— int, sum ofusage.input_tokens + usage.output_tokens.
Skip any block whose type is neither text nor tool_use (e.g.,
thinking blocks).
Three responses run for you. Expected output:
text='Paris.' tool_uses=[] stop=end_turn tokens=37
text='Let me search.' tool_uses=[{'id': 't1', 'name': 'search', 'input': {'q': 'ramen'}}] stop=tool_use tokens=58
text='' tool_uses=[] stop=max_tokens tokens=1024
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.