The Stigg Agent SDK is in public beta and its API may change before general availability.
Installation
How it connects to Stigg
The Agent SDK builds on two Stigg clients and picks one for each operation. You never call either of them directly.
On an account without Governance, the SDK uses the Node server SDK alone, at customer level. For caching, see Caching and accuracy. For concepts, see AI agents and Usage governance.
StiggAgentSDK
init(). If it fails, for example because Stigg is briefly unreachable, a later call tries again after a short wait, so the SDK recovers without a restart.
For example, new StiggAgentSDK({ apiKey, baseUrl: 'https://stigg.internal', ingest: 'sync' }). The shorthand takes the API key first and the other options second, for example new StiggAgentSDK(apiKey, { ingest: 'sync' }).
Methods
In the order you typically use them: wrap a client (orfetch), register an agent, check and report anything a wrapped client does not cover, read the summary, flush before the process exits, and deregister agents you no longer need.
wrap
client. Every model call it makes is checked before the call (when the context sets entitlement) and its usage is reported after it. The client you passed in is not changed. Synchronous.
Each covered method also accepts a stiggContext field in its own parameters, which overrides the client’s default for that call. A client wrapped with no customerId can serve every customer, and then each call must name its own.
The LLM or gateway client to wrap, for example
new Anthropic(). See Supported providers and gateways.stiggContext has a field the SDK does not recognize, such as a misspelled agentId, rather than ignore it.
wrapFetch
fetch that governs plain HTTP calls to a gateway, for code that has no client to wrap. Each request the recipe covers is checked before it is sent and reported from its response. Any other request passes through. Each request also accepts a stiggContext field, merged over the default and never sent to the gateway. See A gateway you call over HTTP.
register
The Governance entity type to create it as, for example
agent. It must already exist. See Setting up governance.Your own key-value data to store on the agent’s entity, for example
{ owner: 'research-lead' }. Values must be strings. See Entities.The agent’s budget and place in the tree for one credit type or metered feature (a Governance entity entitlement). Omit it to save only the agent’s entity.
GovernanceNotEnabledError when the input sets a budget and Governance is not enabled on the account. Without a budget, it logs a warning instead and returns.
check
Who the check is for and what it is checked against. Must include
customerId and entitlement. See StiggContext.How much the action is about to use. See Requested usage.
The decision. When access is denied,
check() returns a decision with hasAccess: false rather than throwing an AccessDeniedError, so you can branch on it directly.report
Who the usage is attributed to. Must include
customerId. See StiggContext.The usage to report, keyed by Stigg feature or credit type ID, for example
{ 'feature-web-searches': 1 }.The ingest result. A failed report does not throw: the result has
ok: false, and the error is passed to onError.summary
flush
stigg.flush() only. After flushing, also release the SDK’s connections, like close.stigg.flush() returns one summary per wrapped client. governed.stigg.flush() returns that client’s summary.deregister
Other methods
init
close
client is not closed.
denial
err is an AccessDeniedError, otherwise undefined. Use it in a catch block to tell denied access apart from other errors. The same as isAccessDenied(err).
governanceHeaders
x-stigg-customer-id, x-stigg-dimensions, x-stigg-attribution-key and x-stigg-run-id headers for a context, with no wrapped client involved. See A gateway you operate.
ingestFromHeaders
x-stigg-* request headers and reports the usage the proxy observed. It never checks access.
The ingest result.
StiggContext
Says who a call is for and how it is checked and reported. Pass it towrap() as the default for every call through a client, and inside any call’s own parameters to override it for that call. Every field is optional in either place, but each call must end up with a customerId. A field the SDK does not recognize, such as a misspelled agentId, throws an error rather than being ignored.
The credit type or metered feature the call is checked against. Omit it to meter without checking.
The agent the call is attributed to. Requires
attributionKey. See Usage governance.The attribution key of the agent’s entity type, for example
agentId. The agent ID is sent under this key on every check and report.More attribution, such as a user or a model, sent on every check and report. Merged key by key when overridden.
Overrides the SDK’s fallback strategy.
What to report. Defaults to the vendor’s own metrics. See Usage mappers.
Overrides the SDK’s ingest mode.
The event name in
async mode. Defaults to the vendor call, such as anthropic.messages.create.AccessDecision
What a check returns: fromcheck(), from stigg.denial(err), and to the onDecision hook.
Whether access is granted and the call may go ahead. The same as
hasAccess in an entitlement check.Why access was denied, such as
RequestedUsageExceedingLimit or InsufficientCredits.What would let the call through. See Remedies are product decisions.
What produced the decision.
policy means the check could not complete and the fallback strategy decided. unenforced means no entitlement was set.The SDK made the decision from live data, not Stigg. For a credit check, Stigg returns the balance and the SDK compares it with the requested usage. Can be
true together with isFallback.IngestResult
What a usage report returns.Which ingest mode was used.
sync only: the credits the report used, when what it reported draws a credit balance. Not set when the reported features are not mapped to credits.Helpers
Errors
isAccessDenied(err) returns the decision for an AccessDeniedError and undefined otherwise. isGovernanceNotEnabled(err) and isEngineError(err) check the other two. They work without instanceof, so they are safe across bundles.