@agentpos/measure

中文文档
0.1.5发布于 2026/10/29Apache-2.0TypeScriptES Module零依赖构建来源已验证近 30 天下载 1950

AgentPOS usage measurement: one usage_events row per model call, with tokens, latency, integer cost in USD micros and the traceId of its run. Price table as configuration, CSV export and reader.

原始使用文档
文档目录4 节
  1. What it guarantees
  2. Prices
  3. Storage
  4. Shared with the sibling repositories

@agentpos/measure

One usage_events row per model call: tokens as the provider reported them, latency, the model, the integer cost in USD micros and the traceId of the run. Prices are configuration, not code.

Why it exists: AgentPOS sells assisted setup and a hosted buying agent, and until this package there was no number behind either. A model call that nobody measured is a cost nobody can explain later.

import {
  MemoryUsageEventsRepo,
  measured,
  loadPriceTable,
  usageEventsToCsv,
} from "@agentpos/measure";

const repo = new MemoryUsageEventsRepo(); // SqliteUsageEventsRepo(path) in production
const ctx = { repo, prices: loadPriceTable() };

const { value } = await measured(
  ctx,
  {
    traceId: "scan-7f3",
    stage: "extract",
    provider: "anthropic",
    modelId: "claude-opus-5",
    simulated: false,
  },
  async () => {
    const result = await callTheModel();
    return { value: result, usage: { inputTokens: 1_200, outputTokens: 340 } };
  },
);

console.log(usageEventsToCsv(repo.list()));

In this repository the wrapper you normally want is measuredLlm from @agentpos/core, which applies the above to any LlmProvider.

What it guarantees

  • A failed call is a row too. measured writes outcome: "error" with the error's code and rethrows the error untouched. Failed attempts are real spend.
  • Integers end to end. Prices are converted once, at load, to USD micros per million tokens; every cost after that is integer arithmetic, rounded half up once over the whole call. No floats touch money.
  • Unknown models are visible, not free. A model missing from the price table costs 0 with costKnown: false, so it shows up as unknown instead of hiding as zero.
  • simulated is written, never inferred. A row from a recorded fixture says so.
  • A malformed price table fails loudly. Ignoring MODEL_PRICES_JSON would publish numbers at the wrong price, so a bad value throws MEASURE_INVALID_PRICE_TABLE instead.

Prices

MODEL_PRICES_JSON holds inline JSON or a path to a JSON file, in USD per million tokens:

{ "claude-opus-5": { "inputPerMillionUsd": 5, "outputPerMillionUsd": 25 } }

cacheReadPerMillionUsd and cacheWritePerMillionUsd are optional and default to the input rate, so an unverified cache discount can never make a run look cheaper than it was.

The built-in defaults cover the Nemotron models used by the reasoning buyer and are secondhand and unverified: check them against the provider's console and record the date before publishing any figure that comes from them.

Storage

Behind the storage adapter the repository already uses: MemoryUsageEventsRepo for tests, SqliteUsageEventsRepo (node:sqlite, no native dependency) for runs. Opening the SQLite adapter applies its own DDL, so there is no migration step.

usageEventsToCsv and csvToUsageEvents round-trip the table, so a committed CSV can rebuild every figure a README or a grant report quotes.

Shared with the sibling repositories

The same table exists in agentpos-alexa and agentpos-doorstep, and was written first for the reasoning buyer (agentpos-buyer). This package is the single implementation the four consume, so the four report the same numbers. Stages are a closed union: a new consumer adds its own to UsageStage rather than passing a string.

版本与发布记录

@agentpos/measure 共发布 7 个版本,首次发布于 2026/10/27;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.5latest2026/10/299 天前
0.1.42026/10/2811 天前
0.1.32026/10/2811 天前
0.1.22026/10/2711 天前
0.1.12026/10/2712 天前
0.1.02026/10/2712 天前

关于 @agentpos/measure 的常见问题

@agentpos/measure 是什么?

AgentPOS usage measurement: one usage_events row per model call, with tokens, latency, integer cost in USD micros and the traceId of its run. Price table as configuration, CSV export and reader.

如何安装 @agentpos/measure?

在项目目录运行 npm install @agentpos/measure,或使用 pnpm add / yarn add / bun add。

@agentpos/measure 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@agentpos/measure 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@agentpos/measure 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.5。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@agentpos/measure 可以商用吗?

@agentpos/measure 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@agentpos/measure 的下载量有多少?

根据 npm 官方下载统计,@agentpos/measure 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,950 次,日均约 65 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@agentpos/measure 还在维护吗?

最新版本 0.1.5 发布于 2026/10/29(9 天前);至今共发布 7 个版本,其中过去 12 个月发布了 7 个;首次发布于 2026/10/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。