跳到正文
DEV Community· rene·· 5 小時前AI 評分61

甚麼會令你願意為 AI 節省成本付費?30 行腳本揭示智能體 token 的去向

What would make you pay for AI cost savings? A 30-line script that shows where your agent tokens go

AI 導讀

作者提供 30 行腳本,分析智能體對話紀錄中的重複內容,估算重複 token 佔總量的比例。腳本以約每 4 個字元折算 1 個 token,並列出重複比例及重複片段。作者提醒,這只是粗略估算,重複比例不等同實際節省額;文中亦列出停止重讀未變檔案、截短工具輸出等免費改善方法。

正文

rene

A recent Ask HN asked: "Would you pay for saving AI cost?" and "what would you need to see before trusting to pay?" My answer: a number measured on my own logs, before and after, that I can reproduce. Not a percentage claim from a landing page.

So here is the measuring part, free. Run it on your own agent transcripts first; decide about fixes after.

Step 1: find where the tokens go

Most coding-agent cost is not your prompt. It is repeated context: the same file dumps, tool output and boilerplate resent every turn. This script takes a JSONL transcript (one message per line, with a content string) and reports which chunks repeat the most.

// token-hotspots.mjs  -  node token-hotspots.mjs transcript.jsonl
import fs from "node:fs";
const lines = fs.readFileSync(process.argv[2], "utf8").split("\n").filter(Boolean);
const approxTokens = s => Math.ceil(s.length / 4); // rough: ~4 chars per token
const seen = new Map();
let total = 0;
for (const l of lines) {
  let m; try { m = JSON.parse(l); } catch { continue; }
  const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content ?? "");
  total += approxTokens(text);
  // fingerprint each 400-char block
  for (let i = 0; i < text.length; i += 400) {
    const k = text.slice(i, i + 400);
    if (k.length < 200) continue;
    seen.set(k, (seen.get(k) || 0) + 1);
  }
}
const repeated = [...seen.entries()].filter(([, n]) => n > 1);
const wasted = repeated.reduce((a, [k, n]) => a + approxTokens(k) * (n - 1), 0);
console.log({ messages: lines.length, approxTotalTokens: total, approxRepeatedTokens: wasted,
  repeatedShare: total ? +(wasted / total).toFixed(3) : 0 });
repeated.sort((a, b) => b[1] - a[1]).slice(0, 5)
  .forEach(([k, n]) => console.log(`${n}x  ${k.slice(0, 80).replace(/\n/g, " ")}...`));

The repeatedShare number is the ceiling on what a de-duplication pass could save for that transcript. If it is 3%, do not pay anyone. If it is 40%, now you know why.

Step 2: the usual fixes, in order of effort

  1. Stop re-reading unchanged files. Cache by path + mtime and send a reference instead of the dump.
  2. Truncate tool output at the source (tail of test logs, head+tail of long diffs).
  3. Pin a short, stable system prompt and move long rules into files loaded on demand.
  4. Summarize old turns instead of replaying them.
  5. Ask for terse output when you do not need prose.

None of these needs a paid tool. The value of a paid checklist is only saving you the hour of working out the order and the thresholds.

Honest limits

  • The 4-chars-per-token estimate is rough; use your provider's tokenizer for billing-grade numbers.
  • Repetition share is not the same as savings: some repeats are cached by the provider at a discount, so check your pricing.
  • I have not measured savings across a population of users; run it on yours.

If you want the checklist version of the fixes above (with the thresholds and the order I use), it is a pay-what-you-want download, $1 minimum: Prompt Token Diet Checklist. The script above is yours either way.

What would you need to see before paying for something like this? I am curious which number convinces you.

來源:DEV Community · dev.to