Cost + token accounting
Set a hard dollar ceiling per run and track token usage with either a zero-dep heuristic or provider-accurate counters.
A long-running agent with tools can make dozens of LLM calls before you see the invoice. costGuard lets you set a ceiling and decide what happens when it's hit β stop cleanly, throw, or warn β so runaway runs don't reach production costs.
#costGuard
import { costGuard } from '@agentskit/observability'
const runtime = createRuntime({
adapter,
observers: [costGuard({ maxUsd: 0.50, onExceed: 'throw' })],
})onExceed: 'throw' | 'stop' | 'warn'.
#multiTenantCostGuard
Same accounting partitioned by tenant for SaaS deployments.
import { multiTenantCostGuard } from '@agentskit/observability'
const guard = multiTenantCostGuard({
budgets: { 'acme-co': 5, 'startup-co': 1 },
defaultBudgetUsd: 0.10, // unlisted tenants
onExceeded: ({ tenant, costUsd, budgetUsd }) => {
metrics.increment('agent.budget.exceeded', { tenant })
// Reject the next request at the gateway, log+drop, etc.
},
})
createRuntime({ adapter, observers: [guard] })
// Wire your request scope: AsyncLocalStorage or set-before-call
guard.setTenant(req.tenant)
await runtime.run(task)Why no auto-abort. SaaS multi-tenant deployments typically reject the inbound request at the gateway, not mid-run. Wire the abort to the controller you already track per request.
#Token counters
import { approximateCounter, createProviderCounter } from '@agentskit/observability'
// Zero-dep heuristic
const fast = approximateCounter()
// Provider-accurate (uses adapter-reported usage when available)
const exact = createProviderCounter({ adapter })#Related
Explore nearby
- PeerObservability
Attach loggers, tracers, and cost guards to any runtime β no code changes beyond adding an observer.
- PeerLoggers + tracers
Attach console, LangSmith, or OpenTelemetry observers to any runtime β mix and match, all receive the same event stream.
- PeerTrace viewer
Generate a self-contained HTML trace file from any run β inspect spans offline without a tracing backend.