Menu
CoddyTech

Recap: Review Sorter

Part of the Calling Models From Code section of Coddy's AI Engineering journey. Lesson 15 of 22.

challenge icon

Challenge

Medium

Sort 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).

  1. Write a JSON schema with two required fields: sentiment, a string whose enum is positive, negative and neutral, and summary, a string. Set additionalProperties to False.
  2. Send each review on its own with model="claude-haiku-5-5", max_tokens=200 and that schema as the format. Print <sentiment>: <summary>.
  3. Then print Positive: <n>, Negative: <n>, Neutral: <n>.
  4. Print Tokens: <input> in, <output> out, the totals over all calls.
  5. 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.00002660

Try 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

Practice on your own: Online Python compiler