skip to content

With Hugging Face's InferenceClient, how does the key you pass decide whether a request goes through Hugging Face's router and who bills it?

level: middleimportance: should knowfreq 6%

answer

  1. the key's prefix matters
  2. hf_ token versus a provider's own key
  3. router.huggingface.co/<provider> or straight to the partner
  4. bill_to and the X-HF-Bill-To header
  5. auto-router refuses non-Hugging Face keys

basics

~20 s

A Hugging Face token (hf_ prefix) sends the call through Hugging Face's router and bills your Hugging Face account at the provider's rate. A provider's own key makes InferenceClient call that provider directly, and the provider bills you.

solid answer

~40 s

`InferenceClient` in `huggingface_hub` and in `@huggingface/inference` both look at the key. A token starting with `hf_` is a **routed** request: it goes to `router.huggingface.co/<provider>`, Hugging Face bills your account at the provider's rate, and monthly credits apply; `bill_to="my-org"` (JS: `billTo`) adds `X-HF-Bill-To` so a Team or Enterprise organization pays. Any other key is treated as the **provider's own key**: the client calls that provider's API directly, the provider bills you, and the Python client warns that `bill_to` is ignored. With a provider key you must name the provider, because automatic chat routing raises "Cannot select auto-router when using non-Hugging Face API key". A third path keeps the `hf_` token but stores a custom provider key in your Hugging Face settings: still routed, but billed by the provider, without credits.

code

python · 17 lines
python
import os
from huggingface_hub import InferenceClient

messages = [{"role": "user", "content": "ping"}]
model = "meta-llama/Llama-3.1-8B-Instruct"  # always the Hub id

# hf_ token: routed via router.huggingface.co/together, billed to your HF account
routed = InferenceClient(provider="together", api_key=os.environ["HF_TOKEN"])

# same path, but X-HF-Bill-To makes a Team or Enterprise org pay
org = InferenceClient(provider="together", api_key=os.environ["HF_TOKEN"], bill_to="my-org")

# the partner's own key: called directly, billed by the partner
direct = InferenceClient(provider="together", api_key="<together-api-key>")

for client in (routed, org, direct):
    client.chat_completion(messages, model=model, max_tokens=5)

go deeper

for a junior

Recall that InferenceClient takes a provider, a Hub model id and a key, and that an hf_ token is the normal key.

for a middle

Explain how the key's prefix picks routed versus direct calls, who bills each, and how bill_to adds the X-HF-Bill-To header for an organization.

for a senior

Diagnose misrouted spend and auto-router errors from the deployed key, saved custom keys and billing headers, and choose the path deliberately per workload.

for a principal

Decide where the inference bill and the credentials should live for a whole organization: one routed account with spending limits, or direct provider contracts with more secrets to govern.

## One client, two network paths `InferenceClient` exists in Python (`huggingface_hub`) and in JavaScript (`@huggingface/inference`). Both accept a `provider` (a partner name such as `"together"`, `"hf-inference"`, or the default `"auto"`), a Hub **model id**, and a key passed as `token` or its OpenAI-style alias `api_key` (JavaScript: the first constructor argument). The model id is always the Hub id; the client looks up each provider's own id from the Hub's provider mapping. What surprises people is that the **key itself chooses the network path**. The clients check its prefix: - **Starts with `hf_`** — a Hugging Face user access token. The client sends the request to `https://router.huggingface.co/<provider>`, Hugging Face authenticates you and forwards the call with its own credentials for that partner. The JavaScript client labels this auth method `hf-token`. - **Anything else** — treated as the partner's own API key. The client logs that it is calling the provider directly and sends the request to the provider's base URL; Hugging Face is not in the path. The JavaScript client labels this `provider-key`. If you pass no key at all, the Python client falls back to the locally saved token from `hf auth login` or `HF_TOKEN`. ## Who pays on each path | Setup | Path | Billed by | Monthly credits | |---|---|---|---| | `hf_` token | routed through Hugging Face | your Hugging Face account, at the provider's rate | yes | | `hf_` token plus `bill_to="my-org"` | routed | that Team or Enterprise organization | the organization's pool | | `hf_` token plus a custom provider key saved in Hub settings | routed; Hugging Face swaps in your key | the provider | no | | the provider's own key passed to the client | direct to the provider | the provider | no | A few mechanics behind the table: 1. **`bill_to` is a header.** The Python argument `bill_to` and the JavaScript option `billTo` both set `X-HF-Bill-To`. Raw HTTP and the OpenAI SDK set that header themselves. The token's owner must belong to the organization. 2. **`bill_to` does nothing on the direct path.** The Python client warns that a non-Hugging Face key means the provider bills directly and the `bill_to` value will be ignored. 3. **Saved custom keys keep your code unchanged.** You still send your `hf_` token; Hugging Face swaps the authentication when it routes, and the provider bills you, so credits no longer apply. ## The automatic-selection trap With `provider` left at its default, a **chat completion** goes to Hugging Face's server-side auto-router, which the client only uses with a Hugging Face token. Pass a partner's key with no provider and the client raises `Cannot select auto-router when using non-Hugging Face API key.` Name the provider explicitly whenever you use that provider's key. For tasks other than chat, automatic selection is resolved by the client itself: it fetches the model's provider mapping and takes the first provider in your preference order from the Inference Providers settings. ## Choosing a path deliberately - **Routed with an `hf_` token** is the default for teams: one credential, one bill, credits, and the freedom to switch providers by changing one argument. - **Organization billing** keeps personal tokens but moves spend to the organization, which can also set a spending limit and disable providers from its settings. - **A provider's own key** suits a team that already has a contract and committed spend with that partner, or needs a provider feature outside Hugging Face's integration; the cost is one more secret to manage and no Hugging Face-side usage view. ## Small API details that bite - **`token` and `api_key` are aliases.** Passing both to the Python client raises a `ValueError`; pass one. - **No boolean tokens.** Since `huggingface_hub` v2.0 the client takes a token string or `None`; `token=True` raises a `TypeError`. - **The default key is ambient.** With no key argument the client uses whatever token the machine is logged in with, so a CI runner logged in as a personal account bills that person unless `bill_to` says otherwise. ## Reading a surprise invoice When spend lands in the wrong place, check, in order: the key's prefix in the deployed environment, whether a custom provider key is saved in the Hugging Face settings of the token's owner, and whether `bill_to` or `X-HF-Bill-To` is set on the requests. Usage per model and provider for the past month is shown in the Inference Providers settings, for users and for organizations on a paid plan.

  • You keep your `hf_` token in code but want the partner to bill you under your existing contract. What changes?
    Save your provider key as a custom key in the Hugging Face Inference Providers settings. Requests stay routed through Hugging Face with your unchanged code and token; Hugging Face swaps the authentication when forwarding, the provider bills you, and Hugging Face monthly credits stop applying to those calls.
  • Does `@huggingface/inference` behave the same way in JavaScript?
    Yes. It classifies an access token starting with `hf_` as an `hf-token` and anything else as a `provider-key`, routes the first through Hugging Face and sends the second straight to the provider, and rejects a non-Hugging Face key for automatic chat routing. Organization billing is the `billTo` option, which sets `X-HF-Bill-To`.

  • Every InferenceClient request goes through Hugging Face whatever key you pass.
  • bill_to also moves spend when the client holds a provider's own key.
  • The model argument must be the provider's model id when a provider is set.
  • A provider key works with the default automatic provider selection for chat.
  • A custom provider key saved in Hub settings still draws on monthly credits.