Configuration
Every trail.init() argument, every environment variable, and the mistakes that cost people an
afternoon.
trail.init()
trail.init(
environment="default",
application_name="default",
tracer=None,
event_logger=None,
url=None,
public_key=None,
secret_key=None,
disable_batch=False,
capture_message_content=True,
disabled_instrumentors=None,
meter=None,
disable_metrics=False,
pricing_json=None,
guardrail_id=None,
collect_gpu_stats=False,
)
| Argument | Type | Default | What it does |
|---|---|---|---|
environment |
str |
"default" |
Deployment label → OTel deployment.environment. Set it |
application_name |
str |
"default" |
Service label → OTel service.name. Set it |
tracer |
tracer | None |
Supply your own OTel tracer instead of TrailAI creating one |
event_logger |
provider | None |
Supply your own OTel event logger provider |
url |
str |
None |
TrailAI SDK URL. Falls back to TRAIL_URL |
public_key |
str |
None |
Falls back to TRAIL_PUBLIC_KEY |
secret_key |
str |
None |
Falls back to TRAIL_SECRET_KEY. The older spelling secrect_key= is still accepted |
disable_batch |
bool |
False |
True exports each span immediately instead of batching |
capture_message_content |
bool |
True |
Whether prompts and completions are recorded |
disabled_instrumentors |
list[str] |
None |
Instrumentors to skip |
meter |
meter | None |
Supply your own OTel meter |
disable_metrics |
bool |
False |
True disables metrics export entirely |
pricing_json |
str |
None |
Path or URL to a custom pricing table |
guardrail_id |
str |
None |
Default guardrail for trail.guard.input() calls |
collect_gpu_stats |
bool |
False |
Export NVIDIA/AMD GPU metrics — GPU Monitoring |
Environment variables
| Variable | Used for |
|---|---|
TRAIL_URL |
Base URL, when url= isn't passed |
TRAIL_PUBLIC_KEY |
Public key, when public_key= isn't passed |
TRAIL_SECRET_KEY |
Secret key, when secret_key= isn't passed |
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT |
Set to false to disable content capture regardless of the argument |
TRAIL_PRICING_JSON |
Path or URL of a custom pricing table, when pricing_json= isn't passed |
TRAIL_DISABLE_PRICING_FETCH |
true to use only the pricing table bundled with the SDK (no network) |
TRAIL_GUARDRAIL_TIMEOUT |
Guardrail request timeout in seconds (default 30) |
TRAIL_EXPORT_EVENTS |
true to also export OTel log events (e.g. to a collector). Off by default |
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT |
Max length of a span attribute (default 32768 characters) |
OTEL_BSP_MAX_EXPORT_BATCH_SIZE |
Spans per export request (default 128) |
Since trailai 1.7.0 the SDK passes the endpoint and keys to its exporters directly. It no longer
writes OTEL_EXPORTER_OTLP_ENDPOINT / OTEL_EXPORTER_OTLP_HEADERS, so your secret key isn't
inherited by child processes and your own OTLP configuration is left alone.
Credentials: env vars vs. arguments
Pass credentials as arguments or set the environment variables — every feature (tracing, metrics, prompts, vault, guardrails) works either way:
import trail
# Reads TRAIL_URL, TRAIL_PUBLIC_KEY and TRAIL_SECRET_KEY
trail.init(application_name="support-bot", environment="production")
# …or explicitly
trail.init(
application_name="support-bot",
environment="production",
url="https://your-trail-host/api/sdk/v1",
public_key="pk-trail-…",
secret_key="sk-trail-…",
)
On
trailaiolder than 1.7.0, prompts, vault and guardrails only worked when the credentials were passed as arguments (env vars alone raisedValueError: make sure trail.init is defined), and the argument was spelledsecrect_key. Upgrade, or pass the values explicitly.
The URL format matters
Either form works for TRAIL_URL / url=:
https://your-trail-host/api/sdk/v1 ← recommended (also works for the TypeScript SDK)
https://your-trail-host/api/sdk
The SDK derives every endpoint from it:
| Feature | Path |
|---|---|
| Tracing | /api/sdk/v1/traces |
| Metrics | /api/sdk/v1/metrics |
| Prompts | /api/sdk/v1/prompt/compiled |
| Vault | /api/sdk/v1/vault/secrets |
| Guardrails | /api/sdk/v1/guardrail/detect |
https://your-trail-host/api and a bare host do not work with the Python SDK.
On
trailaiolder than 1.7.0, only…/api/sdk/v1worked for everything;…/api/sdkbroke prompts, vault and guardrails.
Setting application_name and environment
Leave these at their defaults and every chart in the dashboard collapses into one bar labelled
default. Cost-by-application and cost-by-environment become useless.
trail.init(
application_name="support-bot", # one per service
environment=os.getenv("APP_ENV", "development"), # from your deploy config
...
)
Conventions that work:
application_name— the service name, matching whatever your infrastructure calls it:support-bot,rag-indexer,email-summarizer.environment—production,staging,development. Keep the vocabulary small and identical across services, or grouping fragments.
Privacy & content capture
By default the SDK records full prompts and completions on every span. That's what makes Tracing genuinely useful for debugging — and it means user input is stored in TrailAI.
Disable it two ways:
trail.init(..., capture_message_content=False)
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=false
The environment variable wins: set to false, it overrides capture_message_content=True. That makes
it a useful operational kill switch — you can turn capture off across a fleet without a code change.
With capture off, you keep token counts, cost, latency, model, and errors, and lose prompt and completion text. Most teams turn it off in production and leave it on in staging.
Handling regulated data? Turn it off, and consider whether span names derived from your function names could leak anything.
Disabling instrumentors
trail.init(
...,
disabled_instrumentors=["chroma", "pinecone"],
)
Names must match the SDK's instrumentor keys exactly — unknown names are logged as warnings and ignored. The full list is in Auto-Instrumentation.
Reasons to disable one:
- A library's instrumentation conflicts with your own tracing.
- You want to cut span volume from a chatty vector database.
- An instrumentor is misbehaving with your version of a library.
Batching
By default spans are batched (BatchSpanProcessor) — efficient, and the right choice for production.
trail.init(..., disable_batch=True) # export immediately
Use immediate export when debugging — you want the span visible now, not in a few seconds. Don't use it under production load; a network round trip per span costs latency.
Short-lived processes: buffered spans are exported at normal interpreter exit. In serverless handlers (AWS Lambda, Cloud Run jobs) or code that exits with
os._exit(), calltrail.flush()before returning, ortrail.shutdown()when the process is done.
Metrics
trail.init(..., disable_metrics=True)
Metrics are aggregated counters and histograms exported separately from spans. Disabling them reduces export volume; you lose metric-driven views but keep all trace data.
Custom pricing
Cost is computed client-side from a pricing table fetched at init(). A model that isn't in the
table costs 0.
trail.init(..., pricing_json="https://example.com/our-pricing.json")
trail.init(..., pricing_json="/etc/trail/pricing.json") # local file also works
Structure:
{
"chat": {
"our-fine-tuned-model": {
"promptPrice": 0.0015,
"completionPrice": 0.002
}
},
"embeddings": {
"our-embedding-model": 0.0001
}
}
Prices are per 1,000 tokens.
Supply your own when you use self-hosted models, fine-tunes, or negotiated rates. If a self-hosted analytics meaningful.
Bring your own OpenTelemetry
Already running OTel? Pass your existing providers and TrailAI will use them instead of creating its own:
from opentelemetry import trace
trail.init(
application_name="support-bot",
tracer=trace.get_tracer_provider().get_tracer(__name__),
meter=my_meter,
event_logger=my_event_logger_provider,
url=..., public_key=..., secret_key=...,
)
TrailAI's spans then flow through your pipeline — to TrailAI, to your existing backend, or to both, depending on your processor configuration.
Configuration by environment
Development
trail.init(
application_name="support-bot",
environment="development",
disable_batch=True, # immediate feedback
capture_message_content=True, # full debugging detail
url=..., public_key=..., secret_key=...,
)
Production
trail.init(
application_name="support-bot",
environment="production",
disable_batch=False, # batch for efficiency
capture_message_content=False, # don't store user content
url=..., public_key=..., secret_key=...,
)
Serverless / short-lived
trail.init(
application_name="summarizer-lambda",
environment="production",
disable_batch=True, # the process may not live long enough to flush
url=..., public_key=..., secret_key=...,
)
Configuration checklist
-
TRAIL_URLpoints athttps://your-trail-host/api/sdk/v1 -
TRAIL_PUBLIC_KEY/TRAIL_SECRET_KEYset (or passed toinit()) -
application_nameset to something meaningful -
environmentset from your deploy configuration -
capture_message_contentdecided deliberately for production -
trail.flush()before serverless handlers return -
pricing_jsonsupplied if you use custom or self-hosted models -
trail.init()called before any provider client is constructed