The Cost Report
Part of the Calling Models From Code section of Coddy's AI Engineering journey. Lesson 19 of 22.
Challenge
MediumLast step: every call costs money, so the report ends with what the run used. Keep everything you wrote so far.
- After the client, add
PRICE_IN = 4.00andPRICE_OUT = 20.00: Claude Opus 5.5's price in dollars per million input and output tokens in November 2026 (check the pricing page before you rely on them). - Before the loop, set
tokens_inandtokens_outto0. After each call, add the response'susage.input_tokensandusage.output_tokensto them. - At the end, print
Tokens: <in> in, <out> out, thenCost: $and the cost with 6 decimals. The cost is the input tokens divided by 1,000,000 timesPRICE_IN, plus the output tokens divided by 1,000,000 timesPRICE_OUT.
For example, for the two billing tickets of the last step the report ends with:
billing: 2
Urgent: none
Tokens: 324 in, 72 out
Cost: $0.002736Try it yourself
import coddy_ai as anthropic # in production: import anthropic
import json
client = anthropic.Anthropic()
SCHEMA = {
"type": "object",
"properties": {
"category": {"type": "string", "enum": ["billing", "shipping", "account", "technical", "other"]},
"urgent": {"type": "boolean"},
"summary": {"type": "string"},
},
"required": ["category", "urgent", "summary"],
"additionalProperties": False,
}
count = int(input())
tickets = [input() for _ in range(count)]
print(f"Tickets: {count}")
results = []
for number, ticket in enumerate(tickets, start=1):
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=200,
output_config={"format": {"type": "json_schema", "schema": SCHEMA}},
messages=[{"role": "user", "content": ticket}],
)
for block in response.content:
if block.type == "text":
data = json.loads(block.text)
results.append(data)
urgent = "yes" if data["urgent"] else "no"
print(f"#{number} {data['category']} | urgent: {urgent} | {data['summary']}")
for category in SCHEMA["properties"]["category"]["enum"]:
total = sum(1 for data in results if data["category"] == category)
if total > 0:
print(f"{category}: {total}")
urgent_numbers = [f"#{number}" for number, data in enumerate(results, start=1) if data["urgent"]]
if urgent_numbers:
print("Urgent: " + ", ".join(urgent_numbers))
else:
print("Urgent: none")
All lessons in Calling Models From Code
Practice on your own: Online Python compiler