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

# Calling the proxy directly

> Send chat completions through the TensorCost proxy over HTTP, with token exchange, headers, and the refusals you can get back.

The SDK's `steer` and `route` layers call the proxy for you. If you work in a language without an SDK, or want to see exactly what happens on the wire, call it yourself. The proxy forwards your request to your provider, applies your policies, records the call, and returns the provider's response.

The proxy is on the API host:

```text theme={null}
POST https://api.tensorcost.com/api/inference-proxy/v1/chat/completions
```

It is the only model endpoint. The body is forwarded as it is, so you send the provider's own request shape.

The sequence below is one call from start to finish: exchange the key once, then each call passes TensorCost's checks before it reaches the provider.

```mermaid theme={null}
sequenceDiagram
  participant App as Your app
  participant Px as TensorCost proxy
  participant Pr as Provider
  participant Sp as Spend records
  App->>Px: Exchange SDK key
  Px-->>App: Token, 15 minutes
  App->>Px: Chat completion, with provider URL and key
  Note over Px: Checks: compliance, run caps, period cap,<br/>budget, approved models, safety rules
  alt A check refuses
    Px-->>App: 403 with error.code
  else Allowed
    Px->>Pr: Request, or a routed model's
    Pr-->>Px: Response
    Px-->>App: Response and x-tc-decision headers
  end
  Px-)Sp: Record the call as metered spend
```

## Get a token

The proxy does not accept your SDK key directly. Exchange it for a token that lasts 15 minutes, and exchange again before it expires. The exchange endpoint needs no other credential and allows 60 requests a minute per source IP.

```bash theme={null}
curl -X POST https://api.tensorcost.com/api/inference-proxy/sdk-token/exchange \
  -H "Content-Type: application/json" \
  -d "{\"tenant_id\":\"$TENSORCOST_TENANT_ID\",\"sdk_long_lived_token\":\"$TENSORCOST_API_KEY\"}"
```

```json theme={null}
{
  "token": "eyJ...",
  "expires_in": 900,
  "token_type": "Bearer",
  "environment": "production"
}
```

`tenant_id` is your workspace id from **Connect › SDK setup**; the SDK key comes from **Connect › API keys**. The token carries the key's environment and team. A wrong key, a revoked key, or a key that does not belong to the tenant returns 401.

## Make a call

```bash theme={null}
curl https://api.tensorcost.com/api/inference-proxy/v1/chat/completions \
  -H "Authorization: Bearer $TC_TOKEN" \
  -H "Content-Type: application/json" \
  -H "x-tc-provider-url: https://api.openai.com" \
  -H "x-tc-provider-auth: Bearer $OPENAI_API_KEY" \
  -H "x-tc-max-layer: observe" \
  -H "x-tc-customer: acme" \
  -H "x-tc-feature: search" \
  -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}'
```

### Request headers

| Header | Required | Meaning |
| - | - | - |
| `Authorization` | Yes | `Bearer <token>` from the exchange |
| `x-tc-provider-url` | Yes | The provider's bare origin, such as `https://api.openai.com`. Do not add `/v1`; the proxy adds the path. |
| `x-tc-provider-auth` | Yes | Your provider credential, in one of the forms below |
| `x-tc-max-layer` | No | Ceiling for this call: `off`, `observe`, `govern`, `steer` or `route`. Missing means `observe`. Substituting a model needs `steer` or above. |
| `x-tc-correlation-id` | No | Your id for the call. Generated if absent. |
| `x-tc-customer`, `x-tc-feature`, `x-tc-agent-id`, `x-tc-workflow-id`, `x-tc-team-id`, `x-tc-application`, `x-tc-conversation-id`, `x-tc-tags` | No | [Attribution](https://docs.tensorcost.com/developers/attribution.md). `x-tc-tags` is comma-separated. `x-tc-workflow-id` turns on run caps. |
| `X-TensorCost-Cache: bypass` | No | Skip the semantic cache for this call |

The environment is the token's; `x-tc-environment` is ignored for SDK keys.

### Provider credentials

`x-tc-provider-auth` carries the key you would send to the provider yourself.

| Provider | Accepted forms |
| - | - |
| OpenAI and compatible | `Bearer <key>` |
| Azure OpenAI | `Bearer <key>`, sent upstream as `api-key` |
| Anthropic | `Bearer <key>` or `x-api-key <key>`, sent upstream as `x-api-key` with `anthropic-version: 2023-06-01` |

Bedrock and Vertex calls are signed with the cloud account you connected in TensorCost, not with a header you send. Without a connected account, the provider answers 401.

### Allowed providers

`x-tc-provider-url` must be https and one of `api.openai.com`, `*.openai.azure.com`, `api.anthropic.com`, `bedrock-runtime.<region>.amazonaws.com` or `<region>-aiplatform.googleapis.com`. Anything else returns 400.

An Anthropic Messages body sent to this path with `x-tc-provider-url: https://api.anthropic.com` is forwarded to Anthropic's `/v1/messages`. Set `stream: true` to stream; for OpenAI streams to count toward run and period budgets, also send `stream_options: {"include_usage": true}`.

## Response headers

| Header | Value |
| - | - |
| `x-tc-decision-id` | The correlation id of the call |
| `x-tc-decision` | What TensorCost did, as `layer=<layer>; action=<action>; reason=<reason>` |
| `x-tc-routing-outcome` | `routed`, `pass_through_unsafe`, `pass_through_breaker_open`, `pass_through_overloaded`, `pass_through_provider_5xx`, `fallback_route_failed`, `request_rejected`, `policy_dry_run` or `no_policy_match` |
| `x-tc-routed-model` | The model actually called, when known |
| `X-TensorCost-Budget-Warning` | `<budget id>:<ratio>` for a budget close to its cap, comma-joined |
| `X-TensorCost-Period-Budget-Warning` | `<scope>:<period>:<ratio>` for a period budget close to its cap |

## Refusals and errors

TensorCost's own refusals are `403` with a body of the form `{"error": {"code": "...", ...}}`. They are 403 rather than 402 on purpose: a cap you set on yourself is a policy decision, not a payment problem.

| Status | `error.code` | Meaning |
| - | - | - |
| 403 | `RUN_BUDGET_EXCEEDED` | The run reached its cap, and your workspace has enforcement for that layer. The body has `workflow_id`, `cap_cents` and `spent_cents`. |
| 403 | `RUN_STOPPED` | An operator stopped the run. |
| 403 | `PERIOD_BUDGET_EXCEEDED` | A period cap was reached. With `budget_code: "BUDGET_HARD_CAP"` it is a budget's hard cap; `retryable` is true, with a `Retry-After` header, when the spend is not counted yet. |
| 403 | `MARGIN_FLOOR_REACHED` | A customer reached its margin floor. The body has `customer`, `floor_pct`, `cap_cents` and `spent_cents`. See [Every customer keeps a margin](https://docs.tensorcost.com/spend/budgets.md#every-customer-keeps-a-margin). |
| 403 | `MODEL_GOVERNANCE_DENIED` | The model is blocked or not approved. |
| 403 | `COMPLIANCE_DENIED`, `COMPLIANCE_TEAM_MISMATCH` | A compliance profile refused the call. |
| 400 | | Missing `x-tc-provider-url` or `x-tc-provider-auth`, a disallowed provider URL, or a body that is not a JSON object |
| 401 | | The token is missing, invalid or expired. Exchange again. |
| 429 | | More than 600 requests a minute for your workspace |
| 502, 504 | `upstream_unavailable`, `upstream_timeout` | The provider did not answer. A 504 means the call was not retried. |

Any other status is the provider's own, with its body unchanged. Streams are cut after 120 seconds without a chunk.

On the Scale plan a monthly ceiling on proxy requests applies. Past it the proxy answers 402 with `kind: "scale-cogs-exceeded"`. Enterprise has no ceiling.

## Observations without the proxy

If you call the provider yourself and only want TensorCost to record it, post the call to `POST /api/inference-proxy/observation` with the same token. It returns 202. A hard budget cap does not refuse here: it sets `X-TensorCost-Budget-Exceeded` and still records the call. Required fields are `sdk_version`, `provider`, `model`, `operation`, `request_at`, `status` and `correlation_id`; token counts, `customer`, `feature` and the other attribution fields are optional, and a missing cost is priced for you.


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