Menu
CoddyTech

まとめ:経費精算エージェント

CoddyのAI Engineeringジャーニー「ツールとエージェント」セクションの一部。レッスン 15/22。

challenge icon

チャレンジ

難しい

チェックと人間の承認を経てからのみ支出を記録する経費エージェントを作成します。スターターコードは1つのリクエストを読み込みます。TOTALS にはカテゴリごとのこれまでの支出額が保持されています。

  1. 2つのツールを定義します。両方とも category("enum": ["travel", "meals", "office"] を指定した文字列)を受け取ります。
    • log_expense(説明: Log a new expense, in euros, under a category.)は、数値である amount も受け取ります。両方のプロパティが必須です。
    • get_total(説明: Get the total spent so far in one expense category.)
  2. ツール呼び出しを最大5ラウンドまで行うエージェントループを、model="claude-opus-5-5" および max_tokens=300 で実行します。
  3. get_total は <category>: <total> euros spent. を返します。
  4. log_expense はまず金額をチェックします。金額が0以下または200を超える場合は、エラー結果として Amounts must be between 0 and 200 euros. を返し、何も質問しません。
  5. それ以外の場合は、Approve <amount> euros for <category>? (yes/no) を出力し、1行読み込みます。yes 以外が入力された場合は、エラー結果として The user declined to log this expense. を返します。
  6. 承認された場合、その金額をカテゴリの合計に追加し、Logged <amount> euros for <category>. New total: <total> euros. を返します。
  7. Answer: と最終テキストを出力します。

すべての金額と合計は小数点以下2桁で記述してください。エラー結果には "is_error": True を含めます。例えば、入力が Log 25 euros for a taxi under travel で、応答が yes の場合、プログラムは次のように出力します:

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.

自分で試してみよう

import coddy_ai as anthropic  # 本番環境では: import anthropic

client = anthropic.Anthropic()

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

request = input()

# 以下にコードを記述してください

ツールとエージェントのすべてのレッスン

自分で練習してみよう: Pythonオンラインコンパイラ