Menu
CoddyTech

Checking the Results

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

challenge icon

Challenge

Medium

Now that every ticket is a dict in results, plain Python can report on the day. Keep everything you wrote so far, and add these lines after the loop:

  1. For each category, in the order of the enum in SCHEMA, print <category>: <count>. Skip a category with no tickets.
  2. Print Urgent: and the numbers of the urgent tickets, each written #<number> and joined by a comma and a space. When no ticket is urgent, print Urgent: none.

urgent is a real boolean after json.loads, so test it with if data["urgent"]:. For example, for two tickets that are both billing and not urgent, the program ends with:

billing: 2
Urgent: none

Try 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']}")

All lessons in Calling Models From Code

Practice on your own: Online Python compiler