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.
Basic usage
init.ts
Overview
Theinit() function:
- Configures authentication with your Gentrace API key
- Sets up global client instances for making API requests
- Establishes connection settings like base URL, timeouts, and retry behavior
- Enables SDK functionality by making other Gentrace functions available
- Automatically configures OpenTelemetry (by default) with proper exporters and settings
Parameters
Stainless SDK Generation: The parameters and client options
described in this section are automatically generated by the
Stainless API SDK generation platform.
Function signatures
Parameters
- TypeScript
- Python
ClientOptions
default:"{}"
Configuration options for initializing the Gentrace SDK client.
Advanced configuration
Automatic OpenTelemetry setup (default)
auto-otel.ts
Custom OpenTelemetry configuration
custom-otel.ts
Custom OTEL collector endpoint
If you’re using your own OpenTelemetry collector, configure the trace exporter in theotelSetup configuration:
custom-collector.ts
Common OTEL Collector configurations: - Local development:
http://localhost:4318/v1/traces - Docker:
http://otel-collector:4318/v1/traces - Kubernetes:
http://otel-collector.observability.svc.cluster.local:4318/v1/traces- Cloud providers: Check your provider’s documentation for the correct endpoint
Disable automatic OpenTelemetry setup
manual-otel.ts
Custom base URL and timeout
custom-config.ts
Custom headers and logging
headers-logging.ts
Environment variables
Instead of passing configuration options directly toinit(), consider using environment variables:
GENTRACE_API_KEY: Your Gentrace API keyGENTRACE_BASE_URL: Custom base URL for the API (useful for self-hosted instances)GENTRACE_LOG(TypeScript only): Log level setting
.env
env-init.ts
Error handling
Theinit() function will throw an error if required configuration is missing:
error-handling.ts
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.Best practices
1. Initialize early
Callinit() before your main execution logic to ensure Gentrace is configured before using any tracing functionality:
early-init.ts
2. Use environment variables
Store sensitive configuration like API keys in environment variables rather than hardcoding them:Requirements
- API Key: A valid Gentrace API key is required. Generate one at https://gentrace.ai/s/api-keys
- Network Access: The SDK needs to communicate with the Gentrace API
- Environment: Node.js 20+ (TypeScript) or Python 3.8+ (Python)
Related functions
traced()- Lower-level function tracinginteraction()- Instrument AI functions for tracingexperiment()- Create testing contexts for grouping evaluationsevalDataset()/eval_dataset()- Run tests against datasetseval()/evalOnce()- Run individual test cases