A spend gate for AI agents on metered data APIs: count, random sample, approved run, enforced by one Claude Code hook
说明与用途
一种自动化方案,帮助 AI 代理安全使用计费数据 API,通过三步支出门控:计数、带种子的随机样本以及经批准的完整运行。
问题
AI agents can unintentionally incur high costs when calling metered data APIs due to default parameters and lack of pre‑execution checks.
结果
The spend gate reduced accidental high‑cost runs by ensuring agents first obtain a free count, then a representative sample, and only proceed to a full run after explicit approval. Sample runs showed realistic cost estimates and data quality metrics.
工作流
- 1. Agent requests a count (mode: "count") to get total rows and confirm no charge per row.
- 2. Agent requests a random sample (mode: "people", maxResults <= 100, sampleSeed set) to estimate cost and data quality.
- 3. Agent requests full run (mode: "people", maxResults > 100) only after receiving approval from the hook.
- 4. The PreToolUse hook checks mode and maxResults, allows free modes, permits capped samples, and blocks or approves full runs based on user approval.
证据
The filter is the one from the job‑change part of the series: sales and revenue leaders at director level and above, employers of 50 to 5,000 people, US, UK and Germany, current role started in the last twelve months.
The same filter counted 21,834 on 10 October. Four days, 536 more people, because start dates reach the database as people update their profiles.
Run log: 12.6 seconds, charged events person-row: 100, apify-actor-start: 1, nothing on the profile or contacts tiers.
The hook that enforces it: Claude Code runs PreToolUse hooks before every tool call that matches a pattern, and an exit code of 2 blocks the call and hands the script's stderr back to the agent as the reason.
- 记录日期: 2026-10-14T17:56:22.743Z