Skip to main content

tensorcost — Python SDK

One-line wrapper SDK for TensorCost. Adds fire-and-forget cost observability and optional inference control to OpenAI, Anthropic, AWS Bedrock, and Google Vertex AI clients without changing how your code calls the underlying provider.

Install

pip install tensorcost

Runtime dependencies: httpx only.

One-line example

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:

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 in this order:

  1. Explicit kwargs to wrap(client, api_key=..., base_url=..., ...)
  2. Environment variables: TENSORCOST_API_KEY, TENSORCOST_BASE_URL, TENSORCOST_TENANT_ID, TENSORCOST_PROXY_URL, etc.
  3. Defaults: base_url defaults to https://api.tensorcost.com

If no api_key is found, wrap() raises MissingConfigError. Same if max_layer is steer or route and no proxy_url is available.

Control layers (max_layer)

v1.0 replaces the old applied_mode boolean with a control-layer ceiling. A layer is a ceiling, not a mode — the effective layer is always min(your max_layer, console published layer).

Layer Behavior
off No SDK wiring; client returned unchanged
observe Telemetry only (default when max_layer is omitted)
govern Metadata-only POST /admit before the provider call
steer Proxy on the path; proxy may steer without full live routing
route Full applied-mode routing through the inference-proxy
from openai import OpenAI
from tensorcost import wrap

client = wrap(
    OpenAI(api_key="sk-..."),
    api_key="tc-...",
    max_layer="route",
    proxy_url="https://api.my-instance.tensorcost.com",
)

The SDK fetches the console-published ceiling from GET /api/inference-proxy/v1/sdk-layer (30s cache, decays to govern after 5 minutes unreachable). Govern refusals and proxy decisions surface in the x-tc-decision response header.

applied_mode=True still works but is deprecated — it maps to max_layer="route" with a warning. Prefer max_layer in new code.

Proxy hardening

When max_layer is govern, steer, or route, you can configure retries, timeouts, lifecycle hooks, and the fail-open circuit breaker.

from tensorcost import wrap, RetryConfig

client = wrap(
    OpenAI(api_key="sk-..."),
    max_layer="route",
    proxy_url="https://...",
    retry=RetryConfig(max_attempts=5, base_delay_ms=500.0, max_delay_ms=30_000.0),
    timeout_s=30.0,
    fail_open_enabled=True,
)

Typed errors: TensorCostError, TensorCostNetworkError, TensorCostTimeoutError, TensorCostProxyError, TensorCostQuotaError, TensorCostProviderError.

Agent-run attribution

client = wrap(
    OpenAI(api_key="sk-..."),
    agent_id="support-bot",
)

task_client = client.with_meta(workflow_id="ticket-48291")
task_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Summarize this ticket"}],
)

Applied-mode proxy calls forward x-tc-agent-id / x-tc-workflow-id. with_meta() is supported on OpenAI and Anthropic wrapped clients; Bedrock and Vertex accept wrap-time defaults only.

Fail-open guarantee

If TensorCost is unreachable, slow, or returns an error, your underlying provider call still completes normally. The SDK logs a warning via logging.getLogger("tensorcost") and moves on.

What gets sent

For every intercepted call we POST metadata only — never prompt or completion content:

  • SDK version, provider, model, operation, modality
  • Timestamps, token counts, correlation UUID, status
  • Environment, agent/workflow ids, chargeback tags when configured

Authentication uses a short-lived JWT from POST /api/inference-proxy/sdk-token/exchange.

Currently supported

  • OpenAI (openai >= 1.0) — chat, completions, embeddings, responses
  • Anthropic (anthropic >= 0.20) — messages
  • AWS Bedrock (boto3 bedrock-runtime) — invoke/converse (+ streaming placeholders). Observe/govern only — max_layer="steer" or "route" raises MissingConfigError.
  • Google Vertex AI (google-genai with vertexai=True) — generate_content (+ streaming). Observe/govern only.
  • Azure OpenAI — wrap AzureOpenAI (tagged provider: openai)

Development

pip install -e '.[dev]'
pytest

See PUBLISHING.md for the PyPI release runbook.

License

Apache-2.0. See LICENSE for the full text.

Metadata

Release files for tensorcost 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tensorcost 1.0.0
File Size Uploaded Provenance
tensorcost-1.0.0.tar.gz 60.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tensorcost 1.0.0
File Interpreter ABI Platform
tensorcost-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 108.8 kB

Release files / tensorcost-1.0.0.tar.gz

Download URL tensorcost-1.0.0.tar.gz
Size 60.4 kB
Tags Source
SHA-256 checksum
How to use checksums
49eb16a1f0cf87a4ca16b3609a3cb0d1bcc08876be68ac5d7d97a46efdceaa26
BLAKE2b-256 checksum
How to use checksums
3d1495f023ac8ec37f55b8d5617135c4c931ef7157c99535bc73025431ad0cc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.13

Release files / tensorcost-1.0.0-py3-none-any.whl

Download URL tensorcost-1.0.0-py3-none-any.whl
Size 48.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4bd7833c9c4d4d27db90846ed61b64e7d19e35ef384046ae6efade5861ccd6f3
BLAKE2b-256 checksum
How to use checksums
f91fa50ab0c8e39a15c8ac75fed7cfe4ebaa6b881dfc26c7ac7341cee93f4421
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.13

Security for tensorcost 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page