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
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 withwrapHuggingFace(). 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, andHfInferenceEndpoint. - An already-constructed client instance.
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(...).