@withlocus/credits on your server to call tools and bill for usage in Locus Pro Enterprise. The SDK handles idempotency, retries, and cost headers, so your code can focus on the result.
How it works
The SDK holds your secret key and stays on your server. It gives browsers and agents their own limited credentials: a short-lived user token for the widget, and a scoped agent key for an agent.Make a call
1
Install the SDK
The current release of
@withlocus/credits is 0.5.2.Terminal
2
Call an enabled tool
Enable
tavily/search in your workspace and fund the balance, then:Search and read the cost
What the SDK does
Call a tool
Pass aprovider/endpoint slug and the provider’s request body. To find a slug by capability, use MCP search_apis or the tool search API; version 0.5.2 has no search helper.
- Every call returns
cost.creditsandcost.balanceas exact decimal strings. - The SDK makes one idempotency key per call and reuses it during retries. If your application retries after a restart, pass the original
idempotencyKey. - A streamed model response is charged after it finishes, so 0.5.2 reports its cost as
"0". Read the real charge from the stream’s closing event or the call status. See Inference and streaming. - Errors are typed:
LocusInsufficientCreditsErrorfor a402,LocusRateLimitErrorwhen rate limits persist after retries, andLocusApiErrorfor other failures.
Bill your customers
Each customer can have their own balance. You fund it by selling credits through Stripe Checkout or by moving credits from your platform pool. Calls with theuser option charge that balance at your price.
Charge a customer
attributiontags the ledger entry so you can filter usage later withledger().- Transfers cannot overdraw the source account.
topup()funds the platform pool, or a customer when you passexternalUserId. See Funding & billing.
Agent keys
The SDK calls agent keys Agent Connections. Create one on your trusted backend with a secret key that has bothcredentials:manage and credits:move:
Create a scoped connection
connection.credential securely; it is shown only on creation or rotation. Your agent runtime passes it to its own LocusCredits client. Use mode: "end_user" with externalUserId to bind a key to one customer. Manage keys with list(), get(id), rotate(id), and revoke(id).
Tool sets and per-key pricing overrides are available in the API and dashboard, but not in the 0.5.2 typed helper. See Agent keys for scope and budgets.
Framework MCP presets
Presets turn a connection into the configuration your framework expects:Framework presets
Power the widget
Mint a short-lived token for the widget from an authenticated server route:Mint a browser token
fetchCatalog to show customers their allowed tools and final prices. See Render the provider catalog.
Client options
Most applications only need the credential. These options change the defaults:
See the API reference for complete request and response contracts.
Next steps
Widget
Show customers their balance and let them add credits.
Agent keys
Give an agent a scoped key and budget.
Model inference
Pay for model calls from the same balance.
Funding & billing
Choose how customer balances are funded.