All examples assume
TRULAYER_API_KEY and OPENAI_API_KEY are exported. Install with pip install trulayer openai anthropic.1. Initialise the SDK
Calltrulayer.init() once at app startup. It creates a global client that’s used implicitly by everything else.
TruLayerClient directly instead.
2. Manually trace a block of code
The simplest unit of instrumentation is thetrace() context manager.
with and the end of the block is captured as one trace.
3. Add spans for sub-steps
Within a trace, create spans to break down the work.4. Auto-instrument OpenAI
Skip the manual span wrapping —instrument_openai() patches the client so every chat.completions.create() call automatically becomes a span inside the currently-active trace.
llm span containing the full messages array, the response, tokens, latency, and the model name — no extra code.
5. Auto-instrument Anthropic
Identical pattern for the Anthropic SDK.6. Use LangChain
LangChain integration uses a callback handler rather than monkey-patching.7. Async usage
Useatrace() and await span() on async contexts.
contextvars, so they survive across asyncio.gather() and task spawns automatically.
8. Group traces into a session
Passsession_id when starting a trace to group multiple traces as one conversation.
session_id appear together in the dashboard’s session view. See Sessions.
9. Attach metadata
Set any key-value pairs on a trace or span — used as dashboard filters.10. Submit feedback
Feedback from your UI can be attached to a trace at any time (including after ingestion).trace_id — the easiest way is to surface it from trace.id inside the with block and return it alongside your response.
11. Redact PII
Pass ascrub_fn at init time to run every input/output through your redaction logic before it leaves the process.
12. Production configuration
Before shipping, tune:sample_rate— a value between 0.0 and 1.0; applied at trace creation. Start at 1.0 and lower if ingest cost becomes an issue.batch_size/flush_interval— defaults are fine for most apps.scrub_fn— non-negotiable if your inputs can contain user PII.metadata_validator— optional callback to reject traces with malformed metadata.
13. Verify shutdown
In short-lived scripts, ensuretrulayer.shutdown() is called so buffered traces are flushed.