跳到正文
DEV Community· rene·· 4 小時前AI 評分57

作者分享以 CSV 追蹤 AI 智能體執行成本的方法

A 5-column CSV that stops your AI agents from silently burning money

AI 導讀

作者分享一套以 CSV 記錄自主 AI 智能體每次執行成本與結果的方法,並提供 Python 記錄器。追蹤欄位涵蓋 run_id、agent_role、tokens_in / tokens_out、cost_usd 和 outcome;分析指標包括已交付結果成本、無行動消耗及重試倍數。

正文

rene

Most agent cost problems are not model-pricing problems. They are untracked-run problems: nobody can say which run spent what, for which task, and whether the output was used.

Here is the minimal tracking schema I use for every autonomous agent run. You can start with just a CSV.

The 5 columns

column why it matters
run_id lets you join logs, costs and outcomes
agent_role which agent spent the money
tokens_in / tokens_out the raw driver of cost
cost_usd computed at write time, not at month end
outcome shipped, no_action, failed, retried

Three queries that pay for themselves

  1. Cost per shipped outcome: total cost_usd divided by the count of outcome = shipped. If it is rising, your agents are getting less efficient.
  2. No-action burn: sum cost_usd where outcome = no_action. This is pure waste, and it is often 20-40% of spend in loops that re-read state without acting.
  3. Retry multiplier: runs per task. More than 2 means a prompt or tool problem, not a budget problem.

Minimal Python logger

import csv, time, pathlib

PATH = pathlib.Path('agent_runs.csv')

def log_run(run_id, role, tin, tout, cost, outcome):
    new = not PATH.exists()
    with PATH.open('a', newline='') as f:
        w = csv.writer(f)
        if new:
            w.writerow(['ts','run_id','role','tokens_in','tokens_out','cost_usd','outcome'])
        w.writerow([time.strftime('%Y-%m-%dT%H:%M:%S'), run_id, role, tin, tout, round(cost, 4), outcome])

Call it in a finally block so failed runs are recorded too. Failed runs are where the money hides.

If you want it pre-built

I packaged this schema as a Notion database template plus a CSV starter and the three views above, so you do not have to wire it up yourself: Agent Ops Tracker (Notion + CSV) - https://renevibe76.gumroad.com/l/ptfvli (pay what you want, minimum $1).

The CSV approach above is fully enough to get started, though. The habit matters more than the tool.

What do you track per agent run? I am curious whether anyone measures cost per useful outcome rather than cost per call.

來源:DEV Community · dev.to