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

# Developer tools

> The TensorCost SDKs — one-line wrappers that add cost observability to your OpenAI, Anthropic, or Bedrock client, in Node.js and Python.

# Developer tools

If you're calling OpenAI, Anthropic, or Bedrock directly from your own code (as opposed to connecting your cloud account through the console), the SDKs are the fastest way to get that traffic showing up in TensorCost. Wrap your existing client in one line; nothing else about your code changes.

<Note>
  Looking for the terminal tool that estimates prompt cost without any account or network access? That's the [CLI](/cli) — a separate, simpler tool.
</Note>

## Node.js — `@tensorcost/sdk`

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

Requires Node.js 18+ (uses native `fetch`).

```ts theme={null}
import OpenAI from "openai";
import { wrap } from "@tensorcost/sdk";

const client = wrap(new OpenAI({ apiKey: process.env.OPENAI_API_KEY }));

// Use the client exactly as before — TensorCost observes every call
// and ships fire-and-forget observations to the platform.
const resp = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "hi" }],
});
```

Anthropic and Bedrock work the same way:

<CodeGroup>
  ```ts Anthropic theme={null}
  import Anthropic from "@anthropic-ai/sdk";
  import { wrap } from "@tensorcost/sdk";

  const client = wrap(new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY }));

  await client.messages.create({
    model: "claude-3-5-sonnet-20241022",
    max_tokens: 256,
    messages: [{ role: "user", content: "hi" }],
  });
  ```

  ```ts Bedrock (observe-only) theme={null}
  import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
  import { wrap } from "@tensorcost/sdk";

  const client = wrap(new BedrockRuntimeClient({ region: "us-east-1" }));

  const response = await client.send(
    new InvokeModelCommand({
      modelId: "anthropic.claude-3-haiku-20240307-v1:0",
      body: JSON.stringify({
        anthropic_version: "bedrock-2023-05-31",
        max_tokens: 64,
        messages: [{ role: "user", content: "What is 2 + 2?" }],
      }),
      contentType: "application/json",
      accept: "application/json",
    }),
  );
  ```
</CodeGroup>

For Bedrock, `@aws-sdk/client-bedrock-runtime` is an optional peer dependency — install it only if you're using Bedrock:

```bash theme={null}
npm install @aws-sdk/client-bedrock-runtime
```

`InvokeModelCommand` and `ConverseCommand` report full token counts. `InvokeModelWithResponseStreamCommand` and `ConverseStreamCommand` currently report latency and success/error only — per-chunk token accounting for streaming Bedrock calls is on the roadmap.

### Configuration

| Variable                 | Purpose                                      | Required                                      |
| ------------------------ | -------------------------------------------- | --------------------------------------------- |
| `TENSORCOST_API_KEY`     | Your TensorCost API key, from the console    | Yes                                           |
| `TENSORCOST_BASE_URL`    | Backend base URL                             | No — defaults to `https://api.tensorcost.com` |
| `TENSORCOST_TENANT_ID`   | Explicit tenant ID                           | No                                            |
| `TENSORCOST_ENVIRONMENT` | Environment tag stamped on every observation | No                                            |

Or pass options explicitly: `wrap(client, { apiKey: "...", baseUrl: "..." })`.

### Applied mode (routing through TensorCost)

By default the SDK is observe-only — your call goes straight to the provider, and a fire-and-forget observation ships afterward. Setting `appliedMode: true` (plus a `proxyUrl`) instead routes OpenAI and Anthropic calls through TensorCost's inference proxy, which can redirect a request to a different provider per your policy. Applied mode is **not** available for Bedrock — Bedrock's request signing happens inside the AWS SDK before our wrapper can intercept it, so `wrap(bedrockClient, { appliedMode: true })` throws immediately.

In applied mode you can also configure retries, timeouts, lifecycle hooks, and a fail-open circuit breaker (opens after 3 consecutive proxy failures, routes direct to the provider, closes again after 5 consecutive successful probes). See the package README on npm for the full option list and typed error classes.

### What gets sent

Only metadata — provider, model, operation, timestamps, token counts, status, and a correlation ID. Prompt and completion content are never captured.

### Fail-open guarantee

If TensorCost is slow, down, or misconfigured, your underlying provider call still completes normally — the SDK logs a warning and moves on. The one exception is a missing API key, which throws at `wrap()` time rather than silently dropping every observation.

## Python — `tensorcost`

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

Runtime dependency: `httpx` only.

```python theme={null}
from openai import OpenAI
from tensorcost import wrap

client = wrap(OpenAI(api_key="sk-..."))

resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "hello"}],
)
```

Anthropic works identically:

```python theme={null}
from anthropic import Anthropic
from tensorcost import wrap

client = wrap(Anthropic(api_key="sk-ant-..."))
client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=128,
    messages=[{"role": "user", "content": "hello"}],
)
```

### Configuration

`wrap()` resolves config from, in order: explicit keyword arguments, then environment variables (`TENSORCOST_API_KEY`, `TENSORCOST_BASE_URL`, `TENSORCOST_TENANT_ID`, `TENSORCOST_PROXY_URL`), then defaults (`base_url` defaults to `https://api.tensorcost.com`). A missing API key raises `MissingConfigError`.

### Applied mode

Same idea as the Node SDK: `applied_mode=True` plus `proxy_url` routes `chat.completions` / `messages.create` through the TensorCost inference proxy, which can redirect the call based on tenant policy. If the proxy call fails, the SDK falls back to the direct provider call and logs a warning rather than failing your request.

### Currently supported (Python)

* OpenAI (`openai >= 1.0`) — `chat.completions.create`, `completions.create`
* Anthropic (`anthropic >= 0.20`) — `messages.create`

Bedrock, Azure OpenAI, Vertex AI, streaming responses, tool/function calling, async clients, and LangChain/LlamaIndex adapters are on the roadmap for the Python package — the Node SDK is ahead on Bedrock support today.

## MCP

TensorCost also exposes an MCP endpoint for agent-based access — see [real-time events](/realtime-events#mcp) for the tool surface and authentication.
