Reading the reply
A reply has text, a stop reason, and token usage.
The reply holds more than words. It tells you WHY the model stopped (stop_reason) and how much you used (usage). If stop_reason is 'end_turn', the model finished its thought. If it is 'max_tokens', the reply hit your limit and was cut off in the middle. Usage counts input tokens (what you sent) and output tokens (what came back). Providers charge for both, so good programs keep an eye on them.
# To run on your own computer with an API key (it cannot run in this browser).
# First: pip install anthropic, and set the ANTHROPIC_API_KEY environment variable.
import anthropic
client = anthropic.Anthropic()
reply = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=50,
messages=[{"role": "user", "content": "Tell me about volcanoes."}],
)
print(reply.content[0].text)
print(reply.stop_reason) # "end_turn" or "max_tokens"
print(reply.usage.input_tokens) # tokens you sent
print(reply.usage.output_tokens) # tokens the model wroteIn the real SDK these are attributes (reply.usage.input_tokens). In our pretend model they are dict keys: reply['usage']['input_tokens']. Same information, slightly different spelling.
A token is a piece of text, often part of a word. You will learn to estimate them in a later unit. The pretend model simply counts each word as one token.
When you see 'max_tokens', you have two choices: raise the limit, or ask for a shorter answer in your prompt (for example 'in two sentences').