Recap: Expense Agent
Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 15 of 22.
Challenge
HardBuild 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.
- Define two tools. Both take
category, a string with"enum": ["travel", "meals", "office"].log_expense, described asLog a new expense, in euros, under a category., also takesamount, a number. Both properties are required.get_total, described asGet the total spent so far in one expense category.
- Run the agent loop with
model="claude-opus-5-5"andmax_tokens=300, for at most 5 rounds of tool calls. get_totalreturns<category>: <total> euros spent.log_expensefirst checks the amount: when it is 0 or less, or over 200, it returnsAmounts must be between 0 and 200 euros.as an error result and asks nothing.- Otherwise it prints
Approve <amount> euros for <category>? (yes/no)and reads one line. Anything butyesreturnsThe user declined to log this expense.as an error result. - When approved, it adds the amount to the category's total and returns
Logged <amount> euros for <category>. New total: <total> euros. - 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
3Designing Agents
Writing Tool DescriptionsValidating Tool InputHuman ApprovalChaining Tool ResultsRecap: Expense AgentPractice on your own: Online Python compiler