Chapter 1 · section 5 of 9
The response
response.content is an array, not a string, because a single response can contain several different kinds of thing. Each element is a content block, a typed piece of the answer. The one you care about now has type: "text" and carries a text field; the model these examples use thinks before it answers, and a response can carry a thinking block ahead of its text, which is why talk.ts loops and checks block.type rather than reaching for response.content[0].text. TypeScript refuses to compile the second form in any case, because content is a union of block types and only some of them have a .text.
response.stop_reason says why the model stopped. A stop reason of end_turn means it finished; max_tokens means it hit the ceiling; and refusal means the model’s own safeguards declined the request, which arrives as a successful HTTP response with no answer in it. A refusal is a 200 with nothing to read, and a program that reads content without checking stop_reason first will read an empty answer as an answer. The second program in this chapter checks.
response.usage reports what the request consumed: input_tokens is what you sent and output_tokens is what came back, thinking included. Print them. In Chapter 11 you will add them up across a whole run.