> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tensorcost.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SDKs for Node.js and Python

> Install the TensorCost SDK, wrap your provider client and see the first call in the console.

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

```bash Node.js theme={null}
npm install @tensorcost/sdk
```

```bash Python theme={null}
pip install tensorcost
```

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**

```bash theme={null}
export TENSORCOST_API_KEY=...
export TENSORCOST_TENANT_ID=...
```

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

## Wrap a client

> [tab] Node.js
>
> ```ts theme={null}
> import OpenAI from "openai";
> import { wrap } from "@tensorcost/sdk";
>
> const client = wrap(new OpenAI(), {
>   apiKey: process.env.TENSORCOST_API_KEY,
>   tenantId: process.env.TENSORCOST_TENANT_ID,
> });
>
> const reply = await client.chat.completions.create({
>   model: "gpt-4o-mini",
>   messages: [{ role: "user", content: "Hello" }],
> });
> ```

> [tab] Python
>
> ```python theme={null}
> import os
> from openai import OpenAI
> from tensorcost import wrap
>
> client = wrap(
>     OpenAI(),
>     api_key=os.environ["TENSORCOST_API_KEY"],
>     tenant_id=os.environ["TENSORCOST_TENANT_ID"],
> )
>
> reply = client.chat.completions.create(
>     model="gpt-4o-mini",
>     messages=[{"role": "user", "content": "Hello"}],
> )
> ```

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](https://docs.tensorcost.com/developers/control-layers.md).

## 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.

| Environment variable | What it is for | Required |
| - | - | - |
| `TENSORCOST_API_KEY` | Your SDK key | Yes |
| `TENSORCOST_TENANT_ID` | Your workspace id | Yes |
| `TENSORCOST_BASE_URL` | TensorCost API. Default `https://api.tensorcost.com` | No |
| `TENSORCOST_PROXY_URL` | The proxy, for `steer` and `route`. Use `https://api.tensorcost.com` | For `steer` and `route` |
| `TENSORCOST_CONNECTION_ID` | Id of the provider connection the client talks to, up to 64 characters. It is sent with each call; it does not change the environment, which always comes from the key. In code: `connectionId` (Node.js), `connection_id` (Python) | No |
| `TENSORCOST_ENVIRONMENT` | Environment tag on every call, such as `production` | No |
| `TENSORCOST_APPLICATION` | Application the calls belong to | No |
| `TENSORCOST_TAGS` | Comma-separated tags | No |
| `TENSORCOST_TEAM_ID` | Team the calls belong to | No |
| `TENSORCOST_AGENT_ID`, `TENSORCOST_WORKFLOW_ID` | Default agent and run | No |
| `TENSORCOST_CUSTOMER`, `TENSORCOST_FEATURE` | Default customer and feature, for chargeback | No |

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](https://docs.tensorcost.com/developers/attribution.md).

## Supported providers

| Provider | Client | Layers |
| - | - | - |
| OpenAI | `openai` (Node.js 4.x, Python 1.x) | `observe` to `route` |
| Azure OpenAI | `openai` (`AzureOpenAI`) | `observe` to `route` |
| Anthropic | `@anthropic-ai/sdk`, `anthropic` | `observe`, `govern` |
| AWS Bedrock | `@aws-sdk/client-bedrock-runtime`, `boto3` | `observe` |
| Google Vertex AI | `@google/genai`, `google-genai` | `observe` |

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](https://docs.tensorcost.com/developers/cli.md), and for what each layer may do to a call see [Control layers](https://docs.tensorcost.com/developers/control-layers.md).

> [card] [@tensorcost/sdk](https://www.npmjs.com/package/@tensorcost/sdk)
>
> Node.js reference on npm.

> [card] [tensorcost](https://pypi.org/project/tensorcost/)
>
> Python reference on PyPI.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.