Skip to main content
This tutorial takes you from a blank Python script to a production-ready instrumented app. Each step is independent — skip ahead if you know the material.
All examples assume TRULAYER_API_KEY and OPENAI_API_KEY are exported. Install with pip install trulayer openai anthropic.

1. Initialise the SDK

Call trulayer.init() once at app startup. It creates a global client that’s used implicitly by everything else.
If you need multiple clients (multi-tenant app, testing), construct TruLayerClient directly instead.

2. Manually trace a block of code

The simplest unit of instrumentation is the trace() context manager.
Everything between the 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.
See Traces and spans for the full span type catalogue.

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.
The resulting trace has one 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.
The handler creates spans for every LLM call, tool invocation, and retriever call in the chain.

7. Async usage

Use atrace() and await span() on async contexts.
Traces are carried via contextvars, so they survive across asyncio.gather() and task spawns automatically.

8. Group traces into a session

Pass session_id when starting a trace to group multiple traces as one conversation.
All traces sharing the same 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.
Avoid putting PII or secrets in metadata. Scope is tenant-wide — anyone on the team can see it.

10. Submit feedback

Feedback from your UI can be attached to a trace at any time (including after ingestion).
You’ll need the 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 a scrub_fn at init time to run every input/output through your redaction logic before it leaves the process.
See best practices for PII and configuration.

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.
See full options in configuration.

13. Verify shutdown

In short-lived scripts, ensure trulayer.shutdown() is called so buffered traces are flushed.
Long-lived services (web servers) don’t need this — batches flush on interval, and most frameworks have graceful-shutdown hooks you can wire up.