Skip to main content
The SDK wraps your OpenAI, Anthropic, AWS Bedrock or Google Vertex AI client. Your code calls the provider exactly as before; each call is reported to TensorCost with its cost, model and who it was for. Prompts and completions never leave your process. If TensorCost is slow or down, your call still goes through. Telemetry is sent in the background.

Install

Node.js needs version 18 or later. Python needs 3.9 or later.

Get your keys

1

Mint an SDK key

In the console open Connect › SDK setup and choose Mint SDK key. The key is filled into the snippet on that page and shown once, so copy it then. In the form you pick the key’s environment, which starts on your production one. You need the workspace admin role. After you mint a key, the button is replaced by a note to copy it until your first call is counted. Then the key leaves the page and the button comes back. Mint more, and list or revoke them, on Connect › API keys.
2

Take your workspace id

The snippet on Connect › SDK setup already carries your workspace id (the SDK calls it the tenant id).
3

Set both in your environment

The SDK trades the key for a short-lived token and refreshes it on its own.

Wrap a client

Anthropic, Bedrock and Vertex clients are wrapped the same way. Without a tenant id, observe warns once and sends nothing. The default layer is observe: telemetry only. To refuse or reroute calls, read Control layers.

See the first call

Connect › SDK setup watches for your first call for five minutes after you open it. When one arrives it shows the call’s model, cost and how long ago it ran. If nothing arrives, the page says so and offers Check again. Check that TENSORCOST_API_KEY, TENSORCOST_TENANT_ID and TENSORCOST_BASE_URL are set in the shell that runs your code, and that the key belongs to this workspace.

Configuration

Options passed to wrap() win over environment variables, which win over defaults. The Node.js SDK also reads TENSORCOST_DEPLOYMENT and TENSORCOST_PROMPT_TEMPLATE_ID. The Python SDK reads TENSORCOST_AUTO_WORKFLOW_ID; set it to 1 to give each wrapped client with an agent a fresh run id. What each field does is in Attribution.

Supported providers

Only the calls the SDK wraps are observed; for example, Node.js observes OpenAI chat completions but not embeddings, while Python also covers embeddings and responses. Bedrock and Vertex sign requests inside their own SDKs, so they cannot go through the proxy. Anthropic at steer or route runs at govern. The Node.js SDK also has wrapLangChain, wrapLlamaIndex, wrapCrewAI and wrapVercelAi.

What is sent

Metadata only: provider, model, operation, token counts, timings and status; the environment, application, tags, team, agent, run, customer and feature you set; and the provider account the call was billed under, read from the provider’s response. TensorCost uses that account to match each call with the provider’s own bill, so discounts and credits reach your cost figures. Your provider key is never sent.

Full reference

Errors, retries, serverless helpers and every option are on the package pages. For the command line see CLI, and for what each layer may do to a call see Control layers.

@tensorcost/sdk

Node.js reference on npm.

tensorcost

Python reference on PyPI.