Read the Model's Response

Your script from the last lesson makes a real model call and prints everything that comes back. Somewhere in that wall of JSON is a draft reply about Hannah’s late parcel, surrounded by IDs, settings, and counts. A script that drafts replies for the whole inbox can’t stop and let you scroll, so it has to pull out what it needs by itself.

In this lesson, you’ll read a raw model response and find the three things your code needs from it: the reply’s text, the reason the model stopped, and the number of tokens the call used. Then you’ll write a function that picks those out reliably, on every call. The first question your script has to answer is where, exactly, the reply sits.

Find the Three Fields You Need

Most of a response is noise for your purposes. It echoes the request you sent, such as the model ID and the instructions, and lists settings you didn’t change. Three parts answer the questions your script will keep asking. What did the model write? Did it finish? How many tokens did the call use, and what did it cost?

Here’s one more real run of draft-reply.js from the last lesson, with the fields that don’t matter here cut and marked with .... Open the version for your provider. In both, the reply sits inside a list of parts, and this time it isn’t the only part:

The output list holds two items on this run. The long text line is wrapped here for display:

Language: Program Output
{
  ...
  "output": [
    {
      "content": [],
      ...
      "id": "rs_002e80f7a0e42bd0006b0db8e8765887d2b71db2402623064e",
      "summary": [],
      "type": "reasoning"
    },
    {
      "content": [
        {
          ...
          "text": "Hi Hannah,\n\nI’m sorry your Darjeeling first flush
            hasn’t arrived. Thanks for sending your order number. We’ll
            check the shipping status of order #4417 and get back to you
            with an update.\n\nLeaf & Kettle Tea",
          "type": "output_text"
        }
      ],
      ...
      "type": "message"
    }
  ],
  ...
  "status": "completed",
  ...
  "usage": {
    "input_tokens": 103,
    ...
    "output_tokens": 77,
    "output_tokens_details": {
      "reasoning_tokens": 23
    },
    "total_tokens": 180
  },
  ...
}

Each item in output has a type, and these two are different kinds:

  • The "reasoning" item is where the model worked through the request before writing the reply. Its content and summary lists are empty. Unless your request asks for a summary of that work, OpenAI doesn’t send it back as readable text.
  • The "message" item holds the reply. Its content is a list of parts, and the reply is the text of the part whose type is "output_text".

The top-level status field tells you whether the model finished: "completed" means generation ran to its end, and "incomplete" means it stopped early, with the reason in incomplete_details.reason. A completed response doesn’t always hold a draft, though. When the model declines a request, the message holds a "refusal" part instead of "output_text".

The token counts are in usage: input_tokens for what you sent and output_tokens for what the model generated. The 23 reasoning_tokens are the part of those 77 output tokens that went into the reasoning item.

The content list holds two blocks on this run:

Language: Program Output
{
  "model": "claude-sonnet-5-5",
  "id": "msg_...",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "thinking",
      "thinking": "",
      "signature": "..."
    },
    {
      "type": "text",
      "text": "Hi Hannah,\n\n..."
    }
  ],
  "stop_reason": "end_turn",
  ...
  "usage": {
    "input_tokens": ...,
    ...
    "output_tokens": ...,
    "output_tokens_details": {
      "thinking_tokens": ...
    },
    ...
  }
}

Each block in content has a type, and these two are different kinds:

  • The "thinking" block is where the model worked through the request before writing the reply. Its thinking field is empty. By default, this model doesn’t send that work back as readable text, only an encrypted signature.
  • The "text" block holds the reply, in its text field.

The stop_reason field tells you why the model stopped: "end_turn" means it finished the reply. That doesn’t guarantee a usable draft, though. When Anthropic steps in on a policy issue, stop_reason is "refusal" and the text can be cut short or missing, so check the text as well as the stop reason.

The token counts are in usage: input_tokens for what you sent and output_tokens for what the model generated. The thinking_tokens are the part of the output tokens that went into the thinking block.

Your reply will be worded differently, and your counts will differ a little, because every run generates new text. For everyone, though, the response has the same shape, with a list of typed parts, a stop reason, and the usage counts:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.