@useautumn/gateway package integrates Autumn with the OpenRouter SDK, automatically tracking token usage for every chat.send call. No manual trackTokens calls needed.
Using OpenRouter through the Vercel AI SDK (
@openrouter/ai-sdk-provider)? Use the AI SDK wrapper with providerId: "openrouter" instead.Setup
1. Install the package
Requires
autumn-js and @openrouter/sdk as peer dependencies.2. Wrap your client
UsewithAutumn to wrap your OpenRouter client. It intercepts chat.send calls, enables OpenRouter’s usage accounting on every request, reads the token usage from the response, and reports it to Autumn automatically.
3. Use as normal
The wrapped client works exactly like a regular OpenRouter client — streaming and non-streaming. Token usage is tracked in the background after each call.Token pools
The wrapper normalizes OpenRouter’s usage accounting into the exclusive token pools that trackTokens expects: text input (excluding cached and audio tokens), text output (excluding reasoning tokens), cache reads, cache writes, audio input, and reasoning tokens. Each pool is billed at the model’s published rate. The model is reported to Autumn asopenrouter/<slug> (e.g. openrouter/openai/gpt-4o), using the resolved model from the response — so router aliases like openrouter/auto bill against the model that actually served the request. Autumn prices usage with OpenRouter’s rates from Models.dev, and OpenRouter’s own reported cost is attached to each event as the openrouter_cost property for reconciliation.
Options
| Parameter | Type | Required | Description |
|---|---|---|---|
autumn | Autumn | Yes | Your Autumn SDK client instance |
openRouter | OpenRouter | Yes | The OpenRouter SDK client to wrap |
customerId | string | Yes | The Autumn customer ID to attribute usage to |
featureId | string | No | Target a specific AI credit system feature. Auto-detected if you only have one |
entityId | string | No | Entity ID for entity-scoped balance tracking |
properties | Record<string, unknown> | No | Additional properties to attach to each usage event |
Manual tracking
If you consume OpenRouter through a path the wrapper doesn’t cover (e.g.callModel, or raw fetch against the REST API), use trackOpenRouterUsage directly with the response’s usage object — it accepts both the SDK’s camelCase models and the raw snake_case API shape: