LLMs for mobile engineers, from tokens to your app · Puzzle 2 · 3 of 8
Your first call
Puzzle 1 said the model works in tokens. Now one real call, so everything after it has something concrete to stand on: what your app sends, what comes back, and the three numbers that matter in the response.
The problem
You know the model continues text and counts in tokens. But what does the request look like, in code you could write with OkHttp? And what comes back besides the answer?
See it
One HTTP POST: the model's name, max_tokens, and messages, a list of turns; here, one user turn.
Step 1 of 4Back comes JSON: the text, a stop_reason, and usage, the input and output tokens of this call.
Step 2 of 4If the answer hits your limit, it stops mid-sentence and says so. Predict: which tokens cost more?
Step 3 of 4Output tokens: they're priced higher, and each takes a step to generate.
Step 4 of 4
The building block
A call to Anthropic's Messages API is one HTTP POST with a JSON body (shown slightly simplified):
{
"model": "MODEL_NAME",
"max_tokens": 300,
"messages": [
{ "role": "user", "content": "Summarise this review: …" }
]
}
- model: which model to use, a name from the provider's docs.
- max_tokens: the most tokens the answer may have.
- messages: the conversation as a list of turns, each marked
user(your side) orassistant(the model's earlier answers). Puzzle 4 shows why that list matters.
The response, also JSON:
{
"content": [ { "type": "text", "text": "Short battery life, great screen." } ],
"stop_reason": "end_turn",
"usage": { "input_tokens": 312, "output_tokens": 9 }
}
Three things to read every time:
- the text in
content; - stop_reason:
end_turnmeans the model finished;max_tokensmeans it was cut off at your limit, mid-sentence; - usage: the input and output tokens of this call. That's the bill: providers charge per token, and output tokens cost several times more than input ones. Output is also what takes time, one token per step (puzzle 1).
The analogy
A telegram service that charges per word, with a higher rate for the reply. You state the maximum reply length up front, and if the reply hits it, it stops, mid-sentence.
Check yourself
Sources
- Anthropic: Messages API examples (a response contains
content,stop_reasonsuch as"end_turn"or"max_tokens", and"usage": { "input_tokens": …, "output_tokens": … }; requests sendmodel,max_tokensandmessageswith roles) - Anthropic: Pricing (input and output tokens are priced separately, output higher)
What you now know
A call to a model is one HTTP POST: a model name, max_tokens (the most it may write), and messages, a list of turns each marked user or assistant. The response holds the continuation, a stop_reason (end_turn when it finished, max_tokens when it was cut off), and usage: the input and output tokens you pay for. Output tokens cost more than input ones, and each one takes time to generate.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.