Skip to main content
Hugging Face Inference provides a unified interface to LLMs, embeddings, and other models hosted on Hugging Face and routed providers. Braintrust traces chat completions, text generation, and feature extraction calls.

Setup

Install the Braintrust and @huggingface/inference packages, then set your API keys. Requires @huggingface/inference v2.0.0 or later (any 2.x, 3.x, or 4.x release).
1

Install packages

2

Set environment variables

.env

Auto-instrumentation

To trace Hugging Face Inference SDK calls without modifying your application code, initialize Braintrust normally, then run your app with Braintrust’s import hook to patch the SDK at runtime.
1

Initialize Braintrust and call Hugging Face

2

Run with the import hook

The auto-instrumentation example uses plain JavaScript so node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects — compile your TypeScript to JavaScript, then run the compiled file with the import hook.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

Manual instrumentation

To trace Hugging Face clients manually, wrap them yourself with wrapHuggingFace(). Use this when you want to trace selected clients, or wrap the module directly before constructing clients.
wrapHuggingFace() can wrap either:
  • The module import itself, including InferenceClient, InferenceClientEndpoint, HfInference, and HfInferenceEndpoint.
  • An already-constructed client instance.
If you use routed or custom endpoints via client.endpoint(...), Braintrust records the endpoint URL in span metadata.

What Braintrust traces

Braintrust traces these @huggingface/inference SDK calls, capturing:
  • Chat completions, including streaming chat with first-token timing.
  • Text generation, including streaming text generation with first-token timing.
  • Feature extraction, summarized as embedding count, length, and batch count.
  • Token usage, including prompt, completion, and total tokens.
  • Request metadata, including model, provider, and selected request parameters.
  • Response identifiers, including ID, model, object, created, and finish reason.
  • Routed endpoint URL when calling client.endpoint(...).

Resources