Recap: Review Sorter
Part of the Calling Models From Code section of Coddy's AI Engineering journey. Lesson 15 of 22.
Challenge
MediumSort product reviews and report what the run cost. The starter reads how many reviews there are, then the reviews, and holds the Claude Haiku 5.5 prices from November 2026 ($0.10 per million input tokens, $0.50 per million output tokens; check Anthropic's pricing page for today's).
- Write a JSON schema with two required fields:
sentiment, a string whose enum ispositive,negativeandneutral, andsummary, a string. SetadditionalPropertiestoFalse. - Send each review on its own with
model="claude-haiku-5-5",max_tokens=200and that schema as the format. Print<sentiment>: <summary>. - Then print
Positive: <n>, Negative: <n>, Neutral: <n>. - Print
Tokens: <input> in, <output> out, the totals over all calls. - Print
Cost: $and the total cost with 8 decimal places.
For example, for the input 1 and The chair is grey and has four legs. the program prints:
neutral: The chair is grey and has four legs
Positive: 0, Negative: 0, Neutral: 1
Tokens: 131 in, 27 out
Cost: $0.00002660Try it yourself
import coddy_ai as anthropic # in production: import anthropic
import json
client = anthropic.Anthropic()
# Claude Haiku 5.5 prices in dollars per million tokens (November 2026)
PRICE_INPUT = 0.10
PRICE_OUTPUT = 0.50
count = int(input())
reviews = [input() for _ in range(count)]
# Write your code below
All lessons in Calling Models From Code
3Tokens and Structure
Counting Tokens and CostHandling API ErrorsJSON Schema OutputClassifying With EnumsRecap: Review SorterPractice on your own: Online Python compiler