Menu
CoddyTech

Recap: Expense Agent

Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 15 of 22.

challenge icon

Challenge

Hard

Build an expense agent that logs spending only after a check and a person's approval. The starter reads one request; TOTALS holds what is spent so far per category.

  1. Define two tools. Both take category, a string with "enum": ["travel", "meals", "office"].
    • log_expense, described as Log a new expense, in euros, under a category., also takes amount, a number. Both properties are required.
    • get_total, described as Get the total spent so far in one expense category.
  2. Run the agent loop with model="claude-opus-5-5" and max_tokens=300, for at most 5 rounds of tool calls.
  3. get_total returns <category>: <total> euros spent.
  4. log_expense first checks the amount: when it is 0 or less, or over 200, it returns Amounts must be between 0 and 200 euros. as an error result and asks nothing.
  5. Otherwise it prints Approve <amount> euros for <category>? (yes/no) and reads one line. Anything but yes returns The user declined to log this expense. as an error result.
  6. When approved, it adds the amount to the category's total and returns Logged <amount> euros for <category>. New total: <total> euros.
  7. Print Answer: and the final text.

Write every amount and total with 2 decimals. Error results carry "is_error": True. For example, for the input Log 25 euros for a taxi under travel and yes the program prints:

Approve 25.00 euros for travel? (yes/no)
Answer: Here is what I found: Logged 25.00 euros for travel. New total: 145.00 euros.

Try it yourself

import coddy_ai as anthropic  # in production: import anthropic

client = anthropic.Anthropic()

TOTALS = {"travel": 120.0, "meals": 45.5, "office": 30.0}

request = input()

# Write your code below

All lessons in Tools and Agents

Practice on your own: Online Python compiler