Use init() to setup the Gentrace SDK with your API credentials and configuration
The init() function initializes the Gentrace SDK with your API credentials and configuration options. This function must be called early in your application setup to configure the SDK globally before using any other Gentrace functionality.Starting with the latest SDK versions, init() also automatically configures OpenTelemetry by default, eliminating the need for manual OpenTelemetry setup in most cases.
import { init } from 'gentrace';// Basic initialization with API keyinit({ apiKey: 'your-gentrace-api-key',});// Or using environment variableinit({ apiKey: process.env.GENTRACE_API_KEY,});
Stainless SDK Generation: The parameters and client options
described in this section are automatically generated by the
Stainless API SDK generation platform.
Your Gentrace API key. If not provided, the SDK will attempt to use the GENTRACE_API_KEY environment variable.Generate an API key at https://gentrace.ai/s/api-keys
Override the default base URL for the API.Defaults to GENTRACE_BASE_URL environment variable or https://gentrace.ai/api.This is particularly useful for self-hosted Gentrace instances (e.g., http://self-hosted-gentrace.example.com/api)
Custom sampler instance. Defaults to new GentraceSampler() which samples traces with gentrace.sample=true in baggage. Gentrace wrapper functions add this flag automatically.When running experiments, the SDK also adds gentrace.in_experiment=true to the baggage to mark test spans, preventing production evaluations from running on test data.
import { init } from 'gentrace';// OpenTelemetry is automatically configured by defaultinit({ apiKey: process.env.GENTRACE_API_KEY,});// Same as explicitly setting otelSetup: trueinit({ apiKey: process.env.GENTRACE_API_KEY, otelSetup: true,});
import { init } from 'gentrace';// Disable automatic OpenTelemetry configurationinit({ apiKey: process.env.GENTRACE_API_KEY, otelSetup: false,});// You must configure OpenTelemetry manually// See the OpenTelemetry Setup guide for details
The init() function will throw an error if required configuration is missing:
error-handling.ts
Copy
Ask AI
import { init } from 'gentrace';try { init({ // Missing apiKey - will throw error if GENTRACE_API_KEY env var not set });} catch (error) { console.error('Failed to initialize Gentrace:', error.message);}
Automatic OpenTelemetry Setup: Starting with the latest SDK
versions, init() automatically configures OpenTelemetry by
default. This eliminates the need for manual OpenTelemetry setup in
most cases. If you need custom OpenTelemetry configuration or want
to disable automatic setup, use the otelSetup (TypeScript) or
otel_setup (Python) parameter.
Call init() before your main execution logic to ensure Gentrace is configured before using any tracing functionality:
early-init.ts
Copy
Ask AI
import { init, experiment, evalOnce } from 'gentrace';// Initialize before using Gentrace functionalityinit({ apiKey: process.env.GENTRACE_API_KEY,});// Now you can use Gentrace functionsasync function main() { await experiment(async () => { // Your experiment code });}main();