Knowing what a call costs#
The newest feature, and the one that changed shape while we built it.
Metered APIs bill per call, and increasingly per token. Once an agent is driving your SDK, the question stops being "did that work" and becomes "what did that just spend, and who spent it".
cost prices each call from whichever source your API actually gives you: a rate table you write, a cost header the server returns, a usage figure in the response body, or a flat per-call unit. It then attributes the spend two ways, to the operation and to the caller, so an invoice can be read as either.
const client = new MyapiSDK({
feature: [
{ name: 'cost', active: true,
path: 'usage.total_tokens', perUnit: 0.00001,
budget: 5.00, onBudget: 'deny' },
{ name: 'retry', active: true, retries: 3 },
],
})
await client.Completion().create({ prompt }, { actor: 'agent:planner' })
client._cost.total // { calls: 1, attempts: 1, amount: 0.0134, ... }
client._cost.actors // { 'agent:planner': { calls: 1, amount: 0.0134 } }
Set a budget and, with onBudget: 'deny', the next operation is refused before an endpoint is resolved, so a runaway loop stops costing money at the point it stops being useful.
Be clear about what that is, though: a cutoff, not a hard cap. The check asks whether you have already spent the budget, not whether this call will exceed it, because what a call costs is usually not known until after it has been made. A reported figure arrives with the response; a retried call is charged per attempt. So the last admitted call can carry you past the number: give it a budget of 2 and hand it one call that prices at 5, and you spend 5. It bounds a run, it does not guarantee a maximum. Size it a call's worth below the number you actually cannot exceed.
The detail worth knowing: money is spent per HTTP attempt, but it is owed by an operation. A call that retried twice cost you three times, because your provider charged three times, and it should still show up as one call. So cost works at both seams, pricing at the transport and attributing at the end of the pipeline. That also means the order matters. Put cost inside cache, as the array above does, or a response served from cache is charged for money nobody spent.